From d6e19b92731a2de1179050619fec55bdccd02925 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Tue, 16 Apr 2024 19:08:44 -0300 Subject: [PATCH] adjusts in v1.7.3 --- CHANGELOG.md | 7 + Dockerfile | 2 +- package.json | 2 +- .../whatsapp/whatsapp.baileys.service.ts | 140 +++++++++++++----- src/docs/swagger.yaml | 2 +- 5 files changed, 111 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4c4363a9..dddbe8c5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# 1.7.3 (develop) + +### Fixed +* Fix audio encoding +* Recovering messages lost +* Adjusts in proxy + # 1.7.2 (2024-04-12 17:31) ### Feature diff --git a/Dockerfile b/Dockerfile index b4f20c803..b913a37e9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM node:20.7.0-alpine AS builder -LABEL version="1.7.2" description="Api to control whatsapp features through http requests." +LABEL version="1.7.3" description="Api to control whatsapp features through http requests." LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes" LABEL contact="contato@agenciadgcode.com" diff --git a/package.json b/package.json index 8b31f9025..bf5832ad2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "evolution-api", - "version": "1.7.2", + "version": "1.7.3", "description": "Rest api for communication with WhatsApp", "main": "./dist/src/main.js", "scripts": { diff --git a/src/api/services/whatsapp/whatsapp.baileys.service.ts b/src/api/services/whatsapp/whatsapp.baileys.service.ts index 6285250bb..eba80acb7 100644 --- a/src/api/services/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/services/whatsapp/whatsapp.baileys.service.ts @@ -39,9 +39,10 @@ import makeWASocket, { import { Label } from '@whiskeysockets/baileys/lib/Types/Label'; import { LabelAssociation } from '@whiskeysockets/baileys/lib/Types/LabelAssociation'; import axios from 'axios'; +import { exec } from 'child_process'; import { arrayUnique, isBase64, isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; -import ffmpeg from 'fluent-ffmpeg'; +// import ffmpeg from 'fluent-ffmpeg'; import fs, { existsSync, readFileSync } from 'fs'; import { parsePhoneNumber } from 'libphonenumber-js'; import Long from 'long'; @@ -485,11 +486,11 @@ export class BaileysStartupService extends WAStartupService { let options; if (this.localProxy.enabled) { - this.logger.info('Proxy enabled: ' + this.localProxy.proxy.host); + this.logger.info('Proxy enabled: ' + this.localProxy.proxy?.host); if (this.localProxy?.proxy?.host?.includes('proxyscrape')) { try { - const response = await axios.get(this.localProxy.proxy.host); + const response = await axios.get(this.localProxy.proxy?.host); const text = response.data; const proxyUrls = text.split('\r\n'); const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length)); @@ -655,11 +656,11 @@ export class BaileysStartupService extends WAStartupService { let options; if (this.localProxy.enabled) { - this.logger.info('Proxy enabled: ' + this.localProxy.proxy.host); + this.logger.info('Proxy enabled: ' + this.localProxy.proxy?.host); if (this.localProxy?.proxy?.host?.includes('proxyscrape')) { try { - const response = await axios.get(this.localProxy.proxy.host); + const response = await axios.get(this.localProxy.proxy?.host); const text = response.data; const proxyUrls = text.split('\r\n'); const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length)); @@ -1043,6 +1044,12 @@ export class BaileysStartupService extends WAStartupService { } } + if (received.messageStubParameters && received.messageStubParameters[0] === 'Message absent from node') { + this.logger.info('Recovering message lost'); + await this.client.sendMessageAck(JSON.parse(received.messageStubParameters[1], BufferJSON.reviver)); + continue; + } + if ( (type !== 'notify' && type !== 'append') || received.message?.protocolMessage || @@ -2093,7 +2100,7 @@ export class BaileysStartupService extends WAStartupService { content: { audio: Buffer.from(audio, 'base64'), ptt: true, - mimetype: 'audio/mp4', + mimetype: 'audio/ogg; codecs=opus', }, option: { statusJidList: status.statusJidList, @@ -2297,6 +2304,82 @@ export class BaileysStartupService extends WAStartupService { return await this.sendMessageWithTyping(data.number, { ...generate.message }, data?.options, isChatwoot); } + // public async processAudio(audio: string, number: string) { + // this.logger.verbose('Processing audio'); + // let tempAudioPath: string; + // let outputAudio: string; + + // number = number.replace(/\D/g, ''); + // const hash = `${number}-${new Date().getTime()}`; + // this.logger.verbose('Hash to audio name: ' + hash); + + // if (isURL(audio)) { + // this.logger.verbose('Audio is url'); + + // outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`; + // tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`; + + // this.logger.verbose('Output audio path: ' + outputAudio); + // this.logger.verbose('Temp audio path: ' + tempAudioPath); + + // const timestamp = new Date().getTime(); + // const url = `${audio}?timestamp=${timestamp}`; + + // this.logger.verbose('Including timestamp in url: ' + url); + + // let config: any = { + // responseType: 'arraybuffer', + // }; + + // if (this.localProxy.enabled) { + // config = { + // ...config, + // httpsAgent: makeProxyAgent(this.localProxy.proxy), + // }; + // } + + // const response = await axios.get(url, config); + // this.logger.verbose('Getting audio from url'); + + // fs.writeFileSync(tempAudioPath, response.data); + // } else { + // this.logger.verbose('Audio is base64'); + + // outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`; + // tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`; + + // this.logger.verbose('Output audio path: ' + outputAudio); + // this.logger.verbose('Temp audio path: ' + tempAudioPath); + + // const audioBuffer = Buffer.from(audio, 'base64'); + // fs.writeFileSync(tempAudioPath, audioBuffer); + // this.logger.verbose('Temp audio created'); + // } + + // this.logger.verbose('Converting audio to mp4'); + // return new Promise((resolve, reject) => { + // // This fix was suggested by @PurpShell + // ffmpeg.setFfmpegPath(ffmpegPath.path); + + // ffmpeg() + // .input(tempAudioPath) + // .outputFormat('ogg') + // .noVideo() + // .audioCodec('libopus') + // .save(outputAudio) + // .on('error', function (error) { + // console.log('error', error); + // fs.unlinkSync(tempAudioPath); + // if (error) reject(error); + // }) + // .on('end', async function () { + // fs.unlinkSync(tempAudioPath); + // resolve(outputAudio); + // }) + // .run(); + // }); + // } + public async processAudio(audio: string, number: string) { this.logger.verbose('Processing audio'); let tempAudioPath: string; @@ -2309,7 +2392,7 @@ export class BaileysStartupService extends WAStartupService { if (isURL(audio)) { this.logger.verbose('Audio is url'); - outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`; + outputAudio = `${join(this.storePath, 'temp', `${hash}.mp4`)}`; tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`; this.logger.verbose('Output audio path: ' + outputAudio); @@ -2320,25 +2403,14 @@ export class BaileysStartupService extends WAStartupService { this.logger.verbose('Including timestamp in url: ' + url); - let config: any = { - responseType: 'arraybuffer', - }; - - if (this.localProxy.enabled) { - config = { - ...config, - httpsAgent: makeProxyAgent(this.localProxy.proxy), - }; - } - - const response = await axios.get(url, config); + const response = await axios.get(url, { responseType: 'arraybuffer' }); this.logger.verbose('Getting audio from url'); fs.writeFileSync(tempAudioPath, response.data); } else { this.logger.verbose('Audio is base64'); - outputAudio = `${join(this.storePath, 'temp', `${hash}.ogg`)}`; + outputAudio = `${join(this.storePath, 'temp', `${hash}.mp4`)}`; tempAudioPath = `${join(this.storePath, 'temp', `temp-${hash}.mp3`)}`; this.logger.verbose('Output audio path: ' + outputAudio); @@ -2351,25 +2423,15 @@ export class BaileysStartupService extends WAStartupService { this.logger.verbose('Converting audio to mp4'); return new Promise((resolve, reject) => { - // This fix was suggested by @PurpShell - ffmpeg.setFfmpegPath(ffmpegPath.path); - - ffmpeg() - .input(tempAudioPath) - .outputFormat('ogg') - .noVideo() - .audioCodec('libopus') - .save(outputAudio) - .on('error', function (error) { - console.log('error', error); - fs.unlinkSync(tempAudioPath); - if (error) reject(error); - }) - .on('end', async function () { - fs.unlinkSync(tempAudioPath); - resolve(outputAudio); - }) - .run(); + exec(`${ffmpegPath.path} -i ${tempAudioPath} -vn -ab 128k -ar 44100 -f ipod ${outputAudio} -y`, (error) => { + fs.unlinkSync(tempAudioPath); + this.logger.verbose('Temp audio deleted'); + + if (error) reject(error); + + this.logger.verbose('Audio converted to mp4'); + resolve(outputAudio); + }); }); } diff --git a/src/docs/swagger.yaml b/src/docs/swagger.yaml index 1b307f5fe..3a7a2d15a 100644 --- a/src/docs/swagger.yaml +++ b/src/docs/swagger.yaml @@ -25,7 +25,7 @@ info: [![Run in Postman](https://run.pstmn.io/button.svg)](https://god.gw.postman.com/run-collection/26869335-5546d063-156b-4529-915f-909dd628c090?action=collection%2Ffork&source=rip_markdown&collection-url=entityId%3D26869335-5546d063-156b-4529-915f-909dd628c090%26entityType%3Dcollection%26workspaceId%3D339a4ee7-378b-45c9-b5b8-fd2c0a9c2442) - version: 1.7.2 + version: 1.7.3 contact: name: DavidsonGomes email: contato@agenciadgcode.com