Skip to content

Commit

Permalink
Merge branch 'release/1.5.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Sep 28, 2023
2 parents bd64b0c + 413ad66 commit bddd640
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 39 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@

# 1.5.2 (2023-09-28 17:56)

### Fixed

* Fix chatwootSchema in chatwoot model to store reopen_conversation and conversation_pending options
* Problem resolved when sending files from minio to typebot
* Improvement in the "startTypebot" method to create persistent session when triggered
* New manager for Evo 1.5.2 - Set Typebot update
* Resolved problems when reading/querying instances

# 1.5.1 (2023-09-17 13:50)

### 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:16.18-alpine

LABEL version="1.5.1" description="Api to control whatsapp features through http requests."
LABEL version="1.5.2" description="Api to control whatsapp features through http requests."
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
LABEL contact="[email protected]"

Expand Down
2 changes: 1 addition & 1 deletion Extras/appsmith/manager.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "evolution-api",
"version": "1.5.1",
"version": "1.5.2",
"description": "Rest api for communication with WhatsApp",
"main": "./dist/src/main.js",
"scripts": {
Expand Down Expand Up @@ -46,7 +46,7 @@
"@figuro/chatwoot-sdk": "^1.1.16",
"@hapi/boom": "^10.0.1",
"@sentry/node": "^7.59.2",
"@whiskeysockets/baileys": "^6.4.0",
"@whiskeysockets/baileys": "^6.5.0",
"amqplib": "^0.10.3",
"axios": "^1.3.5",
"class-validator": "^0.13.2",
Expand Down Expand Up @@ -78,8 +78,7 @@
"sharp": "^0.30.7",
"socket.io": "^4.7.1",
"socks-proxy-agent": "^8.0.1",
"uuid": "^9.0.0",
"whatsapp-web.js": "^1.22.1"
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/compression": "^1.7.2",
Expand Down
7 changes: 7 additions & 0 deletions src/whatsapp/dto/typebot.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ export class Session {
status?: string;
createdAt?: number;
updateAt?: number;
prefilledVariables?: PrefilledVariables;
}

export class PrefilledVariables {
remoteJid?: string;
pushName?: string;
additionalData?: { [key: string]: any };
}

export class TypebotDto {
Expand Down
2 changes: 2 additions & 0 deletions src/whatsapp/models/chatwoot.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const chatwootSchema = new Schema<ChatwootRaw>({
name_inbox: { type: String, required: true },
sign_msg: { type: Boolean, required: true },
number: { type: String, required: true },
reopen_conversation: { type: Boolean, required: true },
conversation_pending: { type: Boolean, required: true },
});

export const ChatwootModel = dbserver?.model(ChatwootRaw.name, chatwootSchema, 'chatwoot');
Expand Down
10 changes: 10 additions & 0 deletions src/whatsapp/models/typebot.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class Session {
status?: string;
createdAt?: number;
updateAt?: number;
prefilledVariables?: {
remoteJid?: string;
pushName?: string;
additionalData?: { [key: string]: any };
};
}

export class TypebotRaw {
Expand Down Expand Up @@ -40,6 +45,11 @@ const typebotSchema = new Schema<TypebotRaw>({
status: { type: String, required: true },
createdAt: { type: Number, required: true },
updateAt: { type: Number, required: true },
prefilledVariables: {
remoteJid: { type: String, required: false },
pushName: { type: String, required: false },
additionalData: { type: Schema.Types.Mixed, required: false }
},
},
],
});
Expand Down
95 changes: 94 additions & 1 deletion src/whatsapp/services/monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { join } from 'path';
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
// inserido por francis inicio
import { NotFoundException } from '../../exceptions';
// inserido por francis fim
import { dbserver } from '../../libs/db.connect';
import { RedisCache } from '../../libs/redis.client';
import {
Expand Down Expand Up @@ -72,7 +75,7 @@ export class WAMonitoringService {
}, 1000 * 60 * time);
}
}

/* ocultado por francis inicio
public async instanceInfo(instanceName?: string) {
this.logger.verbose('get instance info');
Expand Down Expand Up @@ -128,6 +131,96 @@ export class WAMonitoringService {
return instances;
}
ocultado por francis fim */

// inserido por francis inicio

public async instanceInfo(instanceName?: string) {
this.logger.verbose('get instance info');
if (instanceName && !this.waInstances[instanceName]) {
throw new NotFoundException(`Instance "${instanceName}" not found`);
}

const instances: any[] = [];

for await (const [key, value] of Object.entries(this.waInstances)) {
if (value) {
this.logger.verbose('get instance info: ' + key);
let chatwoot: any;

const urlServer = this.configService.get<HttpServer>('SERVER').URL;

const findChatwoot = await this.waInstances[key].findChatwoot();

if (findChatwoot && findChatwoot.enabled) {
chatwoot = {
...findChatwoot,
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
};
}

if (value.connectionStatus.state === 'open') {
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');

const instanceData = {
instance: {
instanceName: key,
owner: value.wuid,
profileName: (await value.getProfileName()) || 'not loaded',
profilePictureUrl: value.profilePictureUrl,
profileStatus: (await value.getProfileStatus()) || '',
status: value.connectionStatus.state,
},
};

if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;

instanceData.instance['apikey'] = (await this.repository.auth.find(key)).apikey;

instanceData.instance['chatwoot'] = chatwoot;
}

instances.push(instanceData);
} else {
this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state);

const instanceData = {
instance: {
instanceName: key,
status: value.connectionStatus.state,
},
};

if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;

instanceData.instance['apikey'] = (await this.repository.auth.find(key)).apikey;

instanceData.instance['chatwoot'] = chatwoot;
}

instances.push(instanceData);
}
}
}

