Skip to content

Commit

Permalink
Merge branch 'release/1.4.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Jul 25, 2023
2 parents f95f312 + 1cd7291 commit 82b1567
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 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
Expand Down
1 change: 1 addition & 0 deletions Docker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "evolution-api",
"version": "1.4.2",
"version": "1.4.3",
"description": "Rest api for communication with WhatsApp",
"main": "./dist/src/main.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export type EventsWebhook = {
GROUPS_UPSERT: boolean;
GROUP_UPDATE: boolean;
GROUP_PARTICIPANTS_UPDATE: boolean;
CALL: boolean;
NEW_JWT_TOKEN: boolean;
};

Expand Down Expand Up @@ -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',
},
},
Expand Down
1 change: 1 addition & 0 deletions src/dev-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 22 additions & 5 deletions src/validate/validate.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const instanceNameSchema: JSONSchema7 = {
'GROUP_UPDATE',
'GROUP_PARTICIPANTS_UPDATE',
'CONNECTION_UPDATE',
'CALL',
'NEW_JWT_TOKEN',
],
},
Expand Down Expand Up @@ -455,7 +456,7 @@ export const readMessageSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
readMessages: {
read_messages: {
type: 'array',
minItems: 1,
uniqueItems: true,
Expand All @@ -470,7 +471,7 @@ export const readMessageSchema: JSONSchema7 = {
},
},
},
required: ['readMessages'],
required: ['read_messages'],
};

export const privacySettingsSchema: JSONSchema7 = {
Expand Down Expand Up @@ -854,6 +855,7 @@ export const webhookSchema: JSONSchema7 = {
'GROUP_UPDATE',
'GROUP_PARTICIPANTS_UPDATE',
'CONNECTION_UPDATE',
'CALL',
'NEW_JWT_TOKEN',
],
},
Expand Down Expand Up @@ -884,7 +886,22 @@ export const settingsSchema: JSONSchema7 = {
reject_call: { type: 'boolean', enum: [true, false] },
msg_call: { type: 'string' },
groups_ignore: { type: 'boolean', enum: [true, false] },
},
required: ['reject_call'],
...isNotEmpty('reject_call'),
always_online: { type: 'boolean', enum: [true, false] },
read_messages: { type: 'boolean', enum: [true, false] },
read_status: { type: 'boolean', enum: [true, false] },
},
required: [
'reject_call',
'groups_ignore',
'always_online',
'read_messages',
'read_status',
],
...isNotEmpty(
'reject_call',
'groups_ignore',
'always_online',
'read_messages',
'read_status',
),
};
24 changes: 24 additions & 0 deletions src/whatsapp/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
) {}

Expand All @@ -40,6 +42,12 @@ export class InstanceController {
chatwoot_token,
chatwoot_url,
chatwoot_sign_msg,
reject_call,
msg_call,
groups_ignore,
always_online,
read_messages,
read_status,
}: InstanceDto) {
try {
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
Expand Down Expand Up @@ -102,6 +110,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;

Expand All @@ -121,6 +143,7 @@ export class InstanceController {
webhook,
webhook_by_events,
events: getEvents,
settings,
qrcode: getQrcode,
};

Expand Down Expand Up @@ -179,6 +202,7 @@ export class InstanceController {
webhook,
webhook_by_events,
events: getEvents,
settings,
chatwoot: {
enabled: true,
account_id: chatwoot_account_id,
Expand Down
2 changes: 1 addition & 1 deletion src/whatsapp/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Key {
remoteJid: string;
}
export class ReadMessageDto {
readMessages: Key[];
read_messages: Key[];
}

class LastMessage {
Expand Down
12 changes: 9 additions & 3 deletions src/whatsapp/dto/instance.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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;
Expand Down
3 changes: 3 additions & 0 deletions src/whatsapp/dto/settings.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions src/whatsapp/models/settings.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ 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>({
_id: { type: String, _id: true },
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(
Expand Down
33 changes: 31 additions & 2 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,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');
}

Expand All @@ -371,8 +380,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() {
Expand All @@ -387,6 +401,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;
}

Expand Down Expand Up @@ -847,6 +864,7 @@ export class WAStartupService {
printQRInTerminal: false,
browser,
version,
markOnlineOnConnect: this.localSettings.always_online,
connectTimeoutMs: 60_000,
qrTimeout: 40_000,
defaultQueryTimeoutMs: undefined,
Expand Down Expand Up @@ -1143,6 +1161,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');
Expand Down Expand Up @@ -1362,11 +1388,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']) {
Expand Down Expand Up @@ -2400,7 +2430,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,
Expand Down Expand Up @@ -2640,7 +2670,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 {
Expand Down
4 changes: 4 additions & 0 deletions src/whatsapp/types/wa.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -61,6 +62,9 @@ export declare namespace wa {
reject_call?: boolean;
msg_call?: string;
groups_ignore?: boolean;
always_online?: boolean;
read_messages?: boolean;
read_status?: boolean;
};

export type StateConnection = {
Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/whatsapp.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const instanceController = new InstanceController(
authService,
webhookService,
chatwootService,
settingsService,
cache,
);
export const viewsController = new ViewsController(waMonitor, configService);
Expand Down

0 comments on commit 82b1567

Please sign in to comment.