From 85106667a2fea4c9f4ece5ef4e0278a499fdade0 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sat, 15 Jun 2024 12:46:52 -0300 Subject: [PATCH] feat: update message sending logic Refactored sendMessage.controller.ts, chat.dto.ts, and sendMessage.dto.ts to improve message handling. Updated chatwoot.service.ts and sendMessage.router.ts for better integration with Chatwoot. Enhanced whatsapp.baileys.service.ts for more reliable WhatsApp communication. Adjusted chat.schema.ts and message.schema.ts for validation improvements. These changes enhance the overall messaging functionality and reliability. --- src/api/controllers/sendMessage.controller.ts | 5 ---- src/api/dto/chat.dto.ts | 2 +- src/api/dto/sendMessage.dto.ts | 5 ---- .../chatwoot/services/chatwoot.service.ts | 2 +- src/api/routes/sendMessage.router.ts | 12 --------- .../channels/whatsapp.baileys.service.ts | 27 +++---------------- src/validate/chat.schema.ts | 4 +-- src/validate/message.schema.ts | 13 --------- 8 files changed, 7 insertions(+), 63 deletions(-) diff --git a/src/api/controllers/sendMessage.controller.ts b/src/api/controllers/sendMessage.controller.ts index f6ce54c3d..8c005094a 100644 --- a/src/api/controllers/sendMessage.controller.ts +++ b/src/api/controllers/sendMessage.controller.ts @@ -3,7 +3,6 @@ import { isBase64, isURL } from 'class-validator'; import { BadRequestException } from '../../exceptions'; import { InstanceDto } from '../dto/instance.dto'; import { - FakeCallDto, SendAudioDto, SendButtonDto, SendContactDto, @@ -85,8 +84,4 @@ export class SendMessageController { public async sendStatus({ instanceName }: InstanceDto, data: SendStatusDto) { return await this.waMonitor.waInstances[instanceName].statusMessage(data); } - - public async fakeCall({ instanceName }: InstanceDto, data: FakeCallDto) { - return await this.waMonitor.waInstances[instanceName].fakeCall(data); - } } diff --git a/src/api/dto/chat.dto.ts b/src/api/dto/chat.dto.ts index 0d03987ea..b2f47a627 100644 --- a/src/api/dto/chat.dto.ts +++ b/src/api/dto/chat.dto.ts @@ -59,7 +59,7 @@ class Key { remoteJid: string; } export class ReadMessageDto { - read_messages: Key[]; + readMessages: Key[]; } export class LastMessage { diff --git a/src/api/dto/sendMessage.dto.ts b/src/api/dto/sendMessage.dto.ts index 7387a2436..ef922ae1d 100644 --- a/src/api/dto/sendMessage.dto.ts +++ b/src/api/dto/sendMessage.dto.ts @@ -131,11 +131,6 @@ export class SendListDto extends Metadata { sections: Section[]; } -export class FakeCallDto extends Metadata { - number: string; - delay: number; -} - export class ContactMessage { fullName: string; wuid: string; diff --git a/src/api/integrations/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatwoot/services/chatwoot.service.ts index de0c3a597..6ce757b9e 100644 --- a/src/api/integrations/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatwoot/services/chatwoot.service.ts @@ -1346,7 +1346,7 @@ export class ChatwootService { }; waInstance?.markMessageAsRead({ - read_messages: [ + readMessages: [ { id: key.id, fromMe: key.fromMe, diff --git a/src/api/routes/sendMessage.router.ts b/src/api/routes/sendMessage.router.ts index 223bbd3f3..006b49ef6 100644 --- a/src/api/routes/sendMessage.router.ts +++ b/src/api/routes/sendMessage.router.ts @@ -3,7 +3,6 @@ import { RequestHandler, Router } from 'express'; import { audioMessageSchema, contactMessageSchema, - fakeCallSchema, listMessageSchema, locationMessageSchema, mediaMessageSchema, @@ -16,7 +15,6 @@ import { } from '../../validate/validate.schema'; import { RouterBroker } from '../abstract/abstract.router'; import { - FakeCallDto, SendAudioDto, SendContactDto, SendListDto, @@ -145,16 +143,6 @@ export class MessageRouter extends RouterBroker { execute: (instance, data) => sendMessageController.sendList(instance, data), }); - return res.status(HttpStatus.CREATED).json(response); - }) - .post(this.routerPath('fakeCall'), ...guards, async (req, res) => { - const response = await this.dataValidate({ - request: req, - schema: fakeCallSchema, - ClassRef: FakeCallDto, - execute: (instance, data) => sendMessageController.fakeCall(instance, data), - }); - return res.status(HttpStatus.CREATED).json(response); }); // .post(this.routerPath('sendButtons'), ...guards, async (req, res) => { diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index 968fa0e22..040a05443 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -109,7 +109,6 @@ import { InstanceDto, SetPresenceDto } from '../../dto/instance.dto'; import { HandleLabelDto, LabelDto } from '../../dto/label.dto'; import { ContactMessage, - FakeCallDto, MediaMessage, Options, SendAudioDto, @@ -1985,6 +1984,8 @@ export class BaileysStartupService extends ChannelStartupService { await this.client.sendPresenceUpdate('paused', sender); } + + return { presence: data.presence }; } catch (error) { this.logger.error(error); throw new BadRequestException(error.toString()); @@ -2696,7 +2697,7 @@ export class BaileysStartupService extends ChannelStartupService { public async markMessageAsRead(data: ReadMessageDto) { try { const keys: proto.IMessageKey[] = []; - data.read_messages.forEach((read) => { + data.readMessages.forEach((read) => { if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) { keys.push({ remoteJid: read.remoteJid, @@ -3434,26 +3435,4 @@ export class BaileysStartupService extends ChannelStartupService { public async templateMessage() { throw new Error('Method not available in the Baileys service'); } - - public async fakeCall(data: FakeCallDto) { - try { - const number = this.createJid(data.number); - - if (number.includes('@g.us')) { - throw new BadRequestException('Group calls are not supported'); - } - - const mdDelay = data.delay ?? 0; - - const call = await this.client.offerCall(number); - - await delay(mdDelay); - - await this.client.rejectCall(call.callId, call.toJid); - - return call; - } catch (error) { - throw new BadRequestException('Error making fake call', error.toString()); - } - } } diff --git a/src/validate/chat.schema.ts b/src/validate/chat.schema.ts index 5ef1c93d4..dba279959 100644 --- a/src/validate/chat.schema.ts +++ b/src/validate/chat.schema.ts @@ -45,7 +45,7 @@ export const readMessageSchema: JSONSchema7 = { $id: v4(), type: 'object', properties: { - read_messages: { + readMessages: { type: 'array', minItems: 1, uniqueItems: true, @@ -60,7 +60,7 @@ export const readMessageSchema: JSONSchema7 = { }, }, }, - required: ['read_messages'], + required: ['readMessages'], }; export const archiveChatSchema: JSONSchema7 = { diff --git a/src/validate/message.schema.ts b/src/validate/message.schema.ts index e2a01c599..1f7548b1f 100644 --- a/src/validate/message.schema.ts +++ b/src/validate/message.schema.ts @@ -358,16 +358,3 @@ export const listMessageSchema: JSONSchema7 = { }, required: ['number', 'title', 'footerText', 'buttonText', 'sections'], }; - -export const fakeCallSchema: JSONSchema7 = { - $id: v4(), - type: 'object', - properties: { - number: { ...numberDefinition }, - delay: { - type: 'integer', - description: 'Enter a value in milliseconds', - }, - }, - required: ['number', 'delay'], -};