Skip to content

Commit f0d40ee

Browse files
committed
New global mode for rabbitmq events
1 parent 68dcc1c commit f0d40ee

File tree

10 files changed

+298
-51
lines changed

10 files changed

+298
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Feature
44

55
* Now in the manager, when logging in with the client's apikey, the listing only shows the instance corresponding to the provided apikey (only with MongoDB)
6+
* New global mode for rabbitmq events
67

78
# 1.7.5 (2024-05-21 08:50)
89

Docker/.env.example

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,33 @@ DATABASE_SAVE_DATA_CONTACTS=false
4747
DATABASE_SAVE_DATA_CHATS=false
4848

4949
RABBITMQ_ENABLED=false
50-
RABBITMQ_RABBITMQ_MODE=global
51-
RABBITMQ_EXCHANGE_NAME=evolution_exchange
5250
RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672
51+
RABBITMQ_EXCHANGE_NAME=evolution_exchange
52+
RABBITMQ_GLOBAL_ENABLED=false
53+
RABBITMQ_EVENTS_APPLICATION_STARTUP=false
54+
RABBITMQ_EVENTS_QRCODE_UPDATED=true
55+
RABBITMQ_EVENTS_MESSAGES_SET=true
56+
RABBITMQ_EVENTS_MESSAGES_UPSERT=true
57+
RABBITMQ_EVENTS_MESSAGES_UPDATE=true
58+
RABBITMQ_EVENTS_MESSAGES_DELETE=true
59+
RABBITMQ_EVENTS_SEND_MESSAGE=true
60+
RABBITMQ_EVENTS_CONTACTS_SET=true
61+
RABBITMQ_EVENTS_CONTACTS_UPSERT=true
62+
RABBITMQ_EVENTS_CONTACTS_UPDATE=true
63+
RABBITMQ_EVENTS_PRESENCE_UPDATE=true
64+
RABBITMQ_EVENTS_CHATS_SET=true
65+
RABBITMQ_EVENTS_CHATS_UPSERT=true
66+
RABBITMQ_EVENTS_CHATS_UPDATE=true
67+
RABBITMQ_EVENTS_CHATS_DELETE=true
68+
RABBITMQ_EVENTS_GROUPS_UPSERT=true
69+
RABBITMQ_EVENTS_GROUPS_UPDATE=true
70+
RABBITMQ_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
71+
RABBITMQ_EVENTS_CONNECTION_UPDATE=true
72+
RABBITMQ_EVENTS_LABELS_EDIT=true
73+
RABBITMQ_EVENTS_LABELS_ASSOCIATION=true
74+
RABBITMQ_EVENTS_CALL=true
75+
RABBITMQ_EVENTS_TYPEBOT_START=false
76+
RABBITMQ_EVENTS_TYPEBOT_CHANGE_STATUS=false
5377

5478
WEBSOCKET_ENABLED=false
5579
WEBSOCKET_GLOBAL_EVENTS=false

Dockerfile

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM node:20.7.0-alpine AS builder
22

3-
LABEL version="1.7.5" description="Api to control whatsapp features through http requests."
3+
LABEL version="1.8.0" description="Api to control whatsapp features through http requests."
44
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
55
LABEL contact="contato@agenciadgcode.com"
66

@@ -59,9 +59,35 @@ ENV DATABASE_SAVE_DATA_CONTACTS=false
5959
ENV DATABASE_SAVE_DATA_CHATS=false
6060

