Skip to content

Commit

Permalink
fix: Update message function to support different message types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgcode-tec committed Jun 26, 2024
1 parent 6cb3357 commit b70ab5a
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/api/services/channels/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b70ab5a

Please sign in to comment.