feat: Added encoding option in endpoint sendWhatsAppAudio

This commit is contained in:
Davidson Gomes 2023-07-24 16:21:29 -03:00
parent 68d980795a
commit 8d91e7cb1d
5 changed files with 40 additions and 26 deletions

View File

@ -8,6 +8,7 @@
* Added reject call and send text message when receiving a call * Added reject call and send text message when receiving a call
* Added setting to ignore group messages * Added setting to ignore group messages
* Added connection with pairing code in chatwoot with command /init:{NUMBER} * Added connection with pairing code in chatwoot with command /init:{NUMBER}
* Added encoding option in endpoint sendWhatsAppAudio
### Fixed ### Fixed

View File

@ -107,7 +107,7 @@ export class InstanceController {
if (qrcode) { if (qrcode) {
this.logger.verbose('creating qrcode'); this.logger.verbose('creating qrcode');
await instance.connectToWhatsapp(number); await instance.connectToWhatsapp(number);
await delay(2000); await delay(3000);
getQrcode = instance.qrCode; getQrcode = instance.qrCode;
} }

View File

@ -16,6 +16,7 @@ export class Options {
quoted?: Quoted; quoted?: Quoted;
mentions?: Mentions; mentions?: Mentions;
linkPreview?: boolean; linkPreview?: boolean;
encoding?: boolean;
} }
class OptionsMessage { class OptionsMessage {
options: Options; options: Options;

View File

@ -1601,8 +1601,6 @@ export class ChatwootService {
const urlServer = this.configService.get<HttpServer>('SERVER').URL; const urlServer = this.configService.get<HttpServer>('SERVER').URL;
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY; const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
console.log('data: ', data);
const requestData = { const requestData = {
instanceName, instanceName,
qrcode, qrcode,
@ -1616,8 +1614,6 @@ export class ChatwootService {
requestData['number'] = number; requestData['number'] = number;
} }
console.log('requestData: ', requestData);
const config = { const config = {
method: 'post', method: 'post',
maxBodyLength: Infinity, maxBodyLength: Infinity,

View File

@ -1354,7 +1354,6 @@ export class WAStartupService {
if (events.call) { if (events.call) {
this.logger.verbose('Listening event: call'); this.logger.verbose('Listening event: call');
console.log('events.call', events.call);
const call = events.call[0]; const call = events.call[0];
if (settings?.reject_call && call.status == 'offer') { if (settings?.reject_call && call.status == 'offer') {
@ -1662,8 +1661,6 @@ export class WAStartupService {
const linkPreview = options?.linkPreview != false ? undefined : false; const linkPreview = options?.linkPreview != false ? undefined : false;
console.log('linkPreview', linkPreview);
let quoted: WAMessage; let quoted: WAMessage;
if (options?.quoted) { if (options?.quoted) {
@ -2179,26 +2176,45 @@ export class WAStartupService {
public async audioWhatsapp(data: SendAudioDto) { public async audioWhatsapp(data: SendAudioDto) {
this.logger.verbose('Sending audio whatsapp'); this.logger.verbose('Sending audio whatsapp');
const convert = await this.processAudio(data.audioMessage.audio, data.number);
if (typeof convert === 'string') {
const audio = fs.readFileSync(convert).toString('base64');
const result = this.sendMessageWithTyping<AnyMessageContent>(
data.number,
{
audio: Buffer.from(audio, 'base64'),
ptt: true,
mimetype: 'audio/mp4',
},
{ presence: 'recording', delay: data?.options?.delay },
);
fs.unlinkSync(convert); if (!data.options?.encoding && data.options?.encoding !== false) {
this.logger.verbose('Converted audio deleted'); data.options.encoding = true;
return result;
} else {
throw new InternalServerErrorException(convert);
} }
if (data.options?.encoding) {
const convert = await this.processAudio(data.audioMessage.audio, data.number);
if (typeof convert === 'string') {
const audio = fs.readFileSync(convert).toString('base64');
const result = this.sendMessageWithTyping<AnyMessageContent>(
data.number,
{
audio: Buffer.from(audio, 'base64'),
ptt: true,
mimetype: 'audio/mp4',
},
{ presence: 'recording', delay: data?.options?.delay },
);
fs.unlinkSync(convert);
this.logger.verbose('Converted audio deleted');
return result;
} else {
throw new InternalServerErrorException(convert);
}
}
return await this.sendMessageWithTyping<AnyMessageContent>(
data.number,
{
audio: isURL(data.audioMessage.audio)
? { url: data.audioMessage.audio }
: Buffer.from(data.audioMessage.audio, 'base64'),
ptt: true,
mimetype: 'audio/ogg; codecs=opus',
},
{ presence: 'recording', delay: data?.options?.delay },
);
} }
public async buttonMessage(data: SendButtonDto) { public async buttonMessage(data: SendButtonDto) {