From 22bd3d7c5802eb937454b38ccf038ad9270096f7 Mon Sep 17 00:00:00 2001 From: gavrielc Date: Sat, 31 Jan 2026 19:32:18 +0200 Subject: [PATCH] Store and display sender's WhatsApp name Use pushName from baileys to get the sender's display name instead of just the phone number. Falls back to phone number if no name. Includes migration to add sender_name column to existing databases. Co-Authored-By: Claude Opus 4.5 --- src/db.ts | 17 ++++++++++++----- src/index.ts | 5 ++--- src/types.ts | 1 + 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/db.ts b/src/db.ts index d7cd7f9..2056575 100644 --- a/src/db.ts +++ b/src/db.ts @@ -22,6 +22,7 @@ export function initDatabase(): void { id TEXT, chat_jid TEXT, sender TEXT, + sender_name TEXT, content TEXT, timestamp TEXT, is_from_me INTEGER, @@ -30,9 +31,14 @@ export function initDatabase(): void { ); CREATE INDEX IF NOT EXISTS idx_timestamp ON messages(timestamp); `); + + // Add sender_name column if it doesn't exist (migration for existing DBs) + try { + db.exec(`ALTER TABLE messages ADD COLUMN sender_name TEXT`); + } catch { /* column already exists */ } } -export function storeMessage(msg: proto.IWebMessageInfo, chatJid: string, isFromMe: boolean): void { +export function storeMessage(msg: proto.IWebMessageInfo, chatJid: string, isFromMe: boolean, pushName?: string): void { if (!msg.key) return; const content = @@ -44,12 +50,13 @@ export function storeMessage(msg: proto.IWebMessageInfo, chatJid: string, isFrom const timestamp = new Date(Number(msg.messageTimestamp) * 1000).toISOString(); const sender = msg.key.participant || msg.key.remoteJid || ''; + const senderName = pushName || sender.split('@')[0]; const msgId = msg.key.id || ''; db.prepare(`INSERT OR REPLACE INTO chats (jid, name, last_message_time) VALUES (?, ?, ?)`) .run(chatJid, chatJid, timestamp); - db.prepare(`INSERT OR REPLACE INTO messages (id, chat_jid, sender, content, timestamp, is_from_me) VALUES (?, ?, ?, ?, ?, ?)`) - .run(msgId, chatJid, sender, content, timestamp, isFromMe ? 1 : 0); + db.prepare(`INSERT OR REPLACE INTO messages (id, chat_jid, sender, sender_name, content, timestamp, is_from_me) VALUES (?, ?, ?, ?, ?, ?, ?)`) + .run(msgId, chatJid, sender, senderName, content, timestamp, isFromMe ? 1 : 0); } export function getNewMessages(jids: string[], lastTimestamp: string): { messages: NewMessage[]; newTimestamp: string } { @@ -57,7 +64,7 @@ export function getNewMessages(jids: string[], lastTimestamp: string): { message const placeholders = jids.map(() => '?').join(','); const sql = ` - SELECT id, chat_jid, sender, content, timestamp + SELECT id, chat_jid, sender, sender_name, content, timestamp FROM messages WHERE timestamp > ? AND chat_jid IN (${placeholders}) ORDER BY timestamp @@ -75,7 +82,7 @@ export function getNewMessages(jids: string[], lastTimestamp: string): { message export function getMessagesSince(chatJid: string, sinceTimestamp: string): NewMessage[] { const sql = ` - SELECT id, chat_jid, sender, content, timestamp + SELECT id, chat_jid, sender, sender_name, content, timestamp FROM messages WHERE chat_jid = ? AND timestamp > ? ORDER BY timestamp diff --git a/src/index.ts b/src/index.ts index fad0456..12546b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,8 +95,7 @@ async function processMessage(msg: NewMessage): Promise { // Build prompt with conversation history const lines = missedMessages.map(m => { const time = new Date(m.timestamp).toLocaleTimeString(); - const sender = m.sender.split('@')[0]; - return `[${time}] ${sender}: ${m.content}`; + return `[${time}] ${m.sender_name}: ${m.content}`; }); const prompt = lines.join('\n'); @@ -213,7 +212,7 @@ async function connectWhatsApp(): Promise { if (!msg.message) continue; const chatJid = msg.key.remoteJid; if (!chatJid || chatJid === 'status@broadcast') continue; - storeMessage(msg, chatJid, msg.key.fromMe || false); + storeMessage(msg, chatJid, msg.key.fromMe || false, msg.pushName || undefined); } }); } diff --git a/src/types.ts b/src/types.ts index 212caaa..dea2a56 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,6 +13,7 @@ export interface NewMessage { id: string; chat_jid: string; sender: string; + sender_name: string; content: string; timestamp: string; }