Skip to content

Commit

Permalink
fix: Correction in WhatsApp sticker sending
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgcode-tec committed Jun 26, 2024
1 parent 99f4fe2 commit 6cb3357
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/api/services/channels/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import EventEmitter2 from 'eventemitter2';
// import { exec } from 'child_process';
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 NodeCache from 'node-cache';
import { getMIMEType } from 'node-mime-types';
Expand Down Expand Up @@ -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(
sender,
{
Expand Down Expand Up @@ -2283,19 +2283,13 @@ export class BaileysStartupService extends ChannelStartupService {
}
}

private async convertToWebP(image: string, number: string) {
private async convertToWebP(image: string): Promise<Buffer> {
try {
let imagePath: string;
const hash = `${number}-${new Date().getTime()}`;

const outputPath = `${join(this.storePath, 'temp', `${hash}.webp`)}`;
let imageBuffer: Buffer;

if (isBase64(image)) {
const base64Data = image.replace(/^data:image\/(jpeg|png|gif);base64,/, '');
const imageBuffer = Buffer.from(base64Data, 'base64');
imagePath = `${join(this.storePath, 'temp', `temp-${hash}.png`)}`;

await sharp(imageBuffer).toFile(imagePath);
imageBuffer = Buffer.from(base64Data, 'base64');
} else {
const timestamp = new Date().getTime();
const url = `${image}?timestamp=${timestamp}`;
Expand All @@ -2318,29 +2312,26 @@ export class BaileysStartupService extends ChannelStartupService {
}

const response = await axios.get(url, config);

const imageBuffer = Buffer.from(response.data, 'binary');
imagePath = `${join(this.storePath, 'temp', `temp-${hash}.png`)}`;

await sharp(imageBuffer).toFile(imagePath);
imageBuffer = Buffer.from(response.data, 'binary');
}

await sharp(imagePath).webp().toFile(outputPath);

fs.unlinkSync(imagePath);
const webpBuffer = await sharp(imageBuffer).webp().toBuffer();

return outputPath;
return webpBuffer;
} catch (error) {
console.error('Erro ao converter a imagem para WebP:', error);
throw error;
}
}

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(
data.number,
{
sticker: { url: convert },
sticker: convert,
gifPlayback,
},
{
delay: data?.delay,
Expand All @@ -2351,8 +2342,6 @@ export class BaileysStartupService extends ChannelStartupService {
},
);

fs.unlinkSync(convert);

return result;
}

Expand Down

0 comments on commit 6cb3357

Please sign in to comment.