feat wa_business: send audio message

This commit is contained in:
Davidson Gomes 2024-03-02 12:03:11 -03:00
parent aef3495a79
commit 6b930058d1

View File

@ -13,6 +13,7 @@ import {
ContactMessage,
MediaMessage,
Options,
SendAudioDto,
SendButtonDto,
SendContactDto,
SendListDto,
@ -675,6 +676,20 @@ export class BusinessStartupService extends WAStartupService {
quoted ? (content.context = { message_id: quoted.id }) : content;
return await this.post(content, 'messages');
}
if (message['audio']) {
this.logger.verbose('Sending message');
content = {
messaging_product: 'whatsapp',
recipient_type: 'individual',
type: 'audio',
to: number.replace(/\D/g, ''),
audio: {
[message['type']]: message['id'],
},
};
quoted ? (content.context = { message_id: quoted.id }) : content;
return await this.post(content, 'messages');
}
if (message['buttons']) {
this.logger.verbose('Sending message');
content = {
@ -892,6 +907,45 @@ export class BusinessStartupService extends WAStartupService {
return await this.sendMessageWithTyping(data.number, { ...message }, data?.options, isChatwoot);
}
public async processAudio(audio: string, number: string) {
this.logger.verbose('Processing audio');
number = number.replace(/\D/g, '');
const hash = `${number}-${new Date().getTime()}`;
this.logger.verbose('Hash to audio name: ' + hash);
let mimetype: string;
const prepareMedia: any = {
fileName: `${hash}.mp3`,
mediaType: 'audio',
media: audio,
};
if (isURL(audio)) {
mimetype = getMIMEType(audio);
prepareMedia.id = audio;
prepareMedia.type = 'link';
} else {
mimetype = getMIMEType(prepareMedia.fileName);
const id = await this.getIdMedia(prepareMedia);
prepareMedia.id = id;
prepareMedia.type = 'id';
}
prepareMedia.mimetype = mimetype;
return prepareMedia;
}
public async audioWhatsapp(data: SendAudioDto, isChatwoot = false) {
this.logger.verbose('Sending audio whatsapp');
const message = await this.processAudio(data.audioMessage.audio, data.number);
return await this.sendMessageWithTyping(data.number, { ...message }, data?.options, isChatwoot);
}
public async buttonMessage(data: SendButtonDto) {
this.logger.verbose('Sending button message');
const embeddedMedia: any = {};
@ -1108,9 +1162,6 @@ export class BusinessStartupService extends WAStartupService {
public async mediaSticker() {
throw new BadRequestException('Method not available on WhatsApp Business API');
}
public async audioWhatsapp() {
throw new BadRequestException('Method not available on WhatsApp Business API');
}
public async pollMessage() {
throw new BadRequestException('Method not available on WhatsApp Business API');
}