feat: add Baileys message normalizer
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { proto } from '@whiskeysockets/baileys';
|
||||
import { normalizeMessage } from './normalizer';
|
||||
|
||||
function makeMsg(overrides: Partial<proto.IWebMessageInfo> = {}): proto.IWebMessageInfo {
|
||||
return {
|
||||
key: {
|
||||
remoteJid: '120363043312345678@g.us',
|
||||
fromMe: false,
|
||||
id: 'ABCDEF123456',
|
||||
participant: '919876543210@s.whatsapp.net',
|
||||
},
|
||||
pushName: 'Alice',
|
||||
message: { conversation: 'Hello world' },
|
||||
...overrides,
|
||||
} as proto.IWebMessageInfo;
|
||||
}
|
||||
|
||||
describe('normalizeMessage', () => {
|
||||
it('normalizes a plain text conversation message', () => {
|
||||
const result = normalizeMessage(makeMsg());
|
||||
expect(result).toMatchObject({
|
||||
platformMsgId: 'ABCDEF123456',
|
||||
sourceGroupJid: '120363043312345678@g.us',
|
||||
senderJid: '919876543210@s.whatsapp.net',
|
||||
senderName: 'Alice',
|
||||
content: 'Hello world',
|
||||
});
|
||||
});
|
||||
|
||||
it('normalizes an extendedTextMessage', () => {
|
||||
const result = normalizeMessage(makeMsg({
|
||||
message: { extendedTextMessage: { text: 'Extended text here' } },
|
||||
}));
|
||||
expect(result?.content).toBe('Extended text here');
|
||||
});
|
||||
|
||||
it('normalizes an imageMessage caption', () => {
|
||||
const result = normalizeMessage(makeMsg({
|
||||
message: { imageMessage: { caption: 'Photo caption' } },
|
||||
}));
|
||||
expect(result?.content).toBe('Photo caption');
|
||||
});
|
||||
|
||||
it('normalizes a videoMessage caption', () => {
|
||||
const result = normalizeMessage(makeMsg({
|
||||
message: { videoMessage: { caption: 'Video caption' } },
|
||||
}));
|
||||
expect(result?.content).toBe('Video caption');
|
||||
});
|
||||
|
||||
it('returns null for own messages (fromMe = true)', () => {
|
||||
const result = normalizeMessage(makeMsg({ key: { remoteJid: '120363043312345678@g.us', fromMe: true, id: 'XYZ', participant: '919876543210@s.whatsapp.net' } }));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null for non-group messages (DMs)', () => {
|
||||
const result = normalizeMessage(makeMsg({
|
||||
key: { remoteJid: '919876543210@s.whatsapp.net', fromMe: false, id: 'XYZ' },
|
||||
}));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when message payload is missing', () => {
|
||||
const result = normalizeMessage(makeMsg({ message: null }));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when key is missing', () => {
|
||||
const result = normalizeMessage({ message: { conversation: 'hi' } } as proto.IWebMessageInfo);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -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