Compare commits

..

18 Commits
1.4.2 ... 1.4.4

Author SHA1 Message Date
Davidson Gomes
6b926dc697 Merge branch 'release/1.4.4' 2023-07-25 16:19:54 -03:00
Davidson Gomes
ecae077c6d fix: Option conversation_pending in chatwoot endpoint 2023-07-25 16:16:49 -03:00
Davidson Gomes
78ab1bed35 Merge tag '1.4.4' into develop
* Fixed chatwoot line wrap issue
* Solved receive location in chatwoot
* When requesting the pairing code, it also brings the qr code
* Option reopen_conversation in chatwoot endpoint
* Option conversation_pending in chatwoot endpoint
2023-07-25 15:31:16 -03:00
Davidson Gomes
2b6dbfde6b Merge branch 'release/1.4.4' 2023-07-25 15:31:06 -03:00
Davidson Gomes
6bb1abd7f0 fix: Option conversation_pending in chatwoot endpoint 2023-07-25 15:29:42 -03:00
Davidson Gomes
aef92240cc fix: Option conversation_pending in chatwoot endpoint 2023-07-25 15:20:21 -03:00
Davidson Gomes
f0d8c2d095 fix: Solved receive location in chatwoot 2023-07-25 13:19:15 -03:00
Davidson Gomes
14529f2c35 fix: When requesting the pairing code, it also brings the qr code 2023-07-25 12:47:35 -03:00
Davidson Gomes
89f40d54d9 fix: Solved receive location in chatwoot 2023-07-25 12:41:54 -03:00
Davidson Gomes
4c006970a2 fix: Fixed chatwoot line wrap issue 2023-07-25 11:52:26 -03:00
Davidson Gomes
de676041df Merge tag '1.4.3' into develop
* Adjusts in settings with options always_online, read_messages and read_status
* Fixed send webhook for event CALL
* Create instance with settings
2023-07-25 10:51:53 -03:00
Davidson Gomes
82b1567ae5 Merge branch 'release/1.4.3' 2023-07-25 10:51:42 -03:00
Davidson Gomes
1cd7291068 version: 1.4.3 2023-07-25 10:51:28 -03:00
Davidson Gomes
fdee1df5b3 fix: Create instance with settings 2023-07-25 10:42:45 -03:00
Davidson Gomes
c314d00ccd fix: Create instance with settings 2023-07-25 10:42:34 -03:00
Davidson Gomes
62e2a8a6e3 fix: Fixed send webhook for event CALL 2023-07-25 10:23:18 -03:00
Davidson Gomes
a12231a0aa fix: Adjusts in settings with options always_online, read_messages and read_status 2023-07-25 09:57:28 -03:00
Davidson Gomes
183efd427a Merge tag '1.4.2' into develop
* Fixed validation is set settings
* Adjusts in group validations
* Ajusts in sticker message to chatwoot
2023-07-24 20:52:58 -03:00
19 changed files with 252 additions and 33 deletions

View File

@@ -1,3 +1,21 @@
# 1.4.4 (2023-07-25 15:24)
### Fixed
* Fixed chatwoot line wrap issue
* Solved receive location in chatwoot
* When requesting the pairing code, it also brings the qr code
* Option reopen_conversation in chatwoot endpoint
* Option conversation_pending in chatwoot endpoint
# 1.4.3 (2023-07-25 10:51)
### Fixed
* Adjusts in settings with options always_online, read_messages and read_status
* Fixed send webhook for event CALL
* Create instance with settings
# 1.4.2 (2023-07-24 20:52)
### Fixed

View File

@@ -73,6 +73,7 @@ WEBHOOK_EVENTS_GROUPS_UPSERT=true
WEBHOOK_EVENTS_GROUPS_UPDATE=true
WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
WEBHOOK_EVENTS_CONNECTION_UPDATE=true
WEBHOOK_EVENTS_CALL=true
# This event fires every time a new token is requested via the refresh route
WEBHOOK_EVENTS_NEW_JWT_TOKEN=false

View File

