Skip to content

Commit

Permalink
fix(config): autoDownload and UploadS3 in websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioselau077 authored and icleitoncosta committed Nov 4, 2023
1 parent 8792a04 commit 7324610
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ This server use config.ts file to define some options, default values are:
// 'event', 'from' ou 'type' to ignore and not send to webhook
ignore: [],
},
websocket: {
// Just leave one active, here or on webhook.autoDownload
autoDownload: false,
// Just leave one active, here or on webhook.uploadS3, to avoid duplication in S3
uploadS3: false,
},
// send data to chatwoot
chatwoot: {
sendQrCode: true,
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export default {
onSelfMessage: false,
ignore: ['status@broadcast'],
},
websocket: {
autoDownload: false,
uploadS3: false,
},
chatwoot: {
sendQrCode: true,
sendStatus: true,
Expand Down
4 changes: 4 additions & 0 deletions src/types/ServerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export interface ServerOptions {
onSelfMessage: boolean;
ignore: string[];
};
websocket: {
autoDownload: boolean;
uploadS3: boolean;
};
archive: {
enable: boolean;
waitTime: number;
Expand Down
8 changes: 6 additions & 2 deletions src/util/createSessionUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Request } from 'express';
import { download } from '../controller/sessionController';
import { WhatsAppServer } from '../types/WhatsAppServer';
import chatWootClient from './chatWootClient';
import { callWebHook, startHelper } from './functions';
import { autoDownload, callWebHook, startHelper } from './functions';
import { clientsArray, eventEmitter } from './sessionUtil';
import Factory from './tokenStore/factory';

Expand Down Expand Up @@ -232,13 +232,17 @@ export default class CreateSessionUtil {
});
});

await client.onAnyMessage((message: any) => {
await client.onAnyMessage(async (message: any) => {
message.session = client.session;

if (message.type === 'sticker') {
download(message, client, req.logger);
}

if (req.serverOptions?.websocket?.autoDownload) {
await autoDownload(client, req, message);
}

req.io.emit('received-message', { response: message });
if (req.serverOptions.webhook.onSelfMessage && message.fromMe)
callWebHook(client, req, 'onselfmessage', message);
Expand Down
11 changes: 9 additions & 2 deletions src/util/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ if (config.webhook.uploadS3) {
mime = config.webhook.uploadS3 ? mimetypes : null;
crypto = config.webhook.uploadS3 ? Crypto : null;
}
if (config?.websocket?.uploadS3) {
mime = config.websocket.uploadS3 ? mimetypes : null;
crypto = config.websocket.uploadS3 ? Crypto : null;
}

export function contactToArray(number: any, isGroup?: boolean) {
const localArr: any = [];
Expand Down Expand Up @@ -143,11 +147,14 @@ export async function callWebHook(
}
}

async function autoDownload(client: any, req: any, message: any) {
export async function autoDownload(client: any, req: any, message: any) {
try {
if (message && (message['mimetype'] || message.isMedia || message.isMMS)) {
const buffer = await client.decryptFile(message);
if (req.serverOptions.webhook.uploadS3) {
if (
req.serverOptions.webhook.uploadS3 ||
req.serverOptions?.websocket?.uploadS3
) {
const hashName = crypto.randomBytes(24).toString('hex');

if (
Expand Down

0 comments on commit 7324610

Please sign in to comment.