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 <noreply@anthropic.com>
This commit is contained in:
gavrielc
2026-01-31 19:32:18 +02:00
parent cbe33f4ba6
commit 22bd3d7c58
3 changed files with 15 additions and 8 deletions

View File

@@ -22,6 +22,7 @@ export function initDatabase(): void {
id TEXT, id TEXT,
chat_jid TEXT, chat_jid TEXT,
sender TEXT, sender TEXT,
sender_name TEXT,
content TEXT, content TEXT,
timestamp TEXT, timestamp TEXT,
is_from_me INTEGER, is_from_me INTEGER,
@@ -30,9 +31,14 @@ export function initDatabase(): void {
); );
CREATE INDEX IF NOT EXISTS idx_timestamp ON messages(timestamp); 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; if (!msg.key) return;
const content = 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 timestamp = new Date(Number(msg.messageTimestamp) * 1000).toISOString();
const sender = msg.key.participant || msg.key.remoteJid || ''; const sender = msg.key.participant || msg.key.remoteJid || '';
const senderName = pushName || sender.split('@')[0];
const msgId = msg.key.id || ''; const msgId = msg.key.id || '';
db.prepare(`INSERT OR REPLACE INTO chats (jid, name, last_message_time) VALUES (?, ?, ?)`) db.prepare(`INSERT OR REPLACE INTO chats (jid, name, last_message_time) VALUES (?, ?, ?)`)
.run(chatJid, chatJid, timestamp); .run(chatJid, chatJid, timestamp);
db.prepare(`INSERT OR REPLACE INTO messages (id, chat_jid, sender, content, timestamp, is_from_me) VALUES (?, ?, ?, ?, ?, ?)`) db.prepare(`INSERT OR REPLACE INTO messages (id, chat_jid, sender, sender_name, content, timestamp, is_from_me) VALUES (?, ?, ?, ?, ?, ?, ?)`)
.run(msgId, chatJid, sender, content, timestamp, isFromMe ? 1 : 0); .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): { messages: NewMessage[]; newTimestamp: string } {
@@ -57,7 +64,7 @@ export function getNewMessages(jids: string[], lastTimestamp: string): { message
const placeholders = jids.map(() => '?').join(','); const placeholders = jids.map(() => '?').join(',');
const sql = ` const sql = `
SELECT id, chat_jid, sender, content, timestamp SELECT id, chat_jid, sender, sender_name, content, timestamp
FROM messages FROM messages
WHERE timestamp > ? AND chat_jid IN (${placeholders}) WHERE timestamp > ? AND chat_jid IN (${placeholders})
ORDER BY timestamp ORDER BY timestamp
@@ -75,7 +82,7 @@ export function getNewMessages(jids: string[], lastTimestamp: string): { message
export function getMessagesSince(chatJid: string, sinceTimestamp: string): NewMessage[] { export function getMessagesSince(chatJid: string, sinceTimestamp: string): NewMessage[] {
const sql = ` const sql = `
SELECT id, chat_jid, sender, content, timestamp SELECT id, chat_jid, sender, sender_name, content, timestamp
FROM messages FROM messages
WHERE chat_jid = ? AND timestamp > ? WHERE chat_jid = ? AND timestamp > ?
ORDER BY timestamp ORDER BY timestamp

View File

@@ -95,8 +95,7 @@ async function processMessage(msg: NewMessage): Promise<void> {
// Build prompt with conversation history // Build prompt with conversation history
const lines = missedMessages.map(m => { const lines = missedMessages.map(m => {
const time = new Date(m.timestamp).toLocaleTimeString(); const time = new Date(m.timestamp).toLocaleTimeString();
const sender = m.sender.split('@')[0]; return `[${time}] ${m.sender_name}: ${m.content}`;
return `[${time}] ${sender}: ${m.content}`;
}); });
const prompt = lines.join('\n'); const prompt = lines.join('\n');
@@ -213,7 +212,7 @@ async function connectWhatsApp(): Promise<void> {
if (!msg.message) continue; if (!msg.message) continue;
const chatJid = msg.key.remoteJid; const chatJid = msg.key.remoteJid;
if (!chatJid || chatJid === 'status@broadcast') continue; if (!chatJid || chatJid === 'status@broadcast') continue;
storeMessage(msg, chatJid, msg.key.fromMe || false); storeMessage(msg, chatJid, msg.key.fromMe || false, msg.pushName || undefined);
} }
}); });
} }

View File

@@ -13,6 +13,7 @@ export interface NewMessage {
id: string; id: string;
chat_jid: string; chat_jid: string;
sender: string; sender: string;
sender_name: string;
content: string; content: string;
timestamp: string; timestamp: string;
} }