Skip to content

Commit fc30bb9

Browse files
committed
feat: Added configuration of events by webhook of instances
1 parent 0f360d3 commit fc30bb9

8 files changed

Lines changed: 81 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Route to update group subject
1111
* Route to update group description
1212
* Route to accept invite code
13+
* Added configuration of events by webhook of instances
1314

1415
### Fixed
1516

src/validate/validate.schema.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@ export const instanceNameSchema: JSONSchema7 = {
2727
properties: {
2828
instanceName: { type: 'string' },
2929
webhook: { type: 'string' },
30+
events: {
31+
type: 'array',
32+
minItems: 1,
33+
items: {
34+
type: 'string',
35+
enum: [
36+
'APPLICATION_STARTUP',
37+
'QRCODE_UPDATED',
38+
'MESSAGES_SET',
39+
'MESSAGES_UPSERT',
40+
'MESSAGES_UPDATE',
41+
'SEND_MESSAGE',
42+
'CONTACTS_SET',
43+
'CONTACTS_UPSERT',
44+
'CONTACTS_UPDATE',
45+
'PRESENCE_UPDATE',
46+
'CHATS_SET',
47+
'CHATS_UPSERT',
48+
'CHATS_UPDATE',
49+
'CHATS_DELETE',
50+
'GROUPS_UPSERT',
51+
'GROUP_UPDATE',
52+
'GROUP_PARTICIPANTS_UPDATE',
53+
'CONNECTION_UPDATE',
54+
'NEW_JWT_TOKEN',
55+
],
56+
},
57+
},
3058
},
3159
...isNotEmpty('instanceName'),
3260
};
@@ -720,6 +748,34 @@ export const webhookSchema: JSONSchema7 = {
720748
properties: {
721749
url: { type: 'string' },
722750
enabled: { type: 'boolean', enum: [true, false] },
751+
events: {
752+
type: 'array',
753+
minItems: 1,
754+
items: {
755+
type: 'string',
756+
enum: [
757+
'APPLICATION_STARTUP',
758+
'QRCODE_UPDATED',
759+
'MESSAGES_SET',
760+
'MESSAGES_UPSERT',
761+
'MESSAGES_UPDATE',
762+
'SEND_MESSAGE',
763+
'CONTACTS_SET',
764+
'CONTACTS_UPSERT',
765+
'CONTACTS_UPDATE',
766+
'PRESENCE_UPDATE',
767+
'CHATS_SET',
768+
'CHATS_UPSERT',
769+
'CHATS_UPDATE',
770+
'CHATS_DELETE',
771+
'GROUPS_UPSERT',
772+
'GROUP_UPDATE',
773+
'GROUP_PARTICIPANTS_UPDATE',
774+
'CONNECTION_UPDATE',
775+
'NEW_JWT_TOKEN',
776+
],
777+
},
778+
},
723779
},
724780
required: ['url', 'enabled'],
725781
...isNotEmpty('url'),

src/whatsapp/controllers/instance.controller.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ export class InstanceController {
2222

2323
private readonly logger = new Logger(InstanceController.name);
2424

25-
public async createInstance({ instanceName, webhook }: InstanceDto) {
26-
//verifica se modo da instancia é container
25+
public async createInstance({ instanceName, webhook, events }: InstanceDto) {
2726
const mode = this.configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
2827

2928
if (mode === 'container') {
30-
//verifica se ja existe uma instancia criada com qualquer nome
3129
if (Object.keys(this.waMonitor.waInstances).length > 0) {
3230
throw new BadRequestException([
3331
'Instance already created',
@@ -50,7 +48,7 @@ export class InstanceController {
5048

5149
if (webhook) {
5250
try {
53-
this.webhookService.create(instance, { enabled: true, url: webhook });
51+
this.webhookService.create(instance, { enabled: true, url: webhook, events });
5452
} catch (error) {
5553
this.logger.log(error);
5654
}

src/whatsapp/dto/instance.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export class InstanceDto {
22
instanceName: string;
33
webhook?: string;
4+
events?: string[];
45
}

src/whatsapp/dto/webhook.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export class WebhookDto {
22
enabled?: boolean;
33
url?: string;
4+
events?: string[];
45
}

src/whatsapp/models/webhook.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ export class WebhookRaw {
55
_id?: string;
66
url?: string;
77
enabled?: boolean;
8+
events?: string[];
89
}
910

1011
const webhookSchema = new Schema<WebhookRaw>({
1112
_id: { type: String, _id: true },
1213
url: { type: String, required: true },
1314
enabled: { type: Boolean, required: true },
15+
events: { type: [String], required: true },
1416
});
1517

1618
export const WebhookModel = dbserver?.model(WebhookRaw.name, webhookSchema, 'webhook');

src/whatsapp/services/whatsapp.service.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export class WAStartupService {
200200
const data = await this.repository.webhook.find(this.instanceName);
201201
this.localWebhook.url = data?.url;
202202
this.localWebhook.enabled = data?.enabled;
203+
this.localWebhook.events = data?.events;
203204
}
204205

205206
public async setWebhook(data: WebhookRaw) {
@@ -212,12 +213,13 @@ export class WAStartupService {
212213
}
213214

214215
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
215-
const webhook = this.configService.get<Webhook>('WEBHOOK');
216+
const webhookGlobal = this.configService.get<Webhook>('WEBHOOK');
217+
const webhookLocal = this.localWebhook.events;
216218
const we = event.replace(/[\.-]/gm, '_').toUpperCase();
217219
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
218220
const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE;
219221

220-
if (webhook.EVENTS[we]) {
222+
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
221223
if (local && instance.MODE !== 'container') {
222224
const { WEBHOOK_BY_EVENTS } = instance;
223225

@@ -229,6 +231,15 @@ export class WAStartupService {
229231
baseURL = this.localWebhook.url;
230232
}
231233

234+
this.logger.log({
235+
local: WAStartupService.name + '.sendDataWebhook-local',
236+
url: baseURL,
237+
event,
238+
instance: this.instance.name,
239+
data,
240+
destination: this.localWebhook.url,
241+
});
242+
232243
try {
233244
if (this.localWebhook.enabled && isURL(this.localWebhook.url)) {
234245
const httpService = axios.create({ baseURL });
@@ -253,12 +264,13 @@ export class WAStartupService {
253264
});
254265
}
255266
}
256-
267+
}
268+
if (webhookGlobal.EVENTS[we]) {
257269
const globalWebhook = this.configService.get<Webhook>('WEBHOOK').GLOBAL;
258270

259271
let globalURL;
260272

261-
if (webhook.GLOBAL.WEBHOOK_BY_EVENTS) {
273+
if (webhookGlobal.GLOBAL.WEBHOOK_BY_EVENTS) {
262274
globalURL = `${globalWebhook.URL}/${transformedWe}`;
263275
} else {
264276
globalURL = globalWebhook.URL;
@@ -273,6 +285,7 @@ export class WAStartupService {
273285
}
274286

275287
this.logger.log({
288+
local: WAStartupService.name + '.sendDataWebhook-global',
276289
url: globalURL,
277290
event,
278291
instance: this.instance.name,

src/whatsapp/types/wa.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export declare namespace wa {
3434
profilePictureUrl?: string;
3535
};
3636

37-
export type LocalWebHook = { enabled?: boolean; url?: string };
37+
export type LocalWebHook = { enabled?: boolean; url?: string; events?: string[] };
3838

3939
export type StateConnection = {
4040
instance?: string;

0 commit comments

Comments
 (0)