From b70ab5a4c3058f9d95bd1efe02432d9c7db1088b Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Wed, 26 Jun 2024 18:23:18 -0300 Subject: [PATCH] fix: Update message function to support different message types The updateMessage function was updated to support different message types such as text, image, and video. A new private function formatUpdateMessage was added to handle the logic of formatting the message based on its type. The function checks the message type and returns the corresponding format for the message. If the message type is not supported, it will return null and throw a BadRequestException. The createJid function is now called in the updateMessage function to get the jid of the recipient. This change improves the flexibility of the updateMessage function and ensures that it can handle different types of messages. --- .../channels/whatsapp.baileys.service.ts | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index b008e391..409dc90b 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -3096,12 +3096,51 @@ export class BaileysStartupService extends ChannelStartupService { } } - public async updateMessage(data: UpdateMessageDto) { + private async formatUpdateMessage(data: UpdateMessageDto) { try { - const jid = this.createJid(data.number); + const msg: any = await this.getMessage(data.key, true); + console.log('msg', msg); + if (msg?.messageType === 'conversation' || msg?.messageType === 'extendedTextMessage') { + return { + text: data.text, + }; + } + + if (msg?.messageType === 'imageMessage') { + return { + image: msg?.message?.imageMessage, + caption: data.text, + }; + } + + if (msg?.messageType === 'videoMessage') { + return { + video: msg?.message?.videoMessage, + caption: data.text, + }; + } + + return null; + } catch (error) { + this.logger.error(error); + throw new BadRequestException(error.toString()); + } + } + + public async updateMessage(data: UpdateMessageDto) { + const jid = this.createJid(data.number); + + const options = await this.formatUpdateMessage(data); + + if (!options) { + this.logger.error('Message not compatible'); + throw new BadRequestException('Message not compatible'); + } + + try { return await this.client.sendMessage(jid, { - text: data.text, + ...(options as any), edit: data.key, }); } catch (error) {