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 description
* Route to accept invite code
* Added configuration of events by webhook of instances
### Fixed

View File

@ -27,6 +27,34 @@ export const instanceNameSchema: JSONSchema7 = {
properties: {
instanceName: { 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'),
};
@ -720,6 +748,34 @@ export const webhookSchema: JSONSchema7 = {
properties: {
url: { type: 'string' },
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'],
...isNotEmpty('url'),

View File

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

View File

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

View File

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

View File

@ -5,12 +5,14 @@ export class WebhookRaw {
_id?: string;
url?: string;
enabled?: boolean;
events?: string[];
}
const webhookSchema = new Schema<WebhookRaw>({
_id: { type: String, _id: true },
url: { type: String, required: true },
enabled: { type: Boolean, required: true },
events: { type: [String], required: true },
});
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);
this.localWebhook.url = data?.url;
this.localWebhook.enabled = data?.enabled;
this.localWebhook.events = data?.events;
}
public async setWebhook(data: WebhookRaw) {
@ -212,12 +213,13 @@ export class WAStartupService {
}
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 transformedWe = we.replace(/_/gm, '-').toLowerCase();
const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE;
if (webhook.EVENTS[we]) {
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
if (local && instance.MODE !== 'container') {
const { WEBHOOK_BY_EVENTS } = instance;
@ -229,6 +231,15 @@ export class WAStartupService {
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 {
if (this.localWebhook.enabled && isURL(this.localWebhook.url)) {
const httpService = axios.create({ baseURL });
@ -253,12 +264,13 @@ export class WAStartupService {
});
}
}
}
if (webhookGlobal.EVENTS[we]) {
const globalWebhook = this.configService.get<Webhook>('WEBHOOK').GLOBAL;
let globalURL;
if (webhook.GLOBAL.WEBHOOK_BY_EVENTS) {
if (webhookGlobal.GLOBAL.WEBHOOK_BY_EVENTS) {
globalURL = `${globalWebhook.URL}/${transformedWe}`;
} else {
globalURL = globalWebhook.URL;
@ -273,6 +285,7 @@ export class WAStartupService {
}
this.logger.log({
local: WAStartupService.name + '.sendDataWebhook-global',
url: globalURL,
event,
instance: this.instance.name,

View File

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