Skip to content

Commit 6b93005

Browse files
committed
feat wa_business: send audio message
1 parent aef3495 commit 6b93005

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

src/whatsapp/services/whatsapp.business.service.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ContactMessage,
1414
MediaMessage,
1515
Options,
16+
SendAudioDto,
1617
SendButtonDto,
1718
SendContactDto,
1819
SendListDto,
@@ -675,6 +676,20 @@ export class BusinessStartupService extends WAStartupService {
675676
quoted ? (content.context = { message_id: quoted.id }) : content;
676677
return await this.post(content, 'messages');
677678
}
679+
if (message['audio']) {
680+
this.logger.verbose('Sending message');
681+
content = {
682+
messaging_product: 'whatsapp',
683+
recipient_type: 'individual',
684+
type: 'audio',
685+
to: number.replace(/\D/g, ''),
686+
audio: {
687+
[message['type']]: message['id'],
688+
},
689+
};
690+
quoted ? (content.context = { message_id: quoted.id }) : content;
691+
return await this.post(content, 'messages');
692+
}
678693
if (message['buttons']) {
679694
this.logger.verbose('Sending message');
680695
content = {
@@ -892,6 +907,45 @@ export class BusinessStartupService extends WAStartupService {
892907
return await this.sendMessageWithTyping(data.number, { ...message }, data?.options, isChatwoot);
893908
}
894909

910+
public async processAudio(audio: string, number: string) {
911+
this.logger.verbose('Processing audio');
912+
913+
number = number.replace(/\D/g, '');
914+
const hash = `${number}-${new Date().getTime()}`;
915+
this.logger.verbose('Hash to audio name: ' + hash);
916+
917+
let mimetype: string;
918+
919+
const prepareMedia: any = {
920+
fileName: `${hash}.mp3`,
921+
mediaType: 'audio',
922+
media: audio,
923+
};
924+
925+
if (isURL(audio)) {
926+
mimetype = getMIMEType(audio);
927+
prepareMedia.id = audio;
928+
prepareMedia.type = 'link';
929+
} else {
930+
mimetype = getMIMEType(prepareMedia.fileName);
931+
const id = await this.getIdMedia(prepareMedia);
932+
prepareMedia.id = id;
933+
prepareMedia.type = 'id';
934+
}
935+
936+
prepareMedia.mimetype = mimetype;
937+
938+
return prepareMedia;
939+
}
940+
941+
public async audioWhatsapp(data: SendAudioDto, isChatwoot = false) {
942+
this.logger.verbose('Sending audio whatsapp');
943+
944+
const message = await this.processAudio(data.audioMessage.audio, data.number);
945+
946+
return await this.sendMessageWithTyping(data.number, { ...message }, data?.options, isChatwoot);
947+
}
948+
895949
public async buttonMessage(data: SendButtonDto) {
896950
this.logger.verbose('Sending button message');
897951
const embeddedMedia: any = {};
@@ -1108,9 +1162,6 @@ export class BusinessStartupService extends WAStartupService {
11081162
public async mediaSticker() {
11091163
throw new BadRequestException('Method not available on WhatsApp Business API');
11101164
}
1111-
public async audioWhatsapp() {
1112-
throw new BadRequestException('Method not available on WhatsApp Business API');
1113-
}
11141165
public async pollMessage() {
11151166
throw new BadRequestException('Method not available on WhatsApp Business API');
11161167
}

0 commit comments

Comments
 (0)