6161
ENV RABBITMQ_ENABLED=false
62-
ENV RABBITMQ_MODE=global
63-
ENV RABBITMQ_EXCHANGE_NAME=evolution_exchange
6462
ENV RABBITMQ_URI=amqp://guest:guest@rabbitmq:5672
63+
ENV RABBITMQ_EXCHANGE_NAME=evolution_exchange
64+
ENV RABBITMQ_GLOBAL_ENABLED=false
65+
ENV RABBITMQ_EVENTS_APPLICATION_STARTUP=false
66+
ENV RABBITMQ_EVENTS_INSTANCE_CREATE=false
67+
ENV RABBITMQ_EVENTS_INSTANCE_DELETE=false
68+
ENV RABBITMQ_EVENTS_QRCODE_UPDATED=true
69+
ENV RABBITMQ_EVENTS_MESSAGES_SET=true
70+
ENV RABBITMQ_EVENTS_MESSAGES_UPSERT=true
71+
ENV RABBITMQ_EVENTS_MESSAGES_UPDATE=true
72+
ENV RABBITMQ_EVENTS_MESSAGES_DELETE=true
73+
ENV RABBITMQ_EVENTS_SEND_MESSAGE=true
74+
ENV RABBITMQ_EVENTS_CONTACTS_SET=true
75+
ENV RABBITMQ_EVENTS_CONTACTS_UPSERT=true
76+
ENV RABBITMQ_EVENTS_CONTACTS_UPDATE=true
77+
ENV RABBITMQ_EVENTS_PRESENCE_UPDATE=true
78+
ENV RABBITMQ_EVENTS_CHATS_SET=true
79+
ENV RABBITMQ_EVENTS_CHATS_UPSERT=true
80+
ENV RABBITMQ_EVENTS_CHATS_UPDATE=true
81+
ENV RABBITMQ_EVENTS_CHATS_DELETE=true
82+
ENV RABBITMQ_EVENTS_GROUPS_UPSERT=true
83+
ENV RABBITMQ_EVENTS_GROUPS_UPDATE=true
84+
ENV RABBITMQ_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
85+
ENV RABBITMQ_EVENTS_CONNECTION_UPDATE=true
86+
ENV RABBITMQ_EVENTS_LABELS_EDIT=true
87+
ENV RABBITMQ_EVENTS_LABELS_ASSOCIATION=true
88+
ENV RABBITMQ_EVENTS_CALL=true
89+
ENV RABBITMQ_EVENTS_TYPEBOT_START=false
90+
ENV RABBITMQ_EVENTS_TYPEBOT_CHANGE_STATUS=false
6591

