From 0e50da324b9b0c8f3effa6206a2007fff116599b Mon Sep 17 00:00:00 2001 From: Amilton Morais Date: Wed, 24 Jan 2024 13:38:29 -0300 Subject: [PATCH] Implemented a function to synchronize message deletions on WhatsApp, automatically reflecting in Chatwoot A new variable has been created, and a function has been implemented to manage the deletion of messages on WhatsApp. Now, when deleting a message for everyone on WhatsApp, the same action will be automatically performed on Chatwoot, ensuring consistency across platforms --- src/config/env.config.ts | 5 +++ src/dev-env.yml | 4 ++ src/whatsapp/models/message.model.ts | 4 +- src/whatsapp/services/chatwoot.service.ts | 54 ++++++++++++----------- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index fde4a073..25dd72a3 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -147,6 +147,7 @@ export type Webhook = { GLOBAL?: GlobalWebhook; EVENTS: EventsWebhook }; export type ConfigSessionPhone = { CLIENT: string; NAME: string }; export type QrCode = { LIMIT: number; COLOR: string }; export type Typebot = { API_VERSION: string; KEEP_OPEN: boolean }; +export type ChatWoot = { MESSAGE_DELETE: boolean }; export type CacheConf = { REDIS: CacheConfRedis; LOCAL: CacheConfLocal }; export type Production = boolean; @@ -167,6 +168,7 @@ export interface Env { CONFIG_SESSION_PHONE: ConfigSessionPhone; QRCODE: QrCode; TYPEBOT: Typebot; + CHATWOOT: ChatWoot; CACHE: CacheConf; AUTHENTICATION: Auth; PRODUCTION?: Production; @@ -330,6 +332,9 @@ export class ConfigService { API_VERSION: process.env?.TYPEBOT_API_VERSION || 'old', KEEP_OPEN: process.env.TYPEBOT_KEEP_OPEN === 'true', }, + CHATWOOT: { + MESSAGE_DELETE: process.env.CHATWOOT_MESSAGE_DELETE === 'false', + }, CACHE: { REDIS: { ENABLED: process.env?.CACHE_REDIS_ENABLED === 'true', diff --git a/src/dev-env.yml b/src/dev-env.yml index 42438aff..c4a9d909 100644 --- a/src/dev-env.yml +++ b/src/dev-env.yml @@ -153,6 +153,10 @@ TYPEBOT: API_VERSION: 'old' # old | latest KEEP_OPEN: false +# If you leave this option as false, when deleting the message for everyone on WhatsApp, it will not be deleted on Chatwoot. +CHATWOOT: + MESSAGE_DELETE: true # false | true + # Cache to optimize application performance CACHE: REDIS: diff --git a/src/whatsapp/models/message.model.ts b/src/whatsapp/models/message.model.ts index 9c7ac9dc..4deb0f0f 100644 --- a/src/whatsapp/models/message.model.ts +++ b/src/whatsapp/models/message.model.ts @@ -26,7 +26,7 @@ export class MessageRaw { messageType?: string; messageTimestamp?: number | Long.Long; owner: string; - source?: 'android' | 'web' | 'ios'; + source?: 'android' | 'web' | 'ios' | 'ios' | 'unknown' | 'desktop'; source_id?: string; source_reply_id?: string; chatwoot?: ChatwootMessage; @@ -45,7 +45,7 @@ const messageSchema = new Schema({ participant: { type: String }, messageType: { type: String }, message: { type: Object }, - source: { type: String, minlength: 3, enum: ['android', 'web', 'ios'] }, + source: { type: String, minlength: 3, enum: ['android', 'web', 'ios','unknown','desktop' ] }, messageTimestamp: { type: Number, required: true }, owner: { type: String, required: true, minlength: 1 }, chatwoot: { diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index ef70cf63..3c4c011c 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -7,7 +7,7 @@ import Jimp from 'jimp'; import mimeTypes from 'mime-types'; import path from 'path'; -import { ConfigService, HttpServer } from '../../config/env.config'; +import { ConfigService, HttpServer, ChatWoot} from '../../config/env.config'; import { Logger } from '../../config/logger.config'; import { ICache } from '../abstract/abstract.cache'; import { ChatwootDto } from '../dto/chatwoot.dto'; @@ -1894,33 +1894,37 @@ export class ChatwootService { } if (event === Events.MESSAGES_DELETE) { - this.logger.verbose('deleting message from instance: ' + instance.instanceName); + + const chatwootDelete = this.configService.get('CHATWOOT').MESSAGE_DELETE + if (chatwootDelete === true) { + this.logger.verbose('deleting message from instance: ' + instance.instanceName); - if (!body?.key?.id) { - this.logger.warn('message id not found'); - return; - } + if (!body?.key?.id) { + this.logger.warn('message id not found'); + return; + } - const message = await this.getMessageByKeyId(instance, body.key.id); - if (message?.chatwoot?.messageId && message?.chatwoot?.conversationId) { - this.logger.verbose('deleting message in repository. Message id: ' + body.key.id); - this.repository.message.delete({ - where: { - key: { - id: body.key.id, - }, - owner: instance.instanceName, - }, - }); + const message = await this.getMessageByKeyId(instance, body.key.id); + if (message?.chatwoot?.messageId && message?.chatwoot?.conversationId) { + this.logger.verbose('deleting message in repository. Message id: ' + body.key.id); + this.repository.message.delete({ + where: { + key: { + id: body.key.id, + }, + owner: instance.instanceName, + }, + }); - this.logger.verbose('deleting message in chatwoot. Message id: ' + body.key.id); - return await client.messages.delete({ - accountId: this.provider.account_id, - conversationId: message.chatwoot.conversationId, - messageId: message.chatwoot.messageId, - }); - } - } + this.logger.verbose('deleting message in chatwoot. Message id: ' + body.key.id); + return await client.messages.delete({ + accountId: this.provider.account_id, + conversationId: message.chatwoot.conversationId, + messageId: message.chatwoot.messageId, + }); + } + } + } if (event === 'messages.read') { this.logger.verbose('read message from instance: ' + instance.instanceName);