feat: Added configuration of events by webhook of instances

This commit is contained in:
Davidson Gomes 2023-06-12 14:40:26 -03:00
parent 0f360d34e8
commit fc30bb9852
8 changed files with 81 additions and 9 deletions

View File

@ -10,6 +10,7 @@
* Route to update group subject * Route to update group subject
* Route to update group description * Route to update group description
* Route to accept invite code * Route to accept invite code
* Added configuration of events by webhook of instances
### Fixed ### Fixed

View File

@ -27,6 +27,34 @@ export const instanceNameSchema: JSONSchema7 = {
properties: { properties: {
instanceName: { type: 'string' }, instanceName: { type: 'string' },
webhook: { type: 'string' }, webhook: { type: 'string' },
events: {
type: 'array',
minItems: 1,
items: {
type: 'string',
enum: [
'APPLICATION_STARTUP',
'QRCODE_UPDATED',
'MESSAGES_SET',
'MESSAGES_UPSERT',
'MESSAGES_UPDATE',
'SEND_MESSAGE',
'CONTACTS_SET',
'CONTACTS_UPSERT',
'CONTACTS_UPDATE',
'PRESENCE_UPDATE',
'CHATS_SET',
'CHATS_UPSERT',
'CHATS_UPDATE',
'CHATS_DELETE',
'GROUPS_UPSERT',
'GROUP_UPDATE',
'GROUP_PARTICIPANTS_UPDATE',
'CONNECTION_UPDATE',
'NEW_JWT_TOKEN',
],
},
},
}, },
...isNotEmpty('instanceName'), ...isNotEmpty('instanceName'),
}; };
@ -720,6 +748,34 @@ export const webhookSchema: JSONSchema7 = {
properties: { properties: {
url: { type: 'string' }, url: { type: 'string' },
enabled: { type: 'boolean', enum: [true, false] }, enabled: { type: 'boolean', enum: [true, false] },
events: {
type: 'array',
minItems: 1,
items: {
type: 'string',
enum: [
'APPLICATION_STARTUP',
'QRCODE_UPDATED',
'MESSAGES_SET',
'MESSAGES_UPSERT',
'MESSAGES_UPDATE',
'SEND_MESSAGE',
'CONTACTS_SET',
'CONTACTS_UPSERT',
'CONTACTS_UPDATE',
'PRESENCE_UPDATE',
'CHATS_SET',
'CHATS_UPSERT',
'CHATS_UPDATE',
'CHATS_DELETE',
'GROUPS_UPSERT',
'GROUP_UPDATE',
'GROUP_PARTICIPANTS_UPDATE',
'CONNECTION_UPDATE',
'NEW_JWT_TOKEN',
],
},
},
}, },
required: ['url', 'enabled'], required: ['url', 'enabled'],
...isNotEmpty('url'), ...isNotEmpty('url'),

View File

@ -22,12 +22,10 @@ export class InstanceController {
private readonly logger = new Logger(InstanceController.name); private readonly logger = new Logger(InstanceController.name);
public async createInstance({ instanceName, webhook }: InstanceDto) { public async createInstance({ instanceName, webhook, events }: InstanceDto) {
//verifica se modo da instancia é container
const mode = this.configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE; const mode = this.configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
if (mode === 'container') { if (mode === 'container') {
//verifica se ja existe uma instancia criada com qualquer nome
if (Object.keys(this.waMonitor.waInstances).length > 0) { if (Object.keys(this.waMonitor.waInstances).length > 0) {
throw new BadRequestException([ throw new BadRequestException([
'Instance already created', 'Instance already created',
@ -50,7 +48,7 @@ export class InstanceController {
if (webhook) { if (webhook) {
try { try {
this.webhookService.create(instance, { enabled: true, url: webhook }); this.webhookService.create(instance, { enabled: true, url: webhook, events });
} catch (error) { } catch (error) {
this.logger.log(error); this.logger.log(error);
} }

View File

@ -1,4 +1,5 @@
export class InstanceDto { export class InstanceDto {
instanceName: string; instanceName: string;
webhook?: string; webhook?: string;
events?: string[];
} }

View File

@ -1,4 +1,5 @@
export class WebhookDto { export class WebhookDto {
enabled?: boolean; enabled?: boolean;
url?: string; url?: string;
events?: string[];
} }

View File

@ -5,12 +5,14 @@ export class WebhookRaw {
_id?: string; _id?: string;
url?: string; url?: string;
enabled?: boolean; enabled?: boolean;
events?: string[];
} }
const webhookSchema = new Schema<WebhookRaw>({ const webhookSchema = new Schema<WebhookRaw>({
_id: { type: String, _id: true }, _id: { type: String, _id: true },
url: { type: String, required: true }, url: { type: String, required: true },
enabled: { type: Boolean, required: true }, enabled: { type: Boolean, required: true },
events: { type: [String], required: true },
}); });
export const WebhookModel = dbserver?.model(WebhookRaw.name, webhookSchema, 'webhook'); export const WebhookModel = dbserver?.model(WebhookRaw.name, webhookSchema, 'webhook');

View File

@ -200,6 +200,7 @@ export class WAStartupService {
const data = await this.repository.webhook.find(this.instanceName); const data = await this.repository.webhook.find(this.instanceName);
this.localWebhook.url = data?.url; this.localWebhook.url = data?.url;
this.localWebhook.enabled = data?.enabled; this.localWebhook.enabled = data?.enabled;
this.localWebhook.events = data?.events;
} }
public async setWebhook(data: WebhookRaw) { public async setWebhook(data: WebhookRaw) {
@ -212,12 +213,13 @@ export class WAStartupService {
} }
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) { public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
const webhook = this.configService.get<Webhook>('WEBHOOK'); const webhookGlobal = this.configService.get<Webhook>('WEBHOOK');
const webhookLocal = this.localWebhook.events;
const we = event.replace(/[\.-]/gm, '_').toUpperCase(); const we = event.replace(/[\.-]/gm, '_').toUpperCase();
const transformedWe = we.replace(/_/gm, '-').toLowerCase(); const transformedWe = we.replace(/_/gm, '-').toLowerCase();
const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE; const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE;
if (webhook.EVENTS[we]) { if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
if (local && instance.MODE !== 'container') { if (local && instance.MODE !== 'container') {
const { WEBHOOK_BY_EVENTS } = instance; const { WEBHOOK_BY_EVENTS } = instance;
@ -229,6 +231,15 @@ export class WAStartupService {
baseURL = this.localWebhook.url; baseURL = this.localWebhook.url;
} }
this.logger.log({
local: WAStartupService.name + '.sendDataWebhook-local',
url: baseURL,
event,
instance: this.instance.name,
data,
destination: this.localWebhook.url,
});
try { try {
if (this.localWebhook.enabled && isURL(this.localWebhook.url)) { if (this.localWebhook.enabled && isURL(this.localWebhook.url)) {
const httpService = axios.create({ baseURL }); const httpService = axios.create({ baseURL });
@ -253,12 +264,13 @@ export class WAStartupService {
}); });
} }
} }
}
if (webhookGlobal.EVENTS[we]) {
const globalWebhook = this.configService.get<Webhook>('WEBHOOK').GLOBAL; const globalWebhook = this.configService.get<Webhook>('WEBHOOK').GLOBAL;
let globalURL; let globalURL;
if (webhook.GLOBAL.WEBHOOK_BY_EVENTS) { if (webhookGlobal.GLOBAL.WEBHOOK_BY_EVENTS) {
globalURL = `${globalWebhook.URL}/${transformedWe}`; globalURL = `${globalWebhook.URL}/${transformedWe}`;
} else { } else {
globalURL = globalWebhook.URL; globalURL = globalWebhook.URL;
@ -273,6 +285,7 @@ export class WAStartupService {
} }
this.logger.log({ this.logger.log({
local: WAStartupService.name + '.sendDataWebhook-global',
url: globalURL, url: globalURL,
event, event,
instance: this.instance.name, instance: this.instance.name,

View File

@ -34,7 +34,7 @@ export declare namespace wa {
profilePictureUrl?: string; profilePictureUrl?: string;
}; };
export type LocalWebHook = { enabled?: boolean; url?: string }; export type LocalWebHook = { enabled?: boolean; url?: string; events?: string[] };
export type StateConnection = { export type StateConnection = {
instance?: string; instance?: string;