From 2f2f9520db599ab6dd6cf7461f167bf0e6eb2db8 Mon Sep 17 00:00:00 2001 From: allburov Date: Tue, 28 Mar 2023 15:36:35 +0700 Subject: [PATCH] [core] add session to webhook data fix #80 --- .../site/content/en/docs/how-to/receive-messages/index.md | 8 ++++++++ src/core/webhooks.core.ts | 6 +++--- src/structures/responses.dto.ts | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/site/content/en/docs/how-to/receive-messages/index.md b/docs/site/content/en/docs/how-to/receive-messages/index.md index 678d9bae..ec98f72e 100644 --- a/docs/site/content/en/docs/how-to/receive-messages/index.md +++ b/docs/site/content/en/docs/how-to/receive-messages/index.md @@ -27,6 +27,7 @@ On the URL that you set via `WHATSAPP_HOOK_URL` you receive JSON-data with follo ```json { "event": "message", + "session": "default", "payload": {} } ``` @@ -64,6 +65,7 @@ Incoming message (text/audio/files) ```json { "event": "message", + "session": "default", "payload": { "id": "true_11111111111@c.us_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "timestamp": 1667561485, @@ -126,6 +128,7 @@ Fired on all message creations, including your own. The payload is the same as f ```json { "event": "message.any", + "session": "default", "payload": {} } ``` @@ -135,6 +138,7 @@ Fired on all message creations, including your own. The payload is the same as f ```json { "event": "message.ack", + "session": "default", "payload": {} } ``` @@ -144,6 +148,7 @@ Fired on all message creations, including your own. The payload is the same as f ```json { "event": "state.change", + "session": "default", "payload": {} } ``` @@ -153,6 +158,7 @@ Fired on all message creations, including your own. The payload is the same as f ```json { "event": "group.join", + "session": "default", "payload": {} } ``` @@ -162,6 +168,7 @@ Fired on all message creations, including your own. The payload is the same as f ```json { "event": "group.left", + "session": "default", "payload": {} } ``` @@ -176,6 +183,7 @@ For example, you can get the webhook like this with `mediaUrl` value (we've skip ```json { "event": "message", + "session": "default", "payload": { "id": "true_11111111111@c.us_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "timestamp": 1667561485, diff --git a/src/core/webhooks.core.ts b/src/core/webhooks.core.ts index 6582f436..0e267f10 100644 --- a/src/core/webhooks.core.ts +++ b/src/core/webhooks.core.ts @@ -40,7 +40,7 @@ export class WebhookConductorCore implements WebhookConductor { this.log.log('Configuring webhooks...') for (const event of this.events) { try { - session.subscribe(event, (data: any) => this.callWebhook(event, data, this.url)) + session.subscribe(event, (data: any) => this.callWebhook(event, session, data, this.url)) } catch (error) { if (error instanceof NotImplementedByEngineError) { this.log.error(error) @@ -53,8 +53,8 @@ export class WebhookConductorCore implements WebhookConductor { this.log.log('Webhooks were configured.') } - public callWebhook(event, data: any, url) { - const json: WAWebhook = {event: event, payload: data} + public callWebhook(event, session: WhatsappSession, data: any, url) { + const json: WAWebhook = {event: event, session: session.name, payload: data} this.log.log(`Sending POST to ${url}...`) this.log.debug(`POST DATA: ${JSON.stringify(json)}`) this.post(json, url) diff --git a/src/structures/responses.dto.ts b/src/structures/responses.dto.ts index 85c857dc..6d80a2bf 100644 --- a/src/structures/responses.dto.ts +++ b/src/structures/responses.dto.ts @@ -133,6 +133,7 @@ export class WAMessage { export class WAWebhook { event: WAEvents + session: string // eslint-disable-next-line @typescript-eslint/ban-types payload: WAMessage | WAGroupNotification | object }