Skip to content

Commit 7289c3d

Browse files
committed
Add source_id and update message
1 parent 5bc33ac commit 7289c3d

4 files changed

Lines changed: 88 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"dependencies": {
4444
"@adiwajshing/keyed-db": "^0.2.4",
4545
"@ffmpeg-installer/ffmpeg": "^1.1.0",
46-
"@figuro/chatwoot-sdk": "^1.1.14",
46+
"@figuro/chatwoot-sdk": "github:raimartinsb/chatwoot-sdk",
4747
"@hapi/boom": "^10.0.1",
4848
"@sentry/node": "^7.59.2",
4949
"@whiskeysockets/baileys": "github:EvolutionAPI/Baileys",

src/whatsapp/models/message.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export class MessageRaw {
2020
messageTimestamp?: number | Long.Long;
2121
owner: string;
2222
source?: 'android' | 'web' | 'ios';
23+
source_id?: string;
24+
source_reply_id?: string;
2325
}
2426

2527
const messageSchema = new Schema<MessageRaw>({

src/whatsapp/services/chatwoot.service.ts

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,8 @@ export class ChatwootService {
608608
conversationId: number,
609609
content: string,
610610
messageType: 'incoming' | 'outgoing' | undefined,
611+
source_id?: string,
612+
source_reply_id?: string,
611613
privateMessage?: boolean,
612614
attachments?: {
613615
content: unknown;
@@ -633,6 +635,8 @@ export class ChatwootService {
633635
message_type: messageType,
634636
attachments: attachments,
635637
private: privateMessage || false,
638+
source_id: source_id,
639+
source_reply_id: source_reply_id,
636640
},
637641
});
638642

@@ -909,10 +913,10 @@ export class ChatwootService {
909913
},
910914
};
911915

912-
await waInstance?.audioWhatsapp(data);
916+
const audioWhatsapp = await waInstance?.audioWhatsapp(data);
913917

914918
this.logger.verbose('audio sent');
915-
return;
919+
return audioWhatsapp;
916920
}
917921

918922
this.logger.verbose('send media to instance: ' + waInstance.instanceName);
@@ -934,10 +938,10 @@ export class ChatwootService {
934938
data.mediaMessage.caption = caption;
935939
}
936940

937-
await waInstance?.mediaMessage(data);
941+
const mediaMessage = await waInstance?.mediaMessage(data);
938942

939943
this.logger.verbose('media sent');
940-
return;
944+
return mediaMessage;
941945
} catch (error) {
942946
this.logger.error(error);
943947
}
@@ -1067,7 +1071,18 @@ export class ChatwootService {
10671071
},
10681072
};
10691073

1070-
await waInstance?.textMessage(data);
1074+
const message = await waInstance?.textMessage(data);
1075+
const conversationId = body?.conversation?.id;
1076+
const messageId = body?.id;
1077+
const dataUpdated = {
1078+
source_id: message.key.id,
1079+
};
1080+
await client.messages.update({
1081+
accountId: this.provider.account_id,
1082+
conversationId,
1083+
data: dataUpdated,
1084+
messageId,
1085+
});
10711086
}
10721087
}
10731088
}
@@ -1142,6 +1157,38 @@ export class ChatwootService {
11421157
return types;
11431158
}
11441159