@@ -74,6 +74,7 @@ ENV WEBHOOK_EVENTS_GROUPS_UPSERT=true
ENV WEBHOOK_EVENTS_GROUPS_UPDATE=true
ENV WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
ENV WEBHOOK_EVENTS_CONNECTION_UPDATE=true
ENV WEBHOOK_EVENTS_CALL=true
ENV WEBHOOK_EVENTS_NEW_JWT_TOKEN=false

View File

@@ -1,6 +1,6 @@
{
"name": "evolution-api",
"version": "1.4.2",
"version": "1.4.4",
"description": "Rest api for communication with WhatsApp",
"main": "./dist/src/main.js",
"scripts": {

View File

@@ -89,6 +89,7 @@ export type EventsWebhook = {
GROUPS_UPSERT: boolean;
GROUP_UPDATE: boolean;
GROUP_PARTICIPANTS_UPDATE: boolean;
CALL: boolean;
NEW_JWT_TOKEN: boolean;
};
@@ -245,6 +246,7 @@ export class ConfigService {
GROUP_UPDATE: process.env?.WEBHOOK_EVENTS_GROUPS_UPDATE === 'true',
GROUP_PARTICIPANTS_UPDATE:
process.env?.WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE === 'true',
CALL: process.env?.WEBHOOK_EVENTS_CALL === 'true',
NEW_JWT_TOKEN: process.env?.WEBHOOK_EVENTS_NEW_JWT_TOKEN === 'true',
},
},

View File

@@ -110,6 +110,7 @@ WEBHOOK:
GROUP_UPDATE: true
GROUP_PARTICIPANTS_UPDATE: true
CONNECTION_UPDATE: true
CALL: true
# This event fires every time a new token is requested via the refresh route
NEW_JWT_TOKEN: false

View File

@@ -53,6 +53,7 @@ export const instanceNameSchema: JSONSchema7 = {
'GROUP_UPDATE',
'GROUP_PARTICIPANTS_UPDATE',
'CONNECTION_UPDATE',
'CALL',
'NEW_JWT_TOKEN',
],
},
@@ -455,7 +456,7 @@ export const readMessageSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
readMessages: {
read_messages: {
type: 'array',
minItems: 1,
uniqueItems: true,
@@ -470,7 +471,7 @@ export const readMessageSchema: JSONSchema7 = {
},
},
},
required: ['readMessages'],
required: ['read_messages'],
};
export const privacySettingsSchema: JSONSchema7 = {
@@ -854,6 +855,7 @@ export const webhookSchema: JSONSchema7 = {
'GROUP_UPDATE',
'GROUP_PARTICIPANTS_UPDATE',
'CONNECTION_UPDATE',
'CALL',
'NEW_JWT_TOKEN',
],
},
@@ -872,9 +874,26 @@ export const chatwootSchema: JSONSchema7 = {
token: { type: 'string' },
url: { type: 'string' },
sign_msg: { type: 'boolean', enum: [true, false] },
reopen_conversation: { type: 'boolean', enum: [true, false] },
conversation_pending: { type: 'boolean', enum: [true, false] },
},
required: ['enabled', 'account_id', 'token', 'url', 'sign_msg'],
...isNotEmpty('account_id', 'token', 'url', 'sign_msg'),
required: [
'enabled',
'account_id',
'token',
'url',
'sign_msg',
'reopen_conversation',
'conversation_pending',
],
...isNotEmpty(
'account_id',
'token',
'url',
'sign_msg',
'reopen_conversation',
'conversation_pending',
),
};
export const settingsSchema: JSONSchema7 = {
@@ -884,7 +903,22 @@ export const settingsSchema: JSONSchema7 = {
reject_call: { type: 'boolean', enum: [true, false] },
msg_call: { type: 'string' },
groups_ignore: { type: 'boolean', enum: [true, false] },
always_online: { type: 'boolean', enum: [true, false] },
read_messages: { type: 'boolean', enum: [true, false] },
read_status: { type: 'boolean', enum: [true, false] },
},
required: ['reject_call'],
...isNotEmpty('reject_call'),
required: [
'reject_call',
'groups_ignore',
'always_online',
'read_messages',
'read_status',
],
...isNotEmpty(
'reject_call',
'groups_ignore',
'always_online',
'read_messages',
'read_status',
),
};

