fix: Correction in WhatsApp sticker sending

Corrected an issue where stickers were not being sent properly in WhatsApp channels. The bug was caused by a mistake in the 'src/api/services/channels/whatsapp.baileys.service.ts' file. This fix resolves the problem and ensures that stickers are sent correctly in WhatsApp channels.
This commit is contained in:
Davidson Gomes 2024-06-26 17:52:51 -03:00
parent 99f4fe2e43
commit 6cb3357f08

View File

@ -45,7 +45,7 @@ import EventEmitter2 from 'eventemitter2';
// import { exec } from 'child_process'; // import { exec } from 'child_process';
import ffmpeg from 'fluent-ffmpeg'; import ffmpeg from 'fluent-ffmpeg';
// import ffmpeg from 'fluent-ffmpeg'; // import ffmpeg from 'fluent-ffmpeg';
import fs, { existsSync, readFileSync } from 'fs'; import { existsSync, readFileSync } from 'fs';
import Long from 'long'; import Long from 'long';
import NodeCache from 'node-cache'; import NodeCache from 'node-cache';
import { getMIMEType } from 'node-mime-types'; import { getMIMEType } from 'node-mime-types';
@ -1897,7 +1897,7 @@ export class BaileysStartupService extends ChannelStartupService {
); );
} }
if (!message['audio'] && !message['poll'] && sender != 'status@broadcast') { if (!message['audio'] && !message['poll'] && !message['sticker'] && sender != 'status@broadcast') {
return await this.client.sendMessage( return await this.client.sendMessage(
sender, sender,
{ {
@ -2283,19 +2283,13 @@ export class BaileysStartupService extends ChannelStartupService {
} }
} }
private async convertToWebP(image: string, number: string) { private async convertToWebP(image: string): Promise<Buffer> {
try { try {
let imagePath: string; let imageBuffer: Buffer;
const hash = `${number}-${new Date().getTime()}`;
const outputPath = `${join(this.storePath, 'temp', `${hash}.webp`)}`;
if (isBase64(image)) { if (isBase64(image)) {
const base64Data = image.replace(/^data:image\/(jpeg|png|gif);base64,/, ''); const base64Data = image.replace(/^data:image\/(jpeg|png|gif);base64,/, '');
const imageBuffer = Buffer.from(base64Data, 'base64'); imageBuffer = Buffer.from(base64Data, 'base64');
imagePath = `${join(this.storePath, 'temp', `temp-${hash}.png`)}`;
await sharp(imageBuffer).toFile(imagePath);
} else { } else {
const timestamp = new Date().getTime(); const timestamp = new Date().getTime();
const url = `${image}?timestamp=${timestamp}`; const url = `${image}?timestamp=${timestamp}`;
@ -2318,29 +2312,26 @@ export class BaileysStartupService extends ChannelStartupService {
} }
const response = await axios.get(url, config); const response = await axios.get(url, config);
imageBuffer = Buffer.from(response.data, 'binary');
const imageBuffer = Buffer.from(response.data, 'binary');
imagePath = `${join(this.storePath, 'temp', `temp-${hash}.png`)}`;
await sharp(imageBuffer).toFile(imagePath);
} }
await sharp(imagePath).webp().toFile(outputPath); const webpBuffer = await sharp(imageBuffer).webp().toBuffer();
fs.unlinkSync(imagePath); return webpBuffer;
return outputPath;
} catch (error) { } catch (error) {
console.error('Erro ao converter a imagem para WebP:', error); console.error('Erro ao converter a imagem para WebP:', error);
throw error;
} }
} }
public async mediaSticker(data: SendStickerDto) { public async mediaSticker(data: SendStickerDto) {
const convert = await this.convertToWebP(data.sticker, data.number); const convert = await this.convertToWebP(data.sticker);
const gifPlayback = data.sticker.includes('.gif');
const result = await this.sendMessageWithTyping( const result = await this.sendMessageWithTyping(
data.number, data.number,
{ {
sticker: { url: convert }, sticker: convert,
gifPlayback,
}, },
{ {
delay: data?.delay, delay: data?.delay,
@ -2351,8 +2342,6 @@ export class BaileysStartupService extends ChannelStartupService {
}, },
); );
fs.unlinkSync(convert);
return result; return result;
} }