1160+
private getContextIdTypeMessage(msg: any) {
1161+
this.logger.verbose('get type message');
1162+
1163+
const types = {
1164+
conversation: msg.conversation?.contextInfo?.stanzaId,
1165+
imageMessage: msg.imageMessage?.contextInfo?.stanzaId,
1166+
videoMessage: msg.videoMessage?.contextInfo?.stanzaId,
1167+
extendedTextMessage: msg.extendedTextMessage?.contextInfo?.stanzaId,
1168+
messageContextInfo: msg.messageContextInfo?.stanzaId,
1169+
stickerMessage: undefined,
1170+
documentMessage: msg.documentMessage?.contextInfo?.stanzaId,
1171+
documentWithCaptionMessage: msg.documentWithCaptionMessage?.message?.documentMessage?.contextInfo?.stanzaId,
1172+
audioMessage: msg.audioMessage?.contextInfo?.stanzaId,
1173+
contactMessage: msg.contactMessage?.vcard,
1174+
contactsArrayMessage: msg.contactsArrayMessage,
1175+
locationMessage: msg.locationMessage,
1176+
liveLocationMessage: msg.liveLocationMessage,
1177+
};
1178+
1179+
this.logger.verbose('type message: ' + types);
1180+
1181+
return types;
1182+
}
1183+
1184+
private getContextMessageContent(types: any) {
1185+
this.logger.verbose('get message content');
1186+
const typeKey = Object.keys(types).find((key) => types[key] !== undefined);
1187+
1188+
const result = typeKey ? types[typeKey] : undefined;
1189+
return result;
1190+
}
1191+
11451192
private getMessageContent(types: any) {
11461193
this.logger.verbose('get message content');
11471194
const typeKey = Object.keys(types).find((key) => types[key] !== undefined);
@@ -1241,6 +1288,18 @@ export class ChatwootService {
12411288
return messageContent;
12421289
}
12431290

1291+
private getContextConversationMessage(msg: any) {
1292+
this.logger.verbose('get context conversation message');
1293+
1294+
const types = this.getContextIdTypeMessage(msg);
1295+
1296+
const messageContent = this.getContextMessageContent(types);
1297+
1298+
this.logger.verbose('context conversation message: ' + messageContent);
1299+
1300+
return messageContent;
1301+
}
1302+
12441303
public async eventWhatsapp(event: string, instance: InstanceDto, body: any) {
12451304
this.logger.verbose('event whatsapp to instance: ' + instance.instanceName);
12461305
try {
@@ -1269,6 +1328,8 @@ export class ChatwootService {
12691328
this.logger.verbose('get conversation message');
12701329
const bodyMessage = await this.getConversationMessage(body.message);
12711330

1331+
const source_reply_id = this.getContextConversationMessage(body.message);
1332+
12721333
const isMedia = this.isMediaMessage(body.message);
12731334

12741335
if (!bodyMessage && !isMedia) {
@@ -1286,6 +1347,8 @@ export class ChatwootService {
12861347

12871348
const messageType = body.key.fromMe ? 'outgoing' : 'incoming';
12881349

1350+
const source_id = body.key?.id;
1351+
12891352
this.logger.verbose('message type: ' + messageType);
12901353

12911354
this.logger.verbose('is media: ' + isMedia);
@@ -1387,7 +1450,14 @@ export class ChatwootService {
13871450
}
13881451

13891452
this.logger.verbose('send data to chatwoot');
1390-
const send = await this.createMessage(instance, getConversion, content, messageType);
1453+
const send = await this.createMessage(
1454+
instance,
1455+
getConversion,
1456+
content,
1457+
messageType,
1458+
source_id,
1459+
source_reply_id,
1460+
);
13911461

13921462
if (!send) {
13931463
this.logger.warn('message not sent');
@@ -1408,7 +1478,14 @@ export class ChatwootService {
14081478
this.logger.verbose('message is not group');
14091479

14101480
this.logger.verbose('send data to chatwoot');
1411-
const send = await this.createMessage(instance, getConversion, bodyMessage, messageType);
1481+
const send = await this.createMessage(
1482+
instance,
1483+
getConversion,
1484+
bodyMessage,
1485+
messageType,
1486+
source_id,
1487+
source_reply_id,
1488+
);
14121489

14131490
if (!send) {
14141491
this.logger.warn('message not sent');

src/whatsapp/services/whatsapp.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,7 @@ export class WAStartupService {
19921992
messageTimestamp: messageSent.messageTimestamp as number,
19931993
owner: this.instance.name,
19941994
source: getDevice(messageSent.key.id),
1995+
source_id: messageSent.key.id,
19951996
};
19961997

19971998
this.logger.log(messageRaw);

0 commit comments

Comments
 (0)