View File

@@ -44,6 +44,8 @@ export class ChatwootController {
data.token = '';
data.url = '';
data.sign_msg = false;
data.reopen_conversation = false;
data.conversation_pending = false;
}
data.name_inbox = instance.instanceName;

View File

@@ -13,6 +13,7 @@ import { Logger } from '../../config/logger.config';
import { wa } from '../types/wa.types';
import { RedisCache } from '../../db/redis.client';
import { isURL } from 'class-validator';
import { SettingsService } from '../services/settings.service';
export class InstanceController {
constructor(
@@ -23,6 +24,7 @@ export class InstanceController {
private readonly authService: AuthService,
private readonly webhookService: WebhookService,
private readonly chatwootService: ChatwootService,
private readonly settingsService: SettingsService,
private readonly cache: RedisCache,
) {}
@@ -40,6 +42,14 @@ export class InstanceController {
chatwoot_token,
chatwoot_url,
chatwoot_sign_msg,
chatwoot_reopen_conversation,
chatwoot_conversation_pending,
reject_call,
msg_call,
groups_ignore,
always_online,
read_messages,
read_status,
}: InstanceDto) {
try {
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
@@ -102,6 +112,20 @@ export class InstanceController {
}
}
this.logger.verbose('creating settings');
const settings: wa.LocalSettings = {
reject_call: reject_call || false,
msg_call: msg_call || '',
groups_ignore: groups_ignore || false,
always_online: always_online || false,
read_messages: read_messages || false,
read_status: read_status || false,
};
this.logger.verbose('settings: ' + JSON.stringify(settings));
this.settingsService.create(instance, settings);
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
let getQrcode: wa.QrCode;
@@ -121,6 +145,7 @@ export class InstanceController {
webhook,
webhook_by_events,
events: getEvents,
settings,
qrcode: getQrcode,
};
@@ -146,6 +171,24 @@ export class InstanceController {
throw new BadRequestException('Invalid "url" property in chatwoot');
}
if (chatwoot_sign_msg !== true && chatwoot_sign_msg !== false) {
throw new BadRequestException('sign_msg is required');
}
if (
chatwoot_reopen_conversation !== true &&
chatwoot_reopen_conversation !== false
) {
throw new BadRequestException('reopen_conversation is required');
}
if (
chatwoot_conversation_pending !== true &&
chatwoot_conversation_pending !== false
) {
throw new BadRequestException('conversation_pending is required');
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
try {
@@ -157,6 +200,8 @@ export class InstanceController {
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName,
number,
reopen_conversation: chatwoot_reopen_conversation || false,
conversation_pending: chatwoot_conversation_pending || false,
});
this.chatwootService.initInstanceChatwoot(
@@ -179,12 +224,15 @@ export class InstanceController {
webhook,
webhook_by_events,
events: getEvents,
settings,
chatwoot: {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
reopen_conversation: chatwoot_reopen_conversation || false,
conversation_pending: chatwoot_conversation_pending || false,
number,
name_inbox: instance.instanceName,
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,

View File

@@ -59,7 +59,7 @@ class Key {
remoteJid: string;
}
export class ReadMessageDto {
readMessages: Key[];
read_messages: Key[];
}
class LastMessage {

View File

@@ -6,4 +6,6 @@ export class ChatwootDto {
name_inbox?: string;
sign_msg?: boolean;
number?: string;
reopen_conversation?: boolean;
conversation_pending?: boolean;
}

View File

@@ -1,13 +1,21 @@
export class InstanceDto {
instanceName: string;
webhook?: string;
webhook_by_events?: boolean;
events?: string[];
qrcode?: boolean;
number?: string;
token?: string;
webhook?: string;
webhook_by_events?: boolean;
events?: string[];
reject_call?: boolean;
msg_call?: string;
groups_ignore?: boolean;
always_online?: boolean;
read_messages?: boolean;
read_status?: boolean;
chatwoot_account_id?: string;
chatwoot_token?: string;
chatwoot_url?: string;
chatwoot_sign_msg?: boolean;
chatwoot_reopen_conversation?: boolean;
chatwoot_conversation_pending?: boolean;
}

View File

@@ -2,4 +2,7 @@ export class SettingsDto {
reject_call?: boolean;
msg_call?: string;
groups_ignore?: boolean;
always_online?: boolean;
read_messages?: boolean;
read_status?: boolean;
}

View File

@@ -10,6 +10,8 @@ export class ChatwootRaw {
name_inbox?: string;
sign_msg?: boolean;
number?: string;
reopen_conversation?: boolean;
conversation_pending?: boolean;
}
const chatwootSchema = new Schema<ChatwootRaw>({

View File

@@ -6,6 +6,9 @@ export class SettingsRaw {
reject_call?: boolean;
msg_call?: string;
groups_ignore?: boolean;
always_online?: boolean;
read_messages?: boolean;
read_status?: boolean;
}
const settingsSchema = new Schema<SettingsRaw>({
@@ -13,6 +16,9 @@ const settingsSchema = new Schema<SettingsRaw>({
reject_call: { type: Boolean, required: true },
msg_call: { type: String, required: true },
groups_ignore: { type: Boolean, required: true },
always_online: { type: Boolean, required: true },
read_messages: { type: Boolean, required: true },
read_status: { type: Boolean, required: true },
});
export const SettingsModel = dbserver?.model(

View File

@@ -13,6 +13,7 @@ import { SendAudioDto } from '../dto/sendMessage.dto';
import { SendMediaDto } from '../dto/sendMessage.dto';
import { ROOT_DIR } from '../../config/path.config';
import { ConfigService, HttpServer } from '../../config/env.config';
import { type } from 'os';
export class ChatwootService {
private messageCacheFile: string;
@@ -230,12 +231,19 @@ export class ChatwootService {
if (qrcode) {
this.logger.verbose('create conversation in chatwoot');
const data = {
contact_id: contactId.toString(),
inbox_id: inboxId.toString(),
};
if (this.provider.conversation_pending) {
data['status'] = 'pending';
}
console.log('this.provider', this.provider);
const conversation = await client.conversations.create({
accountId: this.provider.account_id,
data: {
contact_id: contactId.toString(),
inbox_id: inboxId.toString(),
},
data,
});
if (!conversation) {
@@ -520,11 +528,20 @@ export class ChatwootService {
})) as any;
if (contactConversations) {
let conversation: any;
if (this.provider.reopen_conversation) {
conversation = contactConversations.payload.find(
(conversation) => conversation.inbox_id == filterInbox.id,
);
} else {
conversation = contactConversations.payload.find(
(conversation) =>
conversation.status !== 'resolved' &&
conversation.inbox_id == filterInbox.id,
);
}
this.logger.verbose('return conversation if exists');
const conversation = contactConversations.payload.find(
(conversation) =>
conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id,
);
if (conversation) {
this.logger.verbose('conversation found');
return conversation.id;
@@ -532,12 +549,18 @@ export class ChatwootService {
}
this.logger.verbose('create conversation in chatwoot');
const data = {
contact_id: contactId.toString(),
inbox_id: filterInbox.id.toString(),
};
if (this.provider.conversation_pending) {
data['status'] = 'pending';
}
const conversation = await client.conversations.create({
accountId: this.provider.account_id,
data: {
contact_id: `${contactId}`,
inbox_id: `${filterInbox.id}`,
},
data,
});
if (!conversation) {
@@ -1125,12 +1148,12 @@ export class ChatwootService {
}
if (body.message_type === 'template' && body.event === 'message_created') {
this.logger.verbose('check if is csat');
this.logger.verbose('check if is template');
const data: SendTextDto = {
number: chatId,
textMessage: {
text: body.content,
text: body.content.replace(/\\\r\n|\\\n|\n/g, '\n'),
},
options: {
delay: 1200,
@@ -1186,6 +1209,11 @@ export class ChatwootService {
audioMessage: msg.audioMessage?.caption,
contactMessage: msg.contactMessage?.vcard,
contactsArrayMessage: msg.contactsArrayMessage,
locationMessage: msg.locationMessage
? msg.locationMessage?.degreesLatitude +
',' +
msg.locationMessage?.degreesLongitude
: undefined,
};
this.logger.verbose('type message: ' + types);
@@ -1199,6 +1227,20 @@ export class ChatwootService {
const result = typeKey ? types[typeKey] : undefined;
if (typeKey === 'locationMessage') {
const [latitude, longitude] = result.split(',');
const formattedLocation = `**Location:**
**latitude:** ${latitude}
**longitude:** ${longitude}
https://www.google.com/maps/search/?api=1&query=${latitude},${longitude}
`;
this.logger.verbose('message content: ' + formattedLocation);
return formattedLocation;
}
if (typeKey === 'contactMessage') {
const vCardData = result.split('\n');
const contactInfo = {};

View File

@@ -242,13 +242,9 @@ export class WAStartupService {
public get qrCode(): wa.QrCode {
this.logger.verbose('Getting qrcode');
if (this.instance.qrcode?.pairingCode) {
return {
pairingCode: this.instance.qrcode?.pairingCode,
};
}
return {
pairingCode: this.instance.qrcode?.pairingCode,
code: this.instance.qrcode?.code,
base64: this.instance.qrcode?.base64,
};
@@ -316,6 +312,19 @@ export class WAStartupService {
this.localChatwoot.sign_msg = data?.sign_msg;
this.logger.verbose(`Chatwoot sign msg: ${this.localChatwoot.sign_msg}`);
this.localChatwoot.number = data?.number;
this.logger.verbose(`Chatwoot number: ${this.localChatwoot.number}`);
this.localChatwoot.reopen_conversation = data?.reopen_conversation;
this.logger.verbose(
`Chatwoot reopen conversation: ${this.localChatwoot.reopen_conversation}`,
);
this.localChatwoot.conversation_pending = data?.conversation_pending;
this.logger.verbose(
`Chatwoot conversation pending: ${this.localChatwoot.conversation_pending}`,
);
this.logger.verbose('Chatwoot loaded');
}
@@ -327,6 +336,8 @@ export class WAStartupService {
this.logger.verbose(`Chatwoot url: ${data.url}`);
this.logger.verbose(`Chatwoot inbox name: ${data.name_inbox}`);
this.logger.verbose(`Chatwoot sign msg: ${data.sign_msg}`);
this.logger.verbose(`Chatwoot reopen conversation: ${data.reopen_conversation}`);
this.logger.verbose(`Chatwoot conversation pending: ${data.conversation_pending}`);
Object.assign(this.localChatwoot, data);
this.logger.verbose('Chatwoot set');
@@ -346,6 +357,8 @@ export class WAStartupService {
this.logger.verbose(`Chatwoot url: ${data.url}`);
this.logger.verbose(`Chatwoot inbox name: ${data.name_inbox}`);
this.logger.verbose(`Chatwoot sign msg: ${data.sign_msg}`);
this.logger.verbose(`Chatwoot reopen conversation: ${data.reopen_conversation}`);
this.logger.verbose(`Chatwoot conversation pending: ${data.conversation_pending}`);
return data;
}
@@ -362,6 +375,15 @@ export class WAStartupService {
this.localSettings.groups_ignore = data?.groups_ignore;
this.logger.verbose(`Settings groups_ignore: ${this.localSettings.groups_ignore}`);
this.localSettings.always_online = data?.always_online;
this.logger.verbose(`Settings always_online: ${this.localSettings.always_online}`);
this.localSettings.read_messages = data?.read_messages;
this.logger.verbose(`Settings read_messages: ${this.localSettings.read_messages}`);
this.localSettings.read_status = data?.read_status;
this.logger.verbose(`Settings read_status: ${this.localSettings.read_status}`);
this.logger.verbose('Settings loaded');
}
@@ -371,8 +393,13 @@ export class WAStartupService {
this.logger.verbose(`Settings reject_call: ${data.reject_call}`);
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
this.logger.verbose(`Settings always_online: ${data.always_online}`);
this.logger.verbose(`Settings read_messages: ${data.read_messages}`);
this.logger.verbose(`Settings read_status: ${data.read_status}`);
Object.assign(this.localSettings, data);
this.logger.verbose('Settings set');
this.client?.ws?.close();
}
public async findSettings() {
@@ -387,6 +414,9 @@ export class WAStartupService {
this.logger.verbose(`Settings url: ${data.reject_call}`);
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
this.logger.verbose(`Settings always_online: ${data.always_online}`);
this.logger.verbose(`Settings read_messages: ${data.read_messages}`);
this.logger.verbose(`Settings read_status: ${data.read_status}`);
return data;
}
@@ -605,7 +635,6 @@ export class WAStartupService {
color: { light: '#ffffff', dark: '#198754' },
};
console.log(this.phoneNumber);
if (this.phoneNumber) {
await delay(2000);
this.instance.qrcode.pairingCode = await this.client.requestPairingCode(
@@ -847,6 +876,7 @@ export class WAStartupService {
printQRInTerminal: false,
browser,
version,
markOnlineOnConnect: this.localSettings.always_online,
connectTimeoutMs: 60_000,
qrTimeout: 40_000,
defaultQueryTimeoutMs: undefined,
@@ -1143,6 +1173,14 @@ export class WAStartupService {
source: getDevice(received.key.id),
};
if (this.localSettings.read_messages && received.key.id !== 'status@broadcast') {
await this.client.readMessages([received.key]);
}
if (this.localSettings.read_status && received.key.id === 'status@broadcast') {
await this.client.readMessages([received.key]);
}
this.logger.log(messageRaw);
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
@@ -1362,11 +1400,15 @@ export class WAStartupService {
text: settings.msg_call,
});
this.logger.verbose('Sending data to event messages.upsert');
this.client.ev.emit('messages.upsert', {
messages: [msg],
type: 'notify',
});
}
this.logger.verbose('Sending data to webhook in event CALL');
this.sendDataWebhook(Events.CALL, call);
}
if (events['connection.update']) {
@@ -2400,7 +2442,7 @@ export class WAStartupService {
this.logger.verbose('Marking message as read');
try {
const keys: proto.IMessageKey[] = [];
data.readMessages.forEach((read) => {
data.read_messages.forEach((read) => {
if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) {
keys.push({
remoteJid: read.remoteJid,
@@ -2640,7 +2682,6 @@ export class WAStartupService {
await this.client.updateGroupsAddPrivacy(settings.privacySettings.groupadd);
this.logger.verbose('Groups add privacy updated');
// reinicia a instancia
this.client?.ws?.close();
return {

View File

@@ -22,6 +22,7 @@ export enum Events {
GROUPS_UPSERT = 'groups.upsert',
GROUPS_UPDATE = 'groups.update',
GROUP_PARTICIPANTS_UPDATE = 'group-participants.update',
CALL = 'call',
}
export declare namespace wa {
@@ -55,12 +56,18 @@ export declare namespace wa {
url?: string;
name_inbox?: string;
sign_msg?: boolean;
number?: string;
reopen_conversation?: boolean;
conversation_pending?: boolean;
};
export type LocalSettings = {
reject_call?: boolean;
msg_call?: string;
groups_ignore?: boolean;
always_online?: boolean;
read_messages?: boolean;
read_status?: boolean;
};
export type StateConnection = {

View File

@@ -94,6 +94,7 @@ export const instanceController = new InstanceController(
authService,
webhookService,
chatwootService,
settingsService,
cache,
);
export const viewsController = new ViewsController(waMonitor, configService);