Add source_id and update message

This commit is contained in:
raimartinsb 2023-08-31 07:52:36 -03:00
parent 5bc33ac654
commit 7289c3d7f9
4 changed files with 88 additions and 8 deletions

View File

@ -43,7 +43,7 @@
"dependencies": { "dependencies": {
"@adiwajshing/keyed-db": "^0.2.4", "@adiwajshing/keyed-db": "^0.2.4",
"@ffmpeg-installer/ffmpeg": "^1.1.0", "@ffmpeg-installer/ffmpeg": "^1.1.0",
"@figuro/chatwoot-sdk": "^1.1.14", "@figuro/chatwoot-sdk": "github:raimartinsb/chatwoot-sdk",
"@hapi/boom": "^10.0.1", "@hapi/boom": "^10.0.1",
"@sentry/node": "^7.59.2", "@sentry/node": "^7.59.2",
"@whiskeysockets/baileys": "github:EvolutionAPI/Baileys", "@whiskeysockets/baileys": "github:EvolutionAPI/Baileys",

View File

@ -20,6 +20,8 @@ export class MessageRaw {
messageTimestamp?: number | Long.Long; messageTimestamp?: number | Long.Long;
owner: string; owner: string;
source?: 'android' | 'web' | 'ios'; source?: 'android' | 'web' | 'ios';
source_id?: string;
source_reply_id?: string;
} }
const messageSchema = new Schema<MessageRaw>({ const messageSchema = new Schema<MessageRaw>({

View File

@ -608,6 +608,8 @@ export class ChatwootService {
conversationId: number, conversationId: number,
content: string, content: string,
messageType: 'incoming' | 'outgoing' | undefined, messageType: 'incoming' | 'outgoing' | undefined,
source_id?: string,
source_reply_id?: string,
privateMessage?: boolean, privateMessage?: boolean,
attachments?: { attachments?: {
content: unknown; content: unknown;
@ -633,6 +635,8 @@ export class ChatwootService {
message_type: messageType, message_type: messageType,
attachments: attachments, attachments: attachments,
private: privateMessage || false, 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'); this.logger.verbose('audio sent');
return; return audioWhatsapp;
} }
this.logger.verbose('send media to instance: ' + waInstance.instanceName); this.logger.verbose('send media to instance: ' + waInstance.instanceName);
@ -934,10 +938,10 @@ export class ChatwootService {
data.mediaMessage.caption = caption; data.mediaMessage.caption = caption;
} }
await waInstance?.mediaMessage(data); const mediaMessage = await waInstance?.mediaMessage(data);
this.logger.verbose('media sent'); this.logger.verbose('media sent');
return; return mediaMessage;
} catch (error) { } catch (error) {
this.logger.error(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; 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) { private getMessageContent(types: any) {
this.logger.verbose('get message content'); this.logger.verbose('get message content');
const typeKey = Object.keys(types).find((key) => types[key] !== undefined); const typeKey = Object.keys(types).find((key) => types[key] !== undefined);
@ -1241,6 +1288,18 @@ export class ChatwootService {
return messageContent; 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) { public async eventWhatsapp(event: string, instance: InstanceDto, body: any) {
this.logger.verbose('event whatsapp to instance: ' + instance.instanceName); this.logger.verbose('event whatsapp to instance: ' + instance.instanceName);
try { try {
@ -1269,6 +1328,8 @@ export class ChatwootService {
this.logger.verbose('get conversation message'); this.logger.verbose('get conversation message');
const bodyMessage = await this.getConversationMessage(body.message); const bodyMessage = await this.getConversationMessage(body.message);
const source_reply_id = this.getContextConversationMessage(body.message);
const isMedia = this.isMediaMessage(body.message); const isMedia = this.isMediaMessage(body.message);
if (!bodyMessage && !isMedia) { if (!bodyMessage && !isMedia) {
@ -1286,6 +1347,8 @@ export class ChatwootService {
const messageType = body.key.fromMe ? 'outgoing' : 'incoming'; const messageType = body.key.fromMe ? 'outgoing' : 'incoming';
const source_id = body.key?.id;
this.logger.verbose('message type: ' + messageType); this.logger.verbose('message type: ' + messageType);
this.logger.verbose('is media: ' + isMedia); this.logger.verbose('is media: ' + isMedia);
@ -1387,7 +1450,14 @@ export class ChatwootService {
} }
this.logger.verbose('send data to chatwoot'); 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) { if (!send) {
this.logger.warn('message not sent'); this.logger.warn('message not sent');
@ -1408,7 +1478,14 @@ export class ChatwootService {
this.logger.verbose('message is not group'); this.logger.verbose('message is not group');
this.logger.verbose('send data to chatwoot'); 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) { if (!send) {
this.logger.warn('message not sent'); this.logger.warn('message not sent');

View File

@ -1992,6 +1992,7 @@ export class WAStartupService {
messageTimestamp: messageSent.messageTimestamp as number, messageTimestamp: messageSent.messageTimestamp as number,
owner: this.instance.name, owner: this.instance.name,
source: getDevice(messageSent.key.id), source: getDevice(messageSent.key.id),
source_id: messageSent.key.id,
}; };
this.logger.log(messageRaw); this.logger.log(messageRaw);