Skip to content

Commit 0d2a7ad

Browse files
Merge pull request #1343 from adaptwebtech/new_feature_add_send_update_message_webhook
Adicionando um novo webhook no endpoint de updateMessage
2 parents b89f114 + 7e8044a commit 0d2a7ad

File tree

8 files changed

+107
-87
lines changed

8 files changed

+107
-87
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ RABBITMQ_EVENTS_MESSAGES_EDITED=false
6262
RABBITMQ_EVENTS_MESSAGES_UPDATE=false
6363
RABBITMQ_EVENTS_MESSAGES_DELETE=false
6464
RABBITMQ_EVENTS_SEND_MESSAGE=false
65+
RABBITMQ_EVENTS_SEND_MESSAGE_UPDATE=false
6566
RABBITMQ_EVENTS_CONTACTS_SET=false
6667
RABBITMQ_EVENTS_CONTACTS_UPSERT=false
6768
RABBITMQ_EVENTS_CONTACTS_UPDATE=false
@@ -108,6 +109,7 @@ PUSHER_EVENTS_MESSAGES_EDITED=true
108109
PUSHER_EVENTS_MESSAGES_UPDATE=true
109110
PUSHER_EVENTS_MESSAGES_DELETE=true
110111
PUSHER_EVENTS_SEND_MESSAGE=true
112+
PUSHER_EVENTS_SEND_MESSAGE_UPDATE=true
111113
PUSHER_EVENTS_CONTACTS_SET=true
112114
PUSHER_EVENTS_CONTACTS_UPSERT=true
113115
PUSHER_EVENTS_CONTACTS_UPDATE=true
@@ -149,6 +151,7 @@ WEBHOOK_EVENTS_MESSAGES_EDITED=true
149151
WEBHOOK_EVENTS_MESSAGES_UPDATE=true
150152
WEBHOOK_EVENTS_MESSAGES_DELETE=true
151153
WEBHOOK_EVENTS_SEND_MESSAGE=true
154+
WEBHOOK_EVENTS_SEND_MESSAGE_UPDATE=true
152155
WEBHOOK_EVENTS_CONTACTS_SET=true
153156
WEBHOOK_EVENTS_CONTACTS_UPSERT=true
154157
WEBHOOK_EVENTS_CONTACTS_UPDATE=true

