diff --git a/src/whatsapp/services/whatsapp.business.service.ts b/src/whatsapp/services/whatsapp.business.service.ts index 73b1c299..cb802b7f 100644 --- a/src/whatsapp/services/whatsapp.business.service.ts +++ b/src/whatsapp/services/whatsapp.business.service.ts @@ -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'); }