this.logger.verbose('return instance info: ' + instances.length);

return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
}



// inserido por francis fim








private delInstanceFiles() {
this.logger.verbose('cron to delete instance files started');
setInterval(async () => {
Expand Down
71 changes: 45 additions & 26 deletions src/whatsapp/services/typebot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ export class TypebotService {
const url = data.url;
const typebot = data.typebot;
const variables = data.variables;
const findTypebot = await this.find(instance);
const sessions = (findTypebot.sessions as Session[]) ?? [];
const expire = findTypebot.expire;
const keyword_finish = findTypebot.keyword_finish;
const delay_message = findTypebot.delay_message;
const unknown_message = findTypebot.unknown_message;
const listening_from_me = findTypebot.listening_from_me;

const prefilledVariables = {
remoteJid: remoteJid,
Expand All @@ -109,42 +116,47 @@ export class TypebotService {
prefilledVariables[variable.name] = variable.value;
});

const id = Math.floor(Math.random() * 10000000000).toString();

const reqData = {
sessionId: id,
startParams: {
typebot: data.typebot,
prefilledVariables: prefilledVariables,
},
};

const request = await axios.post(data.url + '/api/v1/sendMessage', reqData);

await this.sendWAMessage(
instance,
remoteJid,
request.data.messages,
request.data.input,
request.data.clientSideActions,
);

this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
remoteJid: remoteJid,
const response = await this.createNewSession(instance, {
url: url,
typebot: typebot,
variables: variables,
sessionId: id,
remoteJid: remoteJid,
expire: expire,
keyword_finish: keyword_finish,
delay_message: delay_message,
unknown_message: unknown_message,
listening_from_me: listening_from_me,
sessions: sessions,
prefilledVariables: prefilledVariables,
});

if (response.sessionId) {
await this.sendWAMessage(
instance,
remoteJid,
response.messages,
response.input,
response.clientSideActions,
);

this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
remoteJid: remoteJid,
url: url,
typebot: typebot,
prefilledVariables: prefilledVariables,
sessionId: `${response.sessionId}`,
});
} else {
throw new Error("Session ID not found in response");
}

return {
typebot: {
...instance,
typebot: {
url: url,
remoteJid: remoteJid,
typebot: typebot,
variables: variables,
prefilledVariables: prefilledVariables,
},
},
};
Expand Down Expand Up @@ -193,8 +205,9 @@ export class TypebotService {
startParams: {
typebot: data.typebot,
prefilledVariables: {
...data.prefilledVariables,
remoteJid: data.remoteJid,
pushName: data.pushName,
pushName: data.pushName || 'Default Name',
instanceName: instance.instanceName,
},
},
Expand All @@ -209,6 +222,12 @@ export class TypebotService {
status: 'opened',
createdAt: Date.now(),
updateAt: Date.now(),
prefilledVariables: {
...data.prefilledVariables,
remoteJid: data.remoteJid,
pushName: data.pushName || 'Default Name',
instanceName: instance.instanceName,
}
});

const typebotData = {
Expand Down
26 changes: 20 additions & 6 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2308,22 +2308,36 @@ export class WAStartupService {
mediaMessage.fileName = arrayMatch[1];
this.logger.verbose('File name: ' + mediaMessage.fileName);
}
// *inserido francis inicio
let mimetype: string;
// *inserido francis final


if (mediaMessage.mediatype === 'image' && !mediaMessage.fileName) {
mediaMessage.fileName = 'image.png';
// inserido francis inicio
mimetype = 'image/png';
// inserido francis inicio

}

if (mediaMessage.mediatype === 'video' && !mediaMessage.fileName) {
mediaMessage.fileName = 'video.mp4';
// inserido francis inicio
mimetype = 'video/mp4';
// inserido francis final
}

let mimetype: string;
// ocultado francis inicio
// let mimetype: string;

if (isURL(mediaMessage.media)) {
mimetype = getMIMEType(mediaMessage.media);
} else {
mimetype = getMIMEType(mediaMessage.fileName);
}

// if (isURL(mediaMessage.media)) {
// mimetype = getMIMEType(mediaMessage.media);
// } else {
// mimetype = getMIMEType(mediaMessage.fileName);
// }
// ocultado francis final

this.logger.verbose('Mimetype: ' + mimetype);

Expand Down

0 comments on commit bddd640

Please sign in to comment.