Skip to content

Commit 5386d71

Browse files
Merge pull request #1732 from rafwell/testmsg
Ignore events that are not messages (like EPHEMERAL_SYNC_RESPONSE)
2 parents f59cae7 + afd0e01 commit 5386d71

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,13 @@ export class ChatwootService {
567567
}
568568

569569
public async createConversation(instance: InstanceDto, body: any) {
570+
if (!body?.key) {
571+
this.logger.warn(
572+
`body.key is null or undefined in createConversation. Full body object: ${JSON.stringify(body)}`,
573+
);
574+
return null;
575+
}
576+
570577
const isLid = body.key.previousRemoteJid?.includes('@lid') && body.key.senderPn;
571578
const remoteJid = body.key.remoteJid;
572579
const cacheKey = `${instance.instanceName}:createConversation-${remoteJid}`;
@@ -1893,6 +1900,12 @@ export class ChatwootService {
18931900

18941901
public async eventWhatsapp(event: string, instance: InstanceDto, body: any) {
18951902
try {
1903+
// Ignore events that are not messages (like EPHEMERAL_SYNC_RESPONSE)
1904+
if (body?.type && body.type !== 'message' && body.type !== 'conversation') {
1905+
this.logger.verbose(`Ignoring non-message event type: ${body.type}`);
1906+
return;
1907+
}
1908+
18961909
const waInstance = this.waMonitor.waInstances[instance.instanceName];
18971910

18981911
if (!waInstance) {
@@ -1938,6 +1951,11 @@ export class ChatwootService {
19381951
}
19391952

19401953
if (event === 'messages.upsert' || event === 'send.message') {
1954+
if (!body?.key) {
1955+
this.logger.warn(`body.key is null or undefined. Full body object: ${JSON.stringify(body)}`);
1956+
return;
1957+
}
1958+
19411959
if (body.key.remoteJid === 'status@broadcast') {
19421960
return;
19431961
}
@@ -2260,10 +2278,23 @@ export class ChatwootService {
22602278
}
22612279

22622280
if (event === 'messages.edit' || event === 'send.message.update') {
2281+
// Ignore events that are not messages (like EPHEMERAL_SYNC_RESPONSE)
2282+
if (body?.type && body.type !== 'message') {
2283+
this.logger.verbose(`Ignoring non-message event type: ${body.type}`);
2284+
return;
2285+
}
2286+
2287+
if (!body?.key?.id) {
2288+
this.logger.warn(
2289+
`body.key.id is null or undefined in messages.edit. Full body object: ${JSON.stringify(body)}`,
2290+
);
2291+
return;
2292+
}
2293+
22632294
const editedText = `${
22642295
body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text
22652296
}\n\n_\`${i18next.t('cw.message.edited')}.\`_`;
2266-
const message = await this.getMessageByKeyId(instance, body?.key?.id);
2297+
const message = await this.getMessageByKeyId(instance, body.key.id);
22672298
const key = message.key as {
22682299
id: string;
22692300
fromMe: boolean;

0 commit comments

Comments
 (0)