Fix timezone handling and message filtering

- Add TIMEZONE config using system timezone for cron expressions
- Filter bot messages by content prefix instead of is_from_me
  (user shares WhatsApp account with bot)
- Format messages as XML for cleaner agent parsing
- Update schedule_task tool to clarify local time usage

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Gavriel
2026-02-01 22:54:44 +02:00
parent 066eeb9646
commit 5760b75fa9
5 changed files with 31 additions and 21 deletions

View File

@@ -162,18 +162,19 @@ export function storeMessage(msg: proto.IWebMessageInfo, chatJid: string, isFrom
.run(msgId, chatJid, sender, senderName, content, timestamp, isFromMe ? 1 : 0);
}
export function getNewMessages(jids: string[], lastTimestamp: string): { messages: NewMessage[]; newTimestamp: string } {
export function getNewMessages(jids: string[], lastTimestamp: string, botPrefix: string): { messages: NewMessage[]; newTimestamp: string } {
if (jids.length === 0) return { messages: [], newTimestamp: lastTimestamp };
const placeholders = jids.map(() => '?').join(',');
// Filter out bot's own messages by checking content prefix (not is_from_me, since user shares the account)
const sql = `
SELECT id, chat_jid, sender, sender_name, content, timestamp
FROM messages
WHERE timestamp > ? AND chat_jid IN (${placeholders})
WHERE timestamp > ? AND chat_jid IN (${placeholders}) AND content NOT LIKE ?
ORDER BY timestamp
`;
const rows = db.prepare(sql).all(lastTimestamp, ...jids) as NewMessage[];
const rows = db.prepare(sql).all(lastTimestamp, ...jids, `${botPrefix}:%`) as NewMessage[];
let newTimestamp = lastTimestamp;
for (const row of rows) {
@@ -183,14 +184,15 @@ export function getNewMessages(jids: string[], lastTimestamp: string): { message
return { messages: rows, newTimestamp };
}
export function getMessagesSince(chatJid: string, sinceTimestamp: string): NewMessage[] {
export function getMessagesSince(chatJid: string, sinceTimestamp: string, botPrefix: string): NewMessage[] {
// Filter out bot's own messages by checking content prefix
const sql = `
SELECT id, chat_jid, sender, sender_name, content, timestamp
FROM messages
WHERE chat_jid = ? AND timestamp > ?
WHERE chat_jid = ? AND timestamp > ? AND content NOT LIKE ?
ORDER BY timestamp
`;
return db.prepare(sql).all(chatJid, sinceTimestamp) as NewMessage[];
return db.prepare(sql).all(chatJid, sinceTimestamp, `${botPrefix}:%`) as NewMessage[];
}
export function createTask(task: Omit<ScheduledTask, 'last_run' | 'last_result'>): void {