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.
This commit is contained in:
Davidson Gomes 2024-06-26 18:23:18 -03:00
parent 6cb3357f08
commit b70ab5a4c3

View File

@ -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) {