Skip to content

Commit

Permalink
Merge pull request EvolutionAPI#474 from jaison-x/edit-message
Browse files Browse the repository at this point in the history
feat(chatwoot): show edited messages from WhatsApp in Chatwoot
  • Loading branch information
DavidsonGomes authored Mar 17, 2024
2 parents 5982212 + 3b77455 commit ee1e462
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/utils/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"cw.locationMessage.locationUrl": "URL",
"cw.contactMessage.contact": "Contact",
"cw.contactMessage.name": "Name",
"cw.contactMessage.number": "Number"
"cw.contactMessage.number": "Number",
"cw.message.edited": "Edited Message"
}
3 changes: 2 additions & 1 deletion src/utils/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"cw.locationMessage.locationUrl": "URL",
"cw.contactMessage.contact": "Contato",
"cw.contactMessage.name": "Nome",
"cw.contactMessage.number": "Número"
"cw.contactMessage.number": "Número",
"cw.message.edited": "Mensagem editada"
}
28 changes: 28 additions & 0 deletions src/whatsapp/services/chatwoot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,34 @@ export class ChatwootService {
}
}

if (event === 'messages.edit') {
const editedText = `${
body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text
}\n\n_\`${i18next.t('cw.message.edited')}.\`_`;
const message = await this.getMessageByKeyId(instance, body?.key?.id);
const messageType = message.key?.fromMe ? 'outgoing' : 'incoming';

if (message && message.chatwoot?.conversationId) {
const send = await this.createMessage(
instance,
message.chatwoot.conversationId,
editedText,
messageType,
false,
[],
{
message: { extendedTextMessage: { contextInfo: { stanzaId: message.key.id } } },
},
'WAID:' + body.key.id,
);
if (!send) {
this.logger.warn('edited message not sent');
return;
}
}
return;
}

if (event === 'messages.read') {
this.logger.verbose('read message from instance: ' + instance.instanceName);

Expand Down
11 changes: 11 additions & 0 deletions src/whatsapp/services/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,17 @@ export class BaileysStartupService extends WAStartupService {
try {
this.logger.verbose('Event received: messages.upsert');
for (const received of messages) {
if (
this.localChatwoot.enabled &&
(received.message?.protocolMessage?.editedMessage || received.message?.editedMessage?.message)
) {
const editedMessage =
received.message?.protocolMessage || received.message?.editedMessage?.message?.protocolMessage;
if (editedMessage) {
this.chatwootService.eventWhatsapp('messages.edit', { instanceName: this.instance.name }, editedMessage);
}
}

if (
(type !== 'notify' && type !== 'append') ||
received.message?.protocolMessage ||
Expand Down

0 comments on commit ee1e462

Please sign in to comment.