diff --git a/package.json b/package.json index 6f92aaf3..c28271ca 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "dependencies": { "@adiwajshing/keyed-db": "^0.2.4", "@ffmpeg-installer/ffmpeg": "^1.1.0", - "@figuro/chatwoot-sdk": "^1.1.14", + "@figuro/chatwoot-sdk": "github:raimartinsb/chatwoot-sdk", "@hapi/boom": "^10.0.1", "@sentry/node": "^7.59.2", "@whiskeysockets/baileys": "github:EvolutionAPI/Baileys", diff --git a/src/whatsapp/models/message.model.ts b/src/whatsapp/models/message.model.ts index a5790be7..252cd6e4 100644 --- a/src/whatsapp/models/message.model.ts +++ b/src/whatsapp/models/message.model.ts @@ -20,6 +20,8 @@ export class MessageRaw { messageTimestamp?: number | Long.Long; owner: string; source?: 'android' | 'web' | 'ios'; + source_id?: string; + source_reply_id?: string; } const messageSchema = new Schema({ diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 4dc9215c..b489a2c6 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -608,6 +608,8 @@ export class ChatwootService { conversationId: number, content: string, messageType: 'incoming' | 'outgoing' | undefined, + source_id?: string, + source_reply_id?: string, privateMessage?: boolean, attachments?: { content: unknown; @@ -633,6 +635,8 @@ export class ChatwootService { message_type: messageType, attachments: attachments, private: privateMessage || false, + source_id: source_id, + source_reply_id: source_reply_id, }, }); @@ -909,10 +913,10 @@ export class ChatwootService { }, }; - await waInstance?.audioWhatsapp(data); + const audioWhatsapp = await waInstance?.audioWhatsapp(data); this.logger.verbose('audio sent'); - return; + return audioWhatsapp; } this.logger.verbose('send media to instance: ' + waInstance.instanceName); @@ -934,10 +938,10 @@ export class ChatwootService { data.mediaMessage.caption = caption; } - await waInstance?.mediaMessage(data); + const mediaMessage = await waInstance?.mediaMessage(data); this.logger.verbose('media sent'); - return; + return mediaMessage; } catch (error) { this.logger.error(error); } @@ -1067,7 +1071,18 @@ export class ChatwootService { }, }; - await waInstance?.textMessage(data); + const message = await waInstance?.textMessage(data); + const conversationId = body?.conversation?.id; + const messageId = body?.id; + const dataUpdated = { + source_id: message.key.id, + }; + await client.messages.update({ + accountId: this.provider.account_id, + conversationId, + data: dataUpdated, + messageId, + }); } } } @@ -1142,6 +1157,38 @@ export class ChatwootService { return types; } + private getContextIdTypeMessage(msg: any) { + this.logger.verbose('get type message'); + + const types = { + conversation: msg.conversation?.contextInfo?.stanzaId, + imageMessage: msg.imageMessage?.contextInfo?.stanzaId, + videoMessage: msg.videoMessage?.contextInfo?.stanzaId, + extendedTextMessage: msg.extendedTextMessage?.contextInfo?.stanzaId, + messageContextInfo: msg.messageContextInfo?.stanzaId, + stickerMessage: undefined, + documentMessage: msg.documentMessage?.contextInfo?.stanzaId, + documentWithCaptionMessage: msg.documentWithCaptionMessage?.message?.documentMessage?.contextInfo?.stanzaId, + audioMessage: msg.audioMessage?.contextInfo?.stanzaId, + contactMessage: msg.contactMessage?.vcard, + contactsArrayMessage: msg.contactsArrayMessage, + locationMessage: msg.locationMessage, + liveLocationMessage: msg.liveLocationMessage, + }; + + this.logger.verbose('type message: ' + types); + + return types; + } + + private getContextMessageContent(types: any) { + this.logger.verbose('get message content'); + const typeKey = Object.keys(types).find((key) => types[key] !== undefined); + + const result = typeKey ? types[typeKey] : undefined; + return result; + } + private getMessageContent(types: any) { this.logger.verbose('get message content'); const typeKey = Object.keys(types).find((key) => types[key] !== undefined); @@ -1241,6 +1288,18 @@ export class ChatwootService { return messageContent; } + private getContextConversationMessage(msg: any) { + this.logger.verbose('get context conversation message'); + + const types = this.getContextIdTypeMessage(msg); + + const messageContent = this.getContextMessageContent(types); + + this.logger.verbose('context conversation message: ' + messageContent); + + return messageContent; + } + public async eventWhatsapp(event: string, instance: InstanceDto, body: any) { this.logger.verbose('event whatsapp to instance: ' + instance.instanceName); try { @@ -1269,6 +1328,8 @@ export class ChatwootService { this.logger.verbose('get conversation message'); const bodyMessage = await this.getConversationMessage(body.message); + const source_reply_id = this.getContextConversationMessage(body.message); + const isMedia = this.isMediaMessage(body.message); if (!bodyMessage && !isMedia) { @@ -1286,6 +1347,8 @@ export class ChatwootService { const messageType = body.key.fromMe ? 'outgoing' : 'incoming'; + const source_id = body.key?.id; + this.logger.verbose('message type: ' + messageType); this.logger.verbose('is media: ' + isMedia); @@ -1387,7 +1450,14 @@ export class ChatwootService { } this.logger.verbose('send data to chatwoot'); - const send = await this.createMessage(instance, getConversion, content, messageType); + const send = await this.createMessage( + instance, + getConversion, + content, + messageType, + source_id, + source_reply_id, + ); if (!send) { this.logger.warn('message not sent'); @@ -1408,7 +1478,14 @@ export class ChatwootService { this.logger.verbose('message is not group'); this.logger.verbose('send data to chatwoot'); - const send = await this.createMessage(instance, getConversion, bodyMessage, messageType); + const send = await this.createMessage( + instance, + getConversion, + bodyMessage, + messageType, + source_id, + source_reply_id, + ); if (!send) { this.logger.warn('message not sent'); diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index d11946ee..f6a86f04 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -1992,6 +1992,7 @@ export class WAStartupService { messageTimestamp: messageSent.messageTimestamp as number, owner: this.instance.name, source: getDevice(messageSent.key.id), + source_id: messageSent.key.id, }; this.logger.log(messageRaw);