feat: add Baileys message normalizer
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { proto } from '@whiskeysockets/baileys';
|
||||
|
||||
export interface NormalizedMessage {
|
||||
platformMsgId: string;
|
||||
sourceGroupJid: string;
|
||||
senderJid: string;
|
||||
senderName?: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
function extractText(msg: proto.IWebMessageInfo): string {
|
||||
const m = msg.message;
|
||||
if (!m) return '';
|
||||
return (
|
||||
m.conversation ||
|
||||
m.extendedTextMessage?.text ||
|
||||
m.imageMessage?.caption ||
|
||||
m.videoMessage?.caption ||
|
||||
m.documentMessage?.caption ||
|
||||
''
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeMessage(msg: proto.IWebMessageInfo): NormalizedMessage | null {
|
||||
const key = msg.key;
|
||||
if (!key) return null;
|
||||
|
||||
const remoteJid = key.remoteJid ?? '';
|
||||
|
||||
// Only process group messages (group JIDs end with @g.us)
|
||||
if (!remoteJid.endsWith('@g.us')) return null;
|
||||
|
||||
// Skip our own outgoing messages
|
||||
if (key.fromMe) return null;
|
||||
|
||||
if (!msg.message) return null;
|
||||
|
||||
const content = extractText(msg);
|
||||
|
||||
return {
|
||||
platformMsgId: key.id ?? '',
|
||||
sourceGroupJid: remoteJid,
|
||||
senderJid: key.participant ?? '',
|
||||
senderName: msg.pushName ?? undefined,
|
||||
content,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user