mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-20 04:12:23 -06:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
82b1567ae5 | ||
|
|
1cd7291068 | ||
|
|
fdee1df5b3 | ||
|
|
c314d00ccd | ||
|
|
62e2a8a6e3 | ||
|
|
a12231a0aa | ||
|
|
183efd427a | ||
|
|
f95f3126c3 | ||
|
|
4d9ca4b451 | ||
|
|
c76334a68a | ||
|
|
f475391ba6 | ||
|
|
b77f22790b | ||
|
|
757a578c6e | ||
|
|
c5824767c8 | ||
|
|
84f3f07279 | ||
|
|
f8e1892eee | ||
|
|
036a8edca0 | ||
|
|
58ed6f395f | ||
|
|
ef4be6a612 | ||
|
|
3d8e6f4394 | ||
|
|
0cc1f18a7e | ||
|
|
8b6e577b8f |
16
CHANGELOG.md
16
CHANGELOG.md
@@ -1,3 +1,19 @@
|
|||||||
|
# 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
|
||||||
|
|
||||||
|
* Fixed validation is set settings
|
||||||
|
* Adjusts in group validations
|
||||||
|
* Ajusts in sticker message to chatwoot
|
||||||
|
|
||||||
# 1.4.1 (2023-07-24 18:28)
|
# 1.4.1 (2023-07-24 18:28)
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ WEBHOOK_EVENTS_GROUPS_UPSERT=true
|
|||||||
WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
||||||
WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
||||||
WEBHOOK_EVENTS_CONNECTION_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
|
# This event fires every time a new token is requested via the refresh route
|
||||||
WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
||||||
|
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ ENV WEBHOOK_EVENTS_GROUPS_UPSERT=true
|
|||||||
ENV WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
ENV WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
||||||
ENV WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
ENV WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
||||||
ENV WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
ENV WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
||||||
|
ENV WEBHOOK_EVENTS_CALL=true
|
||||||
|
|
||||||
ENV WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
ENV WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "evolution-api",
|
"name": "evolution-api",
|
||||||
"version": "1.4.1",
|
"version": "1.4.3",
|
||||||
"description": "Rest api for communication with WhatsApp",
|
"description": "Rest api for communication with WhatsApp",
|
||||||
"main": "./dist/src/main.js",
|
"main": "./dist/src/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ export type EventsWebhook = {
|
|||||||
GROUPS_UPSERT: boolean;
|
GROUPS_UPSERT: boolean;
|
||||||
GROUP_UPDATE: boolean;
|
GROUP_UPDATE: boolean;
|
||||||
GROUP_PARTICIPANTS_UPDATE: boolean;
|
GROUP_PARTICIPANTS_UPDATE: boolean;
|
||||||
|
CALL: boolean;
|
||||||
NEW_JWT_TOKEN: boolean;
|
NEW_JWT_TOKEN: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -245,6 +246,7 @@ export class ConfigService {
|
|||||||
GROUP_UPDATE: process.env?.WEBHOOK_EVENTS_GROUPS_UPDATE === 'true',
|
GROUP_UPDATE: process.env?.WEBHOOK_EVENTS_GROUPS_UPDATE === 'true',
|
||||||
GROUP_PARTICIPANTS_UPDATE:
|
GROUP_PARTICIPANTS_UPDATE:
|
||||||
process.env?.WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE === 'true',
|
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',
|
NEW_JWT_TOKEN: process.env?.WEBHOOK_EVENTS_NEW_JWT_TOKEN === 'true',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -110,6 +110,7 @@ WEBHOOK:
|
|||||||
GROUP_UPDATE: true
|
GROUP_UPDATE: true
|
||||||
GROUP_PARTICIPANTS_UPDATE: true
|
GROUP_PARTICIPANTS_UPDATE: true
|
||||||
CONNECTION_UPDATE: true
|
CONNECTION_UPDATE: true
|
||||||
|
CALL: true
|
||||||
# This event fires every time a new token is requested via the refresh route
|
# This event fires every time a new token is requested via the refresh route
|
||||||
NEW_JWT_TOKEN: false
|
NEW_JWT_TOKEN: false
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ export const instanceNameSchema: JSONSchema7 = {
|
|||||||
'GROUP_UPDATE',
|
'GROUP_UPDATE',
|
||||||
'GROUP_PARTICIPANTS_UPDATE',
|
'GROUP_PARTICIPANTS_UPDATE',
|
||||||
'CONNECTION_UPDATE',
|
'CONNECTION_UPDATE',
|
||||||
|
'CALL',
|
||||||
'NEW_JWT_TOKEN',
|
'NEW_JWT_TOKEN',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -455,7 +456,7 @@ export const readMessageSchema: JSONSchema7 = {
|
|||||||
$id: v4(),
|
$id: v4(),
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
readMessages: {
|
read_messages: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
minItems: 1,
|
minItems: 1,
|
||||||
uniqueItems: true,
|
uniqueItems: true,
|
||||||
@@ -470,7 +471,7 @@ export const readMessageSchema: JSONSchema7 = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
required: ['readMessages'],
|
required: ['read_messages'],
|
||||||
};
|
};
|
||||||
|
|
||||||
export const privacySettingsSchema: JSONSchema7 = {
|
export const privacySettingsSchema: JSONSchema7 = {
|
||||||
@@ -667,6 +668,7 @@ export const createGroupSchema: JSONSchema7 = {
|
|||||||
subject: { type: 'string' },
|
subject: { type: 'string' },
|
||||||
description: { type: 'string' },
|
description: { type: 'string' },
|
||||||
profilePicture: { type: 'string' },
|
profilePicture: { type: 'string' },
|
||||||
|
promoteParticipants: { type: 'boolean', enum: [true, false] },
|
||||||
participants: {
|
participants: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
minItems: 1,
|
minItems: 1,
|
||||||
@@ -853,6 +855,7 @@ export const webhookSchema: JSONSchema7 = {
|
|||||||
'GROUP_UPDATE',
|
'GROUP_UPDATE',
|
||||||
'GROUP_PARTICIPANTS_UPDATE',
|
'GROUP_PARTICIPANTS_UPDATE',
|
||||||
'CONNECTION_UPDATE',
|
'CONNECTION_UPDATE',
|
||||||
|
'CALL',
|
||||||
'NEW_JWT_TOKEN',
|
'NEW_JWT_TOKEN',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -883,7 +886,22 @@ export const settingsSchema: JSONSchema7 = {
|
|||||||
reject_call: { type: 'boolean', enum: [true, false] },
|
reject_call: { type: 'boolean', enum: [true, false] },
|
||||||
msg_call: { type: 'string' },
|
msg_call: { type: 'string' },
|
||||||
groups_ignore: { type: 'boolean', enum: [true, false] },
|
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'],
|
required: [
|
||||||
...isNotEmpty('reject_call'),
|
'reject_call',
|
||||||
|
'groups_ignore',
|
||||||
|
'always_online',
|
||||||
|
'read_messages',
|
||||||
|
'read_status',
|
||||||
|
],
|
||||||
|
...isNotEmpty(
|
||||||
|
'reject_call',
|
||||||
|
'groups_ignore',
|
||||||
|
'always_online',
|
||||||
|
'read_messages',
|
||||||
|
'read_status',
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import { Logger } from '../../config/logger.config';
|
|||||||
import { wa } from '../types/wa.types';
|
import { wa } from '../types/wa.types';
|
||||||
import { RedisCache } from '../../db/redis.client';
|
import { RedisCache } from '../../db/redis.client';
|
||||||
import { isURL } from 'class-validator';
|
import { isURL } from 'class-validator';
|
||||||
|
import { SettingsService } from '../services/settings.service';
|
||||||
|
|
||||||
export class InstanceController {
|
export class InstanceController {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -23,6 +24,7 @@ export class InstanceController {
|
|||||||
private readonly authService: AuthService,
|
private readonly authService: AuthService,
|
||||||
private readonly webhookService: WebhookService,
|
private readonly webhookService: WebhookService,
|
||||||
private readonly chatwootService: ChatwootService,
|
private readonly chatwootService: ChatwootService,
|
||||||
|
private readonly settingsService: SettingsService,
|
||||||
private readonly cache: RedisCache,
|
private readonly cache: RedisCache,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -40,78 +42,158 @@ export class InstanceController {
|
|||||||
chatwoot_token,
|
chatwoot_token,
|
||||||
chatwoot_url,
|
chatwoot_url,
|
||||||
chatwoot_sign_msg,
|
chatwoot_sign_msg,
|
||||||
|
reject_call,
|
||||||
|
msg_call,
|
||||||
|
groups_ignore,
|
||||||
|
always_online,
|
||||||
|
read_messages,
|
||||||
|
read_status,
|
||||||
}: InstanceDto) {
|
}: InstanceDto) {
|
||||||
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
|
try {
|
||||||
|
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
|
||||||
|
|
||||||
if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) {
|
if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) {
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(
|
||||||
'The instance name must be lowercase and without special characters',
|
'The instance name must be lowercase and without special characters',
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.verbose('checking duplicate token');
|
|
||||||
await this.authService.checkDuplicateToken(token);
|
|
||||||
|
|
||||||
this.logger.verbose('creating instance');
|
|
||||||
const instance = new WAStartupService(
|
|
||||||
this.configService,
|
|
||||||
this.eventEmitter,
|
|
||||||
this.repository,
|
|
||||||
this.cache,
|
|
||||||
);
|
|
||||||
instance.instanceName = instanceName
|
|
||||||
.toLowerCase()
|
|
||||||
.replace(/[^a-z0-9]/g, '')
|
|
||||||
.replace(' ', '');
|
|
||||||
|
|
||||||
this.logger.verbose('instance: ' + instance.instanceName + ' created');
|
|
||||||
|
|
||||||
this.waMonitor.waInstances[instance.instanceName] = instance;
|
|
||||||
this.waMonitor.delInstanceTime(instance.instanceName);
|
|
||||||
|
|
||||||
this.logger.verbose('generating hash');
|
|
||||||
const hash = await this.authService.generateHash(
|
|
||||||
{
|
|
||||||
instanceName: instance.instanceName,
|
|
||||||
},
|
|
||||||
token,
|
|
||||||
);
|
|
||||||
|
|
||||||
this.logger.verbose('hash: ' + hash + ' generated');
|
|
||||||
|
|
||||||
let getEvents: string[];
|
|
||||||
|
|
||||||
if (webhook) {
|
|
||||||
if (!isURL(webhook, { require_tld: false })) {
|
|
||||||
throw new BadRequestException('Invalid "url" property in webhook');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('creating webhook');
|
this.logger.verbose('checking duplicate token');
|
||||||
try {
|
await this.authService.checkDuplicateToken(token);
|
||||||
this.webhookService.create(instance, {
|
|
||||||
enabled: true,
|
this.logger.verbose('creating instance');
|
||||||
url: webhook,
|
const instance = new WAStartupService(
|
||||||
events,
|
this.configService,
|
||||||
|
this.eventEmitter,
|
||||||
|
this.repository,
|
||||||
|
this.cache,
|
||||||
|
);
|
||||||
|
instance.instanceName = instanceName
|
||||||
|
.toLowerCase()
|
||||||
|
.replace(/[^a-z0-9]/g, '')
|
||||||
|
.replace(' ', '');
|
||||||
|
|
||||||
|
this.logger.verbose('instance: ' + instance.instanceName + ' created');
|
||||||
|
|
||||||
|
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||||
|
this.waMonitor.delInstanceTime(instance.instanceName);
|
||||||
|
|
||||||
|
this.logger.verbose('generating hash');
|
||||||
|
const hash = await this.authService.generateHash(
|
||||||
|
{
|
||||||
|
instanceName: instance.instanceName,
|
||||||
|
},
|
||||||
|
token,
|
||||||
|
);
|
||||||
|
|
||||||
|
this.logger.verbose('hash: ' + hash + ' generated');
|
||||||
|
|
||||||
|
let getEvents: string[];
|
||||||
|
|
||||||
|
if (webhook) {
|
||||||
|
if (!isURL(webhook, { require_tld: false })) {
|
||||||
|
throw new BadRequestException('Invalid "url" property in webhook');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('creating webhook');
|
||||||
|
try {
|
||||||
|
this.webhookService.create(instance, {
|
||||||
|
enabled: true,
|
||||||
|
url: webhook,
|
||||||
|
events,
|
||||||
|
webhook_by_events,
|
||||||
|
});
|
||||||
|
|
||||||
|
getEvents = (await this.webhookService.find(instance)).events;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (qrcode) {
|
||||||
|
this.logger.verbose('creating qrcode');
|
||||||
|
await instance.connectToWhatsapp(number);
|
||||||
|
await delay(5000);
|
||||||
|
getQrcode = instance.qrCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
instance: {
|
||||||
|
instanceName: instance.instanceName,
|
||||||
|
status: 'created',
|
||||||
|
},
|
||||||
|
hash,
|
||||||
|
webhook,
|
||||||
webhook_by_events,
|
webhook_by_events,
|
||||||
|
events: getEvents,
|
||||||
|
settings,
|
||||||
|
qrcode: getQrcode,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.logger.verbose('instance created');
|
||||||
|
this.logger.verbose(result);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!chatwoot_account_id) {
|
||||||
|
throw new BadRequestException('account_id is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!chatwoot_token) {
|
||||||
|
throw new BadRequestException('token is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!chatwoot_url) {
|
||||||
|
throw new BadRequestException('url is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isURL(chatwoot_url, { require_tld: false })) {
|
||||||
|
throw new BadRequestException('Invalid "url" property in chatwoot');
|
||||||
|
}
|
||||||
|
|
||||||
|
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.chatwootService.create(instance, {
|
||||||
|
enabled: true,
|
||||||
|
account_id: chatwoot_account_id,
|
||||||
|
token: chatwoot_token,
|
||||||
|
url: chatwoot_url,
|
||||||
|
sign_msg: chatwoot_sign_msg || false,
|
||||||
|
name_inbox: instance.instanceName,
|
||||||
|
number,
|
||||||
});
|
});
|
||||||
|
|
||||||
getEvents = (await this.webhookService.find(instance)).events;
|
this.chatwootService.initInstanceChatwoot(
|
||||||
|
instance,
|
||||||
|
instance.instanceName,
|
||||||
|
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||||
|
qrcode,
|
||||||
|
number,
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.log(error);
|
this.logger.log(error);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
|
return {
|
||||||
let getQrcode: wa.QrCode;
|
|
||||||
|
|
||||||
if (qrcode) {
|
|
||||||
this.logger.verbose('creating qrcode');
|
|
||||||
await instance.connectToWhatsapp(number);
|
|
||||||
await delay(5000);
|
|
||||||
getQrcode = instance.qrCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = {
|
|
||||||
instance: {
|
instance: {
|
||||||
instanceName: instance.instanceName,
|
instanceName: instance.instanceName,
|
||||||
status: 'created',
|
status: 'created',
|
||||||
@@ -120,75 +202,22 @@ export class InstanceController {
|
|||||||
webhook,
|
webhook,
|
||||||
webhook_by_events,
|
webhook_by_events,
|
||||||
events: getEvents,
|
events: getEvents,
|
||||||
qrcode: getQrcode,
|
settings,
|
||||||
|
chatwoot: {
|
||||||
|
enabled: true,
|
||||||
|
account_id: chatwoot_account_id,
|
||||||
|
token: chatwoot_token,
|
||||||
|
url: chatwoot_url,
|
||||||
|
sign_msg: chatwoot_sign_msg || false,
|
||||||
|
number,
|
||||||
|
name_inbox: instance.instanceName,
|
||||||
|
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
this.logger.verbose('instance created');
|
|
||||||
this.logger.verbose(result);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!chatwoot_account_id) {
|
|
||||||
throw new BadRequestException('account_id is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!chatwoot_token) {
|
|
||||||
throw new BadRequestException('token is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!chatwoot_url) {
|
|
||||||
throw new BadRequestException('url is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isURL(chatwoot_url, { require_tld: false })) {
|
|
||||||
throw new BadRequestException('Invalid "url" property in chatwoot');
|
|
||||||
}
|
|
||||||
|
|
||||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.chatwootService.create(instance, {
|
|
||||||
enabled: true,
|
|
||||||
account_id: chatwoot_account_id,
|
|
||||||
token: chatwoot_token,
|
|
||||||
url: chatwoot_url,
|
|
||||||
sign_msg: chatwoot_sign_msg || false,
|
|
||||||
name_inbox: instance.instanceName,
|
|
||||||
number,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.chatwootService.initInstanceChatwoot(
|
|
||||||
instance,
|
|
||||||
instance.instanceName,
|
|
||||||
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
|
||||||
qrcode,
|
|
||||||
number,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.log(error);
|
console.log(error);
|
||||||
|
return { error: true, message: error.toString() };
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
|
||||||
instance: {
|
|
||||||
instanceName: instance.instanceName,
|
|
||||||
status: 'created',
|
|
||||||
},
|
|
||||||
hash,
|
|
||||||
webhook,
|
|
||||||
webhook_by_events,
|
|
||||||
events: getEvents,
|
|
||||||
chatwoot: {
|
|
||||||
enabled: true,
|
|
||||||
account_id: chatwoot_account_id,
|
|
||||||
token: chatwoot_token,
|
|
||||||
url: chatwoot_url,
|
|
||||||
sign_msg: chatwoot_sign_msg || false,
|
|
||||||
number,
|
|
||||||
name_inbox: instance.instanceName,
|
|
||||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) {
|
public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) {
|
||||||
|
|||||||
@@ -15,10 +15,6 @@ export class SettingsController {
|
|||||||
'requested createSettings from ' + instance.instanceName + ' instance',
|
'requested createSettings from ' + instance.instanceName + ' instance',
|
||||||
);
|
);
|
||||||
|
|
||||||
if (data.reject_call && data.msg_call.trim() == '') {
|
|
||||||
throw new BadRequestException('msg_call is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.settingsService.create(instance, data);
|
return this.settingsService.create(instance, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ class Key {
|
|||||||
remoteJid: string;
|
remoteJid: string;
|
||||||
}
|
}
|
||||||
export class ReadMessageDto {
|
export class ReadMessageDto {
|
||||||
readMessages: Key[];
|
read_messages: Key[];
|
||||||
}
|
}
|
||||||
|
|
||||||
class LastMessage {
|
class LastMessage {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
export class CreateGroupDto {
|
export class CreateGroupDto {
|
||||||
subject: string;
|
subject: string;
|
||||||
description?: string;
|
|
||||||
participants: string[];
|
participants: string[];
|
||||||
|
description?: string;
|
||||||
|
promoteParticipants?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupPictureDto {
|
export class GroupPictureDto {
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
export class InstanceDto {
|
export class InstanceDto {
|
||||||
instanceName: string;
|
instanceName: string;
|
||||||
webhook?: string;
|
|
||||||
webhook_by_events?: boolean;
|
|
||||||
events?: string[];
|
|
||||||
qrcode?: boolean;
|
qrcode?: boolean;
|
||||||
number?: string;
|
number?: string;
|
||||||
token?: 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_account_id?: string;
|
||||||
chatwoot_token?: string;
|
chatwoot_token?: string;
|
||||||
chatwoot_url?: string;
|
chatwoot_url?: string;
|
||||||
|
|||||||
@@ -2,4 +2,7 @@ export class SettingsDto {
|
|||||||
reject_call?: boolean;
|
reject_call?: boolean;
|
||||||
msg_call?: string;
|
msg_call?: string;
|
||||||
groups_ignore?: boolean;
|
groups_ignore?: boolean;
|
||||||
|
always_online?: boolean;
|
||||||
|
read_messages?: boolean;
|
||||||
|
read_status?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ export class SettingsRaw {
|
|||||||
reject_call?: boolean;
|
reject_call?: boolean;
|
||||||
msg_call?: string;
|
msg_call?: string;
|
||||||
groups_ignore?: boolean;
|
groups_ignore?: boolean;
|
||||||
|
always_online?: boolean;
|
||||||
|
read_messages?: boolean;
|
||||||
|
read_status?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const settingsSchema = new Schema<SettingsRaw>({
|
const settingsSchema = new Schema<SettingsRaw>({
|
||||||
@@ -13,6 +16,9 @@ const settingsSchema = new Schema<SettingsRaw>({
|
|||||||
reject_call: { type: Boolean, required: true },
|
reject_call: { type: Boolean, required: true },
|
||||||
msg_call: { type: String, required: true },
|
msg_call: { type: String, required: true },
|
||||||
groups_ignore: { type: Boolean, 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(
|
export const SettingsModel = dbserver?.model(
|
||||||
|
|||||||
@@ -102,7 +102,12 @@ export class RepositoryBroker {
|
|||||||
this.logger.verbose('creating store path: ' + storePath);
|
this.logger.verbose('creating store path: ' + storePath);
|
||||||
|
|
||||||
const tempDir = join(storePath, 'temp');
|
const tempDir = join(storePath, 'temp');
|
||||||
|
const chatwootDir = join(storePath, 'chatwoot');
|
||||||
|
|
||||||
|
if (!fs.existsSync(chatwootDir)) {
|
||||||
|
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
|
||||||
|
fs.mkdirSync(chatwootDir, { recursive: true });
|
||||||
|
}
|
||||||
if (!fs.existsSync(tempDir)) {
|
if (!fs.existsSync(tempDir)) {
|
||||||
this.logger.verbose('creating temp dir: ' + tempDir);
|
this.logger.verbose('creating temp dir: ' + tempDir);
|
||||||
fs.mkdirSync(tempDir, { recursive: true });
|
fs.mkdirSync(tempDir, { recursive: true });
|
||||||
|
|||||||
@@ -1179,7 +1179,7 @@ export class ChatwootService {
|
|||||||
videoMessage: msg.videoMessage?.caption,
|
videoMessage: msg.videoMessage?.caption,
|
||||||
extendedTextMessage: msg.extendedTextMessage?.text,
|
extendedTextMessage: msg.extendedTextMessage?.text,
|
||||||
messageContextInfo: msg.messageContextInfo?.stanzaId,
|
messageContextInfo: msg.messageContextInfo?.stanzaId,
|
||||||
stickerMessage: msg.stickerMessage?.fileSha256.toString('base64'),
|
stickerMessage: undefined,
|
||||||
documentMessage: msg.documentMessage?.caption,
|
documentMessage: msg.documentMessage?.caption,
|
||||||
documentWithCaptionMessage:
|
documentWithCaptionMessage:
|
||||||
msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
|
msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
|
||||||
@@ -1199,10 +1199,6 @@ export class ChatwootService {
|
|||||||
|
|
||||||
const result = typeKey ? types[typeKey] : undefined;
|
const result = typeKey ? types[typeKey] : undefined;
|
||||||
|
|
||||||
if (typeKey === 'stickerMessage') {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeKey === 'contactMessage') {
|
if (typeKey === 'contactMessage') {
|
||||||
const vCardData = result.split('\n');
|
const vCardData = result.split('\n');
|
||||||
const contactInfo = {};
|
const contactInfo = {};
|
||||||
|
|||||||
@@ -116,16 +116,15 @@ import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-d
|
|||||||
import Long from 'long';
|
import Long from 'long';
|
||||||
import { WebhookRaw } from '../models/webhook.model';
|
import { WebhookRaw } from '../models/webhook.model';
|
||||||
import { ChatwootRaw } from '../models/chatwoot.model';
|
import { ChatwootRaw } from '../models/chatwoot.model';
|
||||||
|
import { SettingsRaw } from '../models';
|
||||||
import { dbserver } from '../../db/db.connect';
|
import { dbserver } from '../../db/db.connect';
|
||||||
import NodeCache from 'node-cache';
|
import NodeCache from 'node-cache';
|
||||||
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
|
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
|
||||||
import sharp from 'sharp';
|
import sharp from 'sharp';
|
||||||
import { RedisCache } from '../../db/redis.client';
|
import { RedisCache } from '../../db/redis.client';
|
||||||
import { Log } from '../../config/env.config';
|
import { Log } from '../../config/env.config';
|
||||||
import ProxyAgent from 'proxy-agent';
|
|
||||||
import { ChatwootService } from './chatwoot.service';
|
import { ChatwootService } from './chatwoot.service';
|
||||||
import { waMonitor } from '../whatsapp.module';
|
import { waMonitor } from '../whatsapp.module';
|
||||||
import { SettingsRaw } from '../models';
|
|
||||||
|
|
||||||
export class WAStartupService {
|
export class WAStartupService {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -363,6 +362,15 @@ export class WAStartupService {
|
|||||||
this.localSettings.groups_ignore = data?.groups_ignore;
|
this.localSettings.groups_ignore = data?.groups_ignore;
|
||||||
this.logger.verbose(`Settings groups_ignore: ${this.localSettings.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');
|
this.logger.verbose('Settings loaded');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -372,8 +380,13 @@ export class WAStartupService {
|
|||||||
this.logger.verbose(`Settings reject_call: ${data.reject_call}`);
|
this.logger.verbose(`Settings reject_call: ${data.reject_call}`);
|
||||||
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
|
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
|
||||||
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
|
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);
|
Object.assign(this.localSettings, data);
|
||||||
this.logger.verbose('Settings set');
|
this.logger.verbose('Settings set');
|
||||||
|
|
||||||
|
this.client?.ws?.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async findSettings() {
|
public async findSettings() {
|
||||||
@@ -382,12 +395,15 @@ export class WAStartupService {
|
|||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
this.logger.verbose('Settings not found');
|
this.logger.verbose('Settings not found');
|
||||||
throw new NotFoundException('Settings not found');
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose(`Settings url: ${data.reject_call}`);
|
this.logger.verbose(`Settings url: ${data.reject_call}`);
|
||||||
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
|
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
|
||||||
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -848,6 +864,7 @@ export class WAStartupService {
|
|||||||
printQRInTerminal: false,
|
printQRInTerminal: false,
|
||||||
browser,
|
browser,
|
||||||
version,
|
version,
|
||||||
|
markOnlineOnConnect: this.localSettings.always_online,
|
||||||
connectTimeoutMs: 60_000,
|
connectTimeoutMs: 60_000,
|
||||||
qrTimeout: 40_000,
|
qrTimeout: 40_000,
|
||||||
defaultQueryTimeoutMs: undefined,
|
defaultQueryTimeoutMs: undefined,
|
||||||
@@ -1129,7 +1146,7 @@ export class WAStartupService {
|
|||||||
received.messageTimestamp = received.messageTimestamp?.toNumber();
|
received.messageTimestamp = received.messageTimestamp?.toNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings.groups_ignore && received.key.remoteJid.includes('@g.us')) {
|
if (settings?.groups_ignore && received.key.remoteJid.includes('@g.us')) {
|
||||||
this.logger.verbose('group ignored');
|
this.logger.verbose('group ignored');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1144,6 +1161,14 @@ export class WAStartupService {
|
|||||||
source: getDevice(received.key.id),
|
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.log(messageRaw);
|
||||||
|
|
||||||
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
|
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
|
||||||
@@ -1363,11 +1388,15 @@ export class WAStartupService {
|
|||||||
text: settings.msg_call,
|
text: settings.msg_call,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.logger.verbose('Sending data to event messages.upsert');
|
||||||
this.client.ev.emit('messages.upsert', {
|
this.client.ev.emit('messages.upsert', {
|
||||||
messages: [msg],
|
messages: [msg],
|
||||||
type: 'notify',
|
type: 'notify',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('Sending data to webhook in event CALL');
|
||||||
|
this.sendDataWebhook(Events.CALL, call);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (events['connection.update']) {
|
if (events['connection.update']) {
|
||||||
@@ -1517,7 +1546,7 @@ export class WAStartupService {
|
|||||||
.split(/\:/)[0]
|
.split(/\:/)[0]
|
||||||
.split('@')[0];
|
.split('@')[0];
|
||||||
|
|
||||||
if (number.length >= 18) {
|
if (number.includes('-') && number.length >= 24) {
|
||||||
this.logger.verbose('Jid created is group: ' + `${number}@g.us`);
|
this.logger.verbose('Jid created is group: ' + `${number}@g.us`);
|
||||||
number = number.replace(/[^\d-]/g, '');
|
number = number.replace(/[^\d-]/g, '');
|
||||||
return `${number}@g.us`;
|
return `${number}@g.us`;
|
||||||
@@ -1525,6 +1554,12 @@ export class WAStartupService {
|
|||||||
|
|
||||||
number = number.replace(/\D/g, '');
|
number = number.replace(/\D/g, '');
|
||||||
|
|
||||||
|
if (number.length >= 18) {
|
||||||
|
this.logger.verbose('Jid created is group: ' + `${number}@g.us`);
|
||||||
|
number = number.replace(/[^\d-]/g, '');
|
||||||
|
return `${number}@g.us`;
|
||||||
|
}
|
||||||
|
|
||||||
this.logger.verbose('Jid created is whatsapp: ' + `${number}@s.whatsapp.net`);
|
this.logger.verbose('Jid created is whatsapp: ' + `${number}@s.whatsapp.net`);
|
||||||
return `${number}@s.whatsapp.net`;
|
return `${number}@s.whatsapp.net`;
|
||||||
}
|
}
|
||||||
@@ -1686,20 +1721,13 @@ export class WAStartupService {
|
|||||||
if (options?.mentions) {
|
if (options?.mentions) {
|
||||||
this.logger.verbose('Mentions defined');
|
this.logger.verbose('Mentions defined');
|
||||||
|
|
||||||
if (
|
if (options.mentions?.everyOne) {
|
||||||
!Array.isArray(options.mentions.mentioned) &&
|
|
||||||
!options.mentions.everyOne
|
|
||||||
) {
|
|
||||||
throw new BadRequestException('Mentions must be an array');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.mentions.everyOne) {
|
|
||||||
this.logger.verbose('Mentions everyone');
|
this.logger.verbose('Mentions everyone');
|
||||||
|
|
||||||
this.logger.verbose('Getting group metadata');
|
this.logger.verbose('Getting group metadata');
|
||||||
mentions = groupMetadata.participants.map((participant) => participant.id);
|
mentions = groupMetadata.participants.map((participant) => participant.id);
|
||||||
this.logger.verbose('Getting group metadata for mentions');
|
this.logger.verbose('Getting group metadata for mentions');
|
||||||
} else {
|
} else if (options.mentions?.mentioned?.length) {
|
||||||
this.logger.verbose('Mentions manually defined');
|
this.logger.verbose('Mentions manually defined');
|
||||||
mentions = options.mentions.mentioned.map((mention) => {
|
mentions = options.mentions.mentioned.map((mention) => {
|
||||||
const jid = this.createJid(mention);
|
const jid = this.createJid(mention);
|
||||||
@@ -2402,7 +2430,7 @@ export class WAStartupService {
|
|||||||
this.logger.verbose('Marking message as read');
|
this.logger.verbose('Marking message as read');
|
||||||
try {
|
try {
|
||||||
const keys: proto.IMessageKey[] = [];
|
const keys: proto.IMessageKey[] = [];
|
||||||
data.readMessages.forEach((read) => {
|
data.read_messages.forEach((read) => {
|
||||||
if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) {
|
if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) {
|
||||||
keys.push({
|
keys.push({
|
||||||
remoteJid: read.remoteJid,
|
remoteJid: read.remoteJid,
|
||||||
@@ -2642,7 +2670,6 @@ export class WAStartupService {
|
|||||||
await this.client.updateGroupsAddPrivacy(settings.privacySettings.groupadd);
|
await this.client.updateGroupsAddPrivacy(settings.privacySettings.groupadd);
|
||||||
this.logger.verbose('Groups add privacy updated');
|
this.logger.verbose('Groups add privacy updated');
|
||||||
|
|
||||||
// reinicia a instancia
|
|
||||||
this.client?.ws?.close();
|
this.client?.ws?.close();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -2782,10 +2809,19 @@ export class WAStartupService {
|
|||||||
await this.client.groupUpdateDescription(id, create.description);
|
await this.client.groupUpdateDescription(id, create.description);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (create?.promoteParticipants) {
|
||||||
|
this.logger.verbose('Prometing group participants: ' + create.description);
|
||||||
|
await this.updateGParticipant({
|
||||||
|
groupJid: id,
|
||||||
|
action: 'promote',
|
||||||
|
participants: participants,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const group = await this.client.groupMetadata(id);
|
const group = await this.client.groupMetadata(id);
|
||||||
this.logger.verbose('Getting group metadata');
|
this.logger.verbose('Getting group metadata');
|
||||||
|
|
||||||
return { groupMetadata: group };
|
return group;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
throw new InternalServerErrorException('Error creating group', error.toString());
|
throw new InternalServerErrorException('Error creating group', error.toString());
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ export enum Events {
|
|||||||
GROUPS_UPSERT = 'groups.upsert',
|
GROUPS_UPSERT = 'groups.upsert',
|
||||||
GROUPS_UPDATE = 'groups.update',
|
GROUPS_UPDATE = 'groups.update',
|
||||||
GROUP_PARTICIPANTS_UPDATE = 'group-participants.update',
|
GROUP_PARTICIPANTS_UPDATE = 'group-participants.update',
|
||||||
|
CALL = 'call',
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare namespace wa {
|
export declare namespace wa {
|
||||||
@@ -61,6 +62,9 @@ export declare namespace wa {
|
|||||||
reject_call?: boolean;
|
reject_call?: boolean;
|
||||||
msg_call?: string;
|
msg_call?: string;
|
||||||
groups_ignore?: boolean;
|
groups_ignore?: boolean;
|
||||||
|
always_online?: boolean;
|
||||||
|
read_messages?: boolean;
|
||||||
|
read_status?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type StateConnection = {
|
export type StateConnection = {
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ export const instanceController = new InstanceController(
|
|||||||
authService,
|
authService,
|
||||||
webhookService,
|
webhookService,
|
||||||
chatwootService,
|
chatwootService,
|
||||||
|
settingsService,
|
||||||
cache,
|
cache,
|
||||||
);
|
);
|
||||||
export const viewsController = new ViewsController(waMonitor, configService);
|
export const viewsController = new ViewsController(waMonitor, configService);
|
||||||
|
|||||||
Reference in New Issue
Block a user