6692
ENV WEBSOCKET_ENABLED=false
6793
ENV WEBSOCKET_GLOBAL_EVENTS=false

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "evolution-api",
3-
"version": "1.7.5",
3+
"version": "1.8.0",
44
"description": "Rest api for communication with WhatsApp",
55
"main": "./dist/src/main.js",
66
"scripts": {

src/api/integrations/rabbitmq/libs/amqp.server.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,41 @@ export const getAMQP = (): amqp.Channel | null => {
4242
return amqpChannel;
4343
};
4444

45+
export const initGlobalQueues = () => {
46+
logger.info('Initializing global queues');
47+
const events = configService.get<Rabbitmq>('RABBITMQ').EVENTS;
48+
49+
if (!events) {
50+
logger.warn('No events to initialize on AMQP');
51+
return;
52+
}
53+
54+
const eventKeys = Object.keys(events);
55+
56+
eventKeys.forEach((event) => {
57+
if (events[event] === false) return;
58+
59+
const queueName = `${event.replace(/_/g, '.').toLowerCase()}`;
60+
const amqp = getAMQP();
61+
const exchangeName = 'evolution_exchange';
62+
63+
amqp.assertExchange(exchangeName, 'topic', {
64+
durable: true,
65+
autoDelete: false,
66+
});
67+
68+
amqp.assertQueue(queueName, {
69+
durable: true,
70+
autoDelete: false,
71+
arguments: {
72+
'x-queue-type': 'quorum',
73+
},
74+
});
75+
76+
amqp.bindQueue(queueName, exchangeName, event);
77+
});
78+
};
79+
4580
export const initQueues = (instanceName: string, events: string[]) => {
4681
if (!events || !events.length) return;
4782

src/api/services/channel.service.ts

Lines changed: 108 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
Database,
1414
HttpServer,
1515
Log,
16+
Rabbitmq,
1617
Sqs,
1718
Webhook,
1819
Websocket,
@@ -688,6 +689,9 @@ export class ChannelStartupService {
688689
const rabbitmqLocal = this.localRabbitmq.events;
689690
const sqsLocal = this.localSqs.events;
690691
const serverUrl = this.configService.get<HttpServer>('SERVER').URL;
692+
const rabbitmqEnabled = this.configService.get<Rabbitmq>('RABBITMQ').ENABLED;
693+
const rabbitmqGlobal = this.configService.get<Rabbitmq>('RABBITMQ').GLOBAL_ENABLED;
694+
const rabbitmqEvents = this.configService.get<Rabbitmq>('RABBITMQ').EVENTS;
691695
const we = event.replace(/[.-]/gm, '_').toUpperCase();
692696
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
693697
const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds
@@ -698,67 +702,134 @@ export class ChannelStartupService {
698702
const tokenStore = await this.repository.auth.find(this.instanceName);
699703
const instanceApikey = tokenStore?.apikey || 'Apikey not found';
700704

701-
if (this.localRabbitmq.enabled) {
705+
if (rabbitmqEnabled) {
702706
const amqp = getAMQP();
703-
704-
if (amqp) {
707+
if (this.localRabbitmq.enabled && amqp) {
705708
if (Array.isArray(rabbitmqLocal) && rabbitmqLocal.includes(we)) {
706709
const exchangeName = this.instanceName ?? 'evolution_exchange';
707710

708-
// await amqp.assertExchange(exchangeName, 'topic', {
709-
// durable: true,
710-
// autoDelete: false,
711-
// });
711+
let retry = 0;
712712

713-
await this.assertExchangeAsync(amqp, exchangeName, 'topic', {
714-
durable: true,
715-
autoDelete: false,
716-
});
713+
while (retry < 3) {
714+
try {
715+
await amqp.assertExchange(exchangeName, 'topic', {
716+
durable: true,
717+
autoDelete: false,
718+
});
717719

718-
const queueName = `${this.instanceName}.${event}`;
720+
const queueName = `${this.instanceName}.${event}`;
719721

720-
await amqp.assertQueue(queueName, {
721-
durable: true,
722-
autoDelete: false,
723-
arguments: {
724-
'x-queue-type': 'quorum',
725-
},
726-
});
722+
await amqp.assertQueue(queueName, {
723+
durable: true,
724+
autoDelete: false,
725+
arguments: {
726+
'x-queue-type': 'quorum',
727+
},
728+
});
727729

728-
await amqp.bindQueue(queueName, exchangeName, event);
730+
await amqp.bindQueue(queueName, exchangeName, event);
729731

730-
const message = {
731-
event,
732-
instance: this.instance.name,
733-
data,
734-
server_url: serverUrl,
735-
date_time: now,
736-
sender: this.wuid,
737-
};
732+
const message = {
733+
event,
734+
instance: this.instance.name,
735+
data,
736+
server_url: serverUrl,
737+
date_time: now,
738+
sender: this.wuid,
739+
};
738740

739-
if (expose && instanceApikey) {
740-
message['apikey'] = instanceApikey;
741+
if (expose && instanceApikey) {
742+
message['apikey'] = instanceApikey;
743+
}
744+
745+
await amqp.publish(exchangeName, event, Buffer.from(JSON.stringify(message)));
746+
747+
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
748+
const logData = {
749+
local: ChannelStartupService.name + '.sendData-RabbitMQ',
750+
event,
751+
instance: this.instance.name,
752+
data,
753+
server_url: serverUrl,
754+
apikey: (expose && instanceApikey) || null,
755+
date_time: now,
756+
sender: this.wuid,
757+
};
758+
759+
if (expose && instanceApikey) {
760+
logData['apikey'] = instanceApikey;
761+
}
762+
763+
this.logger.log(logData);
764+
}
765+
break;
766+
} catch (error) {
767+
retry++;
768+
}
741769
}
770+
}
771+
}
772+
773+
if (rabbitmqGlobal && rabbitmqEvents[we] && amqp) {
774+
const exchangeName = 'evolution_exchange';
775+
776+
let retry = 0;
777+
778+
while (retry < 3) {
779+
try {
780+
await amqp.assertExchange(exchangeName, 'topic', {
781+
durable: true,
782+
autoDelete: false,
783+
});
784+
785+
const queueName = transformedWe;
742786

743-
await amqp.publish(exchangeName, event, Buffer.from(JSON.stringify(message)));
787+
await amqp.assertQueue(queueName, {
788+
durable: true,
789+
autoDelete: false,
790+
arguments: {
791+
'x-queue-type': 'quorum',
792+
},
793+
});
744794

745-
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
746-
const logData = {
747-
local: ChannelStartupService.name + '.sendData-RabbitMQ',
795+
await amqp.bindQueue(queueName, exchangeName, event);
796+
797+
const message = {
748798
event,
749799
instance: this.instance.name,
750800
data,
751801
server_url: serverUrl,
752-
apikey: (expose && instanceApikey) || null,
753802
date_time: now,
754803
sender: this.wuid,
755804
};
756805

757806
if (expose && instanceApikey) {
758-
logData['apikey'] = instanceApikey;
807+
message['apikey'] = instanceApikey;
808+
}
809+
await amqp.publish(exchangeName, event, Buffer.from(JSON.stringify(message)));
810+
811+
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
812+
const logData = {
813+
local: ChannelStartupService.name + '.sendData-RabbitMQ-Global',
814+
event,
815+
instance: this.instance.name,
816+
data,
817+
server_url: serverUrl,
818+
apikey: (expose && instanceApikey) || null,
819+
date_time: now,
820+
sender: this.wuid,
821+
};
822+
823+
if (expose && instanceApikey) {
824+
logData['apikey'] = instanceApikey;
825+
}
826+
827+
this.logger.log(logData);
759828
}
760829

761-
this.logger.log(logData);
830+
break;
831+
} catch (error) {
832+
retry++;
762833
}
763834
}
764835
}

0 commit comments

Comments
 (0)