Skip to content

Commit

Permalink
adjusts in v1.7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Apr 16, 2024
1 parent 5e0e405 commit d6e19b9
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 42 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
140 changes: 101 additions & 39 deletions src/api/services/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ info:
</font>
[![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
Expand Down

0 comments on commit d6e19b9

Please sign in to comment.