Docker/swarm/evolution_api_v2.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ services:
3434
- RABBITMQ_EVENTS_MESSAGES_UPDATE=false
3535
- RABBITMQ_EVENTS_MESSAGES_DELETE=false
3636
- RABBITMQ_EVENTS_SEND_MESSAGE=false
37+
- RABBITMQ_EVENTS_SEND_MESSAGE_UPDATE=false
3738
- RABBITMQ_EVENTS_CONTACTS_SET=false
3839
- RABBITMQ_EVENTS_CONTACTS_UPSERT=false
3940
- RABBITMQ_EVENTS_CONTACTS_UPDATE=false
@@ -71,6 +72,7 @@ services:
7172
- WEBHOOK_EVENTS_MESSAGES_UPDATE=true
7273
- WEBHOOK_EVENTS_MESSAGES_DELETE=true
7374
- WEBHOOK_EVENTS_SEND_MESSAGE=true
75+
- WEBHOOK_EVENTS_SEND_MESSAGE_UPDATE=true
7476
- WEBHOOK_EVENTS_CONTACTS_SET=true
7577
- WEBHOOK_EVENTS_CONTACTS_UPSERT=true
7678
- WEBHOOK_EVENTS_CONTACTS_UPDATE=true

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 88 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,37 +1135,39 @@ export class BaileysStartupService extends ChannelStartupService {
11351135
const editedMessage =
11361136
received?.message?.protocolMessage || received?.message?.editedMessage?.message?.protocolMessage;
11371137

1138-
if (received.message?.protocolMessage?.editedMessage || received.message?.editedMessage?.message) {
1139-
if (editedMessage) {
1140-
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled)
1141-
this.chatwootService.eventWhatsapp(
1142-
'messages.edit',
1143-
{ instanceName: this.instance.name, instanceId: this.instance.id },
1144-
editedMessage,
1145-
);
1138+
if (received.message?.protocolMessage?.editedMessage && editedMessage) {
1139+
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled)
1140+
this.chatwootService.eventWhatsapp(
1141+
'messages.edit',
1142+
{ instanceName: this.instance.name, instanceId: this.instance.id },
1143+
editedMessage,
1144+
);
11461145

1147-
await this.sendDataWebhook(Events.MESSAGES_EDITED, editedMessage);
1148-
const oldMessage = await this.getMessage(editedMessage.key, true);
1149-
if ((oldMessage as any)?.id) {
1150-
await this.prismaRepository.message.update({
1151-
where: { id: (oldMessage as any).id },
1152-
data: {
1153-
message: editedMessage.editedMessage as any,
1154-
messageTimestamp: (editedMessage.timestampMs as Long.Long).toNumber(),
1155-
status: 'EDITED',
1156-
},
1157-
});
1158-
await this.prismaRepository.messageUpdate.create({
1159-
data: {
1160-
fromMe: editedMessage.key.fromMe,
1161-
keyId: editedMessage.key.id,
1162-
remoteJid: editedMessage.key.remoteJid,
1163-
status: 'EDITED',
1164-
instanceId: this.instanceId,
1165-
messageId: (oldMessage as any).id,
1166-
},
1167-
});
1168-
}
1146+
await this.sendDataWebhook(Events.MESSAGES_EDITED, editedMessage);
1147+
const oldMessage = await this.getMessage(editedMessage.key, true);
1148+
if ((oldMessage as any)?.id) {
1149+
const editedMessageTimestamp = Long.isLong(editedMessage?.timestampMs)
1150+
? editedMessage.timestampMs?.toNumber()
1151+
: (editedMessage.timestampMs as number);
1152+
1153+
await this.prismaRepository.message.update({
1154+
where: { id: (oldMessage as any).id },
1155+
data: {
1156+
message: editedMessage.editedMessage as any,
1157+
messageTimestamp: editedMessageTimestamp,
1158+
status: 'EDITED',
1159+
},
1160+
});
1161+
await this.prismaRepository.messageUpdate.create({
1162+
data: {
1163+
fromMe: editedMessage.key.fromMe,
1164+
keyId: editedMessage.key.id,
1165+
remoteJid: editedMessage.key.remoteJid,
1166+
status: 'EDITED',
1167+
instanceId: this.instanceId,
1168+
messageId: (oldMessage as any).id,
1169+
},
1170+
});
11691171
}
11701172
}
11711173

@@ -3972,71 +3974,71 @@ export class BaileysStartupService extends ChannelStartupService {
39723974
throw new BadRequestException('Message is older than 15 minutes');
39733975
}
39743976

3975-
const response = await this.client.sendMessage(jid, {
3977+
const messageSent = await this.client.sendMessage(jid, {
39763978
...(options as any),
39773979
edit: data.key,
39783980
});
3979-
if (response) {
3980-
const messageId = response.message?.protocolMessage?.key?.id;
3981-
if (messageId) {
3982-
let message = await this.prismaRepository.message.findFirst({
3983-
where: {
3984-
key: {
3985-
path: ['id'],
3986-
equals: messageId,
3981+
if (messageSent) {
3982+
const editedMessage =
3983+
messageSent?.message?.protocolMessage || messageSent?.message?.editedMessage?.message?.protocolMessage;
3984+
3985+
if (editedMessage) {
3986+
this.sendDataWebhook(Events.SEND_MESSAGE_UPDATE, editedMessage);
3987+
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled)
3988+
this.chatwootService.eventWhatsapp(
3989+
'send.message.update',
3990+
{ instanceName: this.instance.name, instanceId: this.instance.id },
3991+
editedMessage,
3992+
);
3993+
3994+
const messageId = messageSent.message?.protocolMessage?.key?.id;
3995+
if (messageId) {
3996+
let message = await this.prismaRepository.message.findFirst({
3997+
where: {
3998+
key: {
3999+
path: ['id'],
4000+
equals: messageId,
4001+
},
39874002
},
3988-
},
3989-
});
3990-
if (!message) throw new NotFoundException('Message not found');
4003+
});
4004+
if (!message) throw new NotFoundException('Message not found');
39914005

3992-
if (!(message.key.valueOf() as any).fromMe) {
3993-
new BadRequestException('You cannot edit others messages');
3994-
}
3995-
if ((message.key.valueOf() as any)?.deleted) {
3996-
new BadRequestException('You cannot edit deleted messages');
3997-
}
3998-
if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') {
3999-
oldMessage.message.conversation = data.text;
4000-
} else {
4001-
oldMessage.message[oldMessage.messageType].caption = data.text;
4002-
}
4003-
message = await this.prismaRepository.message.update({
4004-
where: { id: message.id },
4005-
data: {
4006-
message: oldMessage.message,
4006+
if (!(message.key.valueOf() as any).fromMe) {
4007+
new BadRequestException('You cannot edit others messages');
4008+
}
4009+
if ((message.key.valueOf() as any)?.deleted) {
4010+
new BadRequestException('You cannot edit deleted messages');
4011+
}
4012+
if (oldMessage.messageType === 'conversation' || oldMessage.messageType === 'extendedTextMessage') {
4013+
oldMessage.message.conversation = data.text;
4014+
} else {
4015+
oldMessage.message[oldMessage.messageType].caption = data.text;
4016+
}
4017+
message = await this.prismaRepository.message.update({
4018+
where: { id: message.id },
4019+
data: {
4020+
message: oldMessage.message,
4021+
status: 'EDITED',
4022+
messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds
4023+
},
4024+
});
4025+
const messageUpdate: any = {
4026+
messageId: message.id,
4027+
keyId: messageId,
4028+
remoteJid: messageSent.key.remoteJid,
4029+
fromMe: messageSent.key.fromMe,
4030+
participant: messageSent.key?.remoteJid,
40074031
status: 'EDITED',
4008-
messageTimestamp: Math.floor(Date.now() / 1000), // Convert to int32 by dividing by 1000 to get seconds
4009-
},
4010-
});
4011-
const messageUpdate: any = {
4012-
messageId: message.id,
4013-
keyId: messageId,
4014-
remoteJid: response.key.remoteJid,
4015-
fromMe: response.key.fromMe,
4016-
participant: response.key?.remoteJid,
4017-
status: 'EDITED',
4018-
instanceId: this.instanceId,
4019-
};
4020-
await this.prismaRepository.messageUpdate.create({
4021-
data: messageUpdate,
4022-
});
4023-
4024-
this.sendDataWebhook(Events.MESSAGES_EDITED, {
4025-
id: message.id,
4026-
instanceId: message.instanceId,
4027-
key: message.key,
4028-
messageType: message.messageType,
4029-
status: 'EDITED',
4030-
source: message.source,
4031-
messageTimestamp: message.messageTimestamp,
4032-
pushName: message.pushName,
4033-
participant: message.participant,
4034-
message: message.message,
4035-
});
4032+
instanceId: this.instanceId,
4033+
};
4034+
await this.prismaRepository.messageUpdate.create({
4035+
data: messageUpdate,
4036+
});
4037+
}
40364038
}
40374039
}
40384040

4039-
return response;
4041+
return messageSent;
40404042
} catch (error) {
40414043
this.logger.error(error);
40424044
throw error;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2199,7 +2199,7 @@ export class ChatwootService {
21992199
}
22002200
}
22012201

2202-
if (event === 'messages.edit') {
2202+
if (event === 'messages.edit' || event === 'send.message.update') {
22032203
const editedText = `${
22042204
body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text
22052205
}\n\n_\`${i18next.t('cw.message.edited')}.\`_`;

src/api/integrations/event/event.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export class EventController {
132132
'MESSAGES_UPDATE',
133133
'MESSAGES_DELETE',
134134
'SEND_MESSAGE',
135+
'SEND_MESSAGE_UPDATE',
135136
'CONTACTS_SET',
136137
'CONTACTS_UPSERT',
137138
'CONTACTS_UPDATE',

src/api/types/wa.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export enum Events {
1515
MESSAGES_UPDATE = 'messages.update',
1616
MESSAGES_DELETE = 'messages.delete',
1717
SEND_MESSAGE = 'send.message',
18+
SEND_MESSAGE_UPDATE = 'send.message.update',
1819
CONTACTS_SET = 'contacts.set',
1920
CONTACTS_UPSERT = 'contacts.upsert',
2021
CONTACTS_UPDATE = 'contacts.update',

src/config/env.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export type EventsRabbitmq = {
7272
MESSAGES_UPDATE: boolean;
7373
MESSAGES_DELETE: boolean;
7474
SEND_MESSAGE: boolean;
75+
SEND_MESSAGE_UPDATE: boolean;
7576
CONTACTS_SET: boolean;
7677
CONTACTS_UPDATE: boolean;
7778
CONTACTS_UPSERT: boolean;
@@ -140,6 +141,7 @@ export type EventsWebhook = {
140141
MESSAGES_UPDATE: boolean;
141142
MESSAGES_DELETE: boolean;
142143
SEND_MESSAGE: boolean;
144+
SEND_MESSAGE_UPDATE: boolean;
143145
CONTACTS_SET: boolean;
144146
CONTACTS_UPDATE: boolean;
145147
CONTACTS_UPSERT: boolean;
@@ -172,6 +174,7 @@ export type EventsPusher = {
172174
MESSAGES_UPDATE: boolean;
173175
MESSAGES_DELETE: boolean;
174176
SEND_MESSAGE: boolean;
177+
SEND_MESSAGE_UPDATE: boolean;
175178
CONTACTS_SET: boolean;
176179
CONTACTS_UPDATE: boolean;
177180
CONTACTS_UPSERT: boolean;
@@ -380,6 +383,7 @@ export class ConfigService {
380383
MESSAGES_UPDATE: process.env?.RABBITMQ_EVENTS_MESSAGES_UPDATE === 'true',
381384
MESSAGES_DELETE: process.env?.RABBITMQ_EVENTS_MESSAGES_DELETE === 'true',
382385
SEND_MESSAGE: process.env?.RABBITMQ_EVENTS_SEND_MESSAGE === 'true',
386+
SEND_MESSAGE_UPDATE: process.env?.RABBITMQ_EVENTS_SEND_MESSAGE_UPDATE === 'true',
383387
CONTACTS_SET: process.env?.RABBITMQ_EVENTS_CONTACTS_SET === 'true',
384388
CONTACTS_UPDATE: process.env?.RABBITMQ_EVENTS_CONTACTS_UPDATE === 'true',
385389
CONTACTS_UPSERT: process.env?.RABBITMQ_EVENTS_CONTACTS_UPSERT === 'true',
@@ -416,6 +420,7 @@ export class ConfigService {
416420
MESSAGES_UPDATE: process.env?.NATS_EVENTS_MESSAGES_UPDATE === 'true',
417421
MESSAGES_DELETE: process.env?.NATS_EVENTS_MESSAGES_DELETE === 'true',
418422
SEND_MESSAGE: process.env?.NATS_EVENTS_SEND_MESSAGE === 'true',
423+
SEND_MESSAGE_UPDATE: process.env?.NATS_EVENTS_SEND_MESSAGE_UPDATE === 'true',
419424
CONTACTS_SET: process.env?.NATS_EVENTS_CONTACTS_SET === 'true',
420425
CONTACTS_UPDATE: process.env?.NATS_EVENTS_CONTACTS_UPDATE === 'true',
421426
CONTACTS_UPSERT: process.env?.NATS_EVENTS_CONTACTS_UPSERT === 'true',
@@ -467,6 +472,7 @@ export class ConfigService {
467472
MESSAGES_UPDATE: process.env?.PUSHER_EVENTS_MESSAGES_UPDATE === 'true',
468473
MESSAGES_DELETE: process.env?.PUSHER_EVENTS_MESSAGES_DELETE === 'true',
469474
SEND_MESSAGE: process.env?.PUSHER_EVENTS_SEND_MESSAGE === 'true',
475+
SEND_MESSAGE_UPDATE: process.env?.PUSHER_EVENTS_SEND_MESSAGE_UPDATE === 'true',
470476
CONTACTS_SET: process.env?.PUSHER_EVENTS_CONTACTS_SET === 'true',
471477
CONTACTS_UPDATE: process.env?.PUSHER_EVENTS_CONTACTS_UPDATE === 'true',
472478
CONTACTS_UPSERT: process.env?.PUSHER_EVENTS_CONTACTS_UPSERT === 'true',
@@ -523,6 +529,7 @@ export class ConfigService {
523529
MESSAGES_UPDATE: process.env?.WEBHOOK_EVENTS_MESSAGES_UPDATE === 'true',
524530
MESSAGES_DELETE: process.env?.WEBHOOK_EVENTS_MESSAGES_DELETE === 'true',
525531
SEND_MESSAGE: process.env?.WEBHOOK_EVENTS_SEND_MESSAGE === 'true',
532+
SEND_MESSAGE_UPDATE: process.env?.WEBHOOK_EVENTS_SEND_MESSAGE_UPDATE === 'true',
526533
CONTACTS_SET: process.env?.WEBHOOK_EVENTS_CONTACTS_SET === 'true',
527534
CONTACTS_UPDATE: process.env?.WEBHOOK_EVENTS_CONTACTS_UPDATE === 'true',
528535
CONTACTS_UPSERT: process.env?.WEBHOOK_EVENTS_CONTACTS_UPSERT === 'true',

src/validate/instance.schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const instanceSchema: JSONSchema7 = {
6868
'MESSAGES_UPDATE',
6969
'MESSAGES_DELETE',
7070
'SEND_MESSAGE',
71+
'SEND_MESSAGE_UPDATE',
7172
'CONTACTS_SET',
7273
'CONTACTS_UPSERT',
7374
'CONTACTS_UPDATE',
@@ -104,6 +105,7 @@ export const instanceSchema: JSONSchema7 = {
104105
'MESSAGES_UPDATE',
105106
'MESSAGES_DELETE',
106107
'SEND_MESSAGE',
108+
'SEND_MESSAGE_UPDATE',
107109
'CONTACTS_SET',
108110
'CONTACTS_UPSERT',
109111
'CONTACTS_UPDATE',
@@ -140,6 +142,7 @@ export const instanceSchema: JSONSchema7 = {
140142
'MESSAGES_UPDATE',
141143
'MESSAGES_DELETE',
142144
'SEND_MESSAGE',
145+
'SEND_MESSAGE_UPDATE',
143146
'CONTACTS_SET',
144147
'CONTACTS_UPSERT',
145148
'CONTACTS_UPDATE',
@@ -176,6 +179,7 @@ export const instanceSchema: JSONSchema7 = {
176179
'MESSAGES_UPDATE',
177180
'MESSAGES_DELETE',
178181
'SEND_MESSAGE',
182+
'SEND_MESSAGE_UPDATE',
179183
'CONTACTS_SET',
180184
'CONTACTS_UPSERT',
181185
'CONTACTS_UPDATE',

0 commit comments

Comments
 (0)