mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 01:41:24 -06:00
wip
This commit is contained in:
parent
5482601b41
commit
ddc75d710f
@ -81,6 +81,7 @@
|
|||||||
"@types/express": "^4.17.17",
|
"@types/express": "^4.17.17",
|
||||||
"@types/js-yaml": "^4.0.5",
|
"@types/js-yaml": "^4.0.5",
|
||||||
"@types/jsonwebtoken": "^8.5.9",
|
"@types/jsonwebtoken": "^8.5.9",
|
||||||
|
"@types/mime-types": "^2.1.1",
|
||||||
"@types/node": "^18.15.11",
|
"@types/node": "^18.15.11",
|
||||||
"@types/qrcode": "^1.5.0",
|
"@types/qrcode": "^1.5.0",
|
||||||
"@types/qrcode-terminal": "^0.12.0",
|
"@types/qrcode-terminal": "^0.12.0",
|
||||||
|
@ -1,114 +1,106 @@
|
|||||||
|
import { isBooleanString } from 'class-validator';
|
||||||
import { readFileSync } from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
import { load } from 'js-yaml';
|
import { load } from 'js-yaml';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { isBooleanString } from 'class-validator';
|
|
||||||
|
|
||||||
export type HttpServer = { TYPE: 'http' | 'https'; PORT: number; URL: string };
|
export type HttpServer = { TYPE: 'http' | 'https'; PORT: number; URL: string };
|
||||||
|
|
||||||
export type HttpMethods = 'POST' | 'GET' | 'PUT' | 'DELETE';
|
export type HttpMethods = 'POST' | 'GET' | 'PUT' | 'DELETE';
|
||||||
export type Cors = {
|
export type Cors = {
|
||||||
ORIGIN: string[];
|
ORIGIN: string[];
|
||||||
METHODS: HttpMethods[];
|
METHODS: HttpMethods[];
|
||||||
CREDENTIALS: boolean;
|
CREDENTIALS: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LogBaileys = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
export type LogBaileys = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
||||||
|
|
||||||
export type LogLevel =
|
export type LogLevel = 'ERROR' | 'WARN' | 'DEBUG' | 'INFO' | 'LOG' | 'VERBOSE' | 'DARK' | 'WEBHOOKS';
|
||||||
| 'ERROR'
|
|
||||||
| 'WARN'
|
|
||||||
| 'DEBUG'
|
|
||||||
| 'INFO'
|
|
||||||
| 'LOG'
|
|
||||||
| 'VERBOSE'
|
|
||||||
| 'DARK'
|
|
||||||
| 'WEBHOOKS';
|
|
||||||
|
|
||||||
export type Log = {
|
export type Log = {
|
||||||
LEVEL: LogLevel[];
|
LEVEL: LogLevel[];
|
||||||
COLOR: boolean;
|
COLOR: boolean;
|
||||||
BAILEYS: LogBaileys;
|
BAILEYS: LogBaileys;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SaveData = {
|
export type SaveData = {
|
||||||
INSTANCE: boolean;
|
INSTANCE: boolean;
|
||||||
NEW_MESSAGE: boolean;
|
NEW_MESSAGE: boolean;
|
||||||
MESSAGE_UPDATE: boolean;
|
MESSAGE_UPDATE: boolean;
|
||||||
CONTACTS: boolean;
|
CONTACTS: boolean;
|
||||||
CHATS: boolean;
|
CHATS: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type StoreConf = {
|
export type StoreConf = {
|
||||||
MESSAGES: boolean;
|
MESSAGES: boolean;
|
||||||
MESSAGE_UP: boolean;
|
MESSAGE_UP: boolean;
|
||||||
CONTACTS: boolean;
|
CONTACTS: boolean;
|
||||||
CHATS: boolean;
|
CHATS: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CleanStoreConf = {
|
export type CleanStoreConf = {
|
||||||
CLEANING_INTERVAL: number;
|
CLEANING_INTERVAL: number;
|
||||||
MESSAGES: boolean;
|
MESSAGES: boolean;
|
||||||
MESSAGE_UP: boolean;
|
MESSAGE_UP: boolean;
|
||||||
CONTACTS: boolean;
|
CONTACTS: boolean;
|
||||||
CHATS: boolean;
|
CHATS: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type DBConnection = {
|
export type DBConnection = {
|
||||||
URI: string;
|
URI: string;
|
||||||
DB_PREFIX_NAME: string;
|
DB_PREFIX_NAME: string;
|
||||||
};
|
};
|
||||||
export type Database = {
|
export type Database = {
|
||||||
CONNECTION: DBConnection;
|
CONNECTION: DBConnection;
|
||||||
ENABLED: boolean;
|
ENABLED: boolean;
|
||||||
SAVE_DATA: SaveData;
|
SAVE_DATA: SaveData;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Redis = {
|
export type Redis = {
|
||||||
ENABLED: boolean;
|
ENABLED: boolean;
|
||||||
URI: string;
|
URI: string;
|
||||||
PREFIX_KEY: string;
|
PREFIX_KEY: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type EventsWebhook = {
|
export type EventsWebhook = {
|
||||||
APPLICATION_STARTUP: boolean;
|
APPLICATION_STARTUP: boolean;
|
||||||
QRCODE_UPDATED: boolean;
|
QRCODE_UPDATED: boolean;
|
||||||
MESSAGES_SET: boolean;
|
MESSAGES_SET: boolean;
|
||||||
MESSAGES_UPSERT: boolean;
|
MESSAGES_UPSERT: boolean;
|
||||||
MESSAGES_UPDATE: boolean;
|
MESSAGES_UPDATE: boolean;
|
||||||
MESSAGES_DELETE: boolean;
|
MESSAGES_DELETE: boolean;
|
||||||
SEND_MESSAGE: boolean;
|
SEND_MESSAGE: boolean;
|
||||||
CONTACTS_SET: boolean;
|
CONTACTS_SET: boolean;
|
||||||
CONTACTS_UPDATE: boolean;
|
CONTACTS_UPDATE: boolean;
|
||||||
CONTACTS_UPSERT: boolean;
|
CONTACTS_UPSERT: boolean;
|
||||||
PRESENCE_UPDATE: boolean;
|
PRESENCE_UPDATE: boolean;
|
||||||
CHATS_SET: boolean;
|
CHATS_SET: boolean;
|
||||||
CHATS_UPDATE: boolean;
|
CHATS_UPDATE: boolean;
|
||||||
CHATS_DELETE: boolean;
|
CHATS_DELETE: boolean;
|
||||||
CHATS_UPSERT: boolean;
|
CHATS_UPSERT: boolean;
|
||||||
CONNECTION_UPDATE: boolean;
|
CONNECTION_UPDATE: boolean;
|
||||||
GROUPS_UPSERT: boolean;
|
GROUPS_UPSERT: boolean;
|
||||||
GROUP_UPDATE: boolean;
|
GROUP_UPDATE: boolean;
|
||||||
GROUP_PARTICIPANTS_UPDATE: boolean;
|
GROUP_PARTICIPANTS_UPDATE: boolean;
|
||||||
CALL: boolean;
|
CALL: boolean;
|
||||||
NEW_JWT_TOKEN: boolean;
|
NEW_JWT_TOKEN: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ApiKey = { KEY: string };
|
export type ApiKey = { KEY: string };
|
||||||
export type Jwt = { EXPIRIN_IN: number; SECRET: string };
|
export type Jwt = { EXPIRIN_IN: number; SECRET: string };
|
||||||
|
|
||||||
export type Auth = {
|
export type Auth = {
|
||||||
API_KEY: ApiKey;
|
API_KEY: ApiKey;
|
||||||
EXPOSE_IN_FETCH_INSTANCES: boolean;
|
EXPOSE_IN_FETCH_INSTANCES: boolean;
|
||||||
JWT: Jwt;
|
JWT: Jwt;
|
||||||
TYPE: 'jwt' | 'apikey';
|
TYPE: 'jwt' | 'apikey';
|
||||||
};
|
};
|
||||||
|
|
||||||
export type DelInstance = number | boolean;
|
export type DelInstance = number | boolean;
|
||||||
|
|
||||||
export type GlobalWebhook = {
|
export type GlobalWebhook = {
|
||||||
URL: string;
|
URL: string;
|
||||||
ENABLED: boolean;
|
ENABLED: boolean;
|
||||||
WEBHOOK_BY_EVENTS: boolean;
|
WEBHOOK_BY_EVENTS: boolean;
|
||||||
};
|
};
|
||||||
export type SslConf = { PRIVKEY: string; FULLCHAIN: string };
|
export type SslConf = { PRIVKEY: string; FULLCHAIN: string };
|
||||||
export type Webhook = { GLOBAL?: GlobalWebhook; EVENTS: EventsWebhook };
|
export type Webhook = { GLOBAL?: GlobalWebhook; EVENTS: EventsWebhook };
|
||||||
@ -117,162 +109,158 @@ export type QrCode = { LIMIT: number };
|
|||||||
export type Production = boolean;
|
export type Production = boolean;
|
||||||
|
|
||||||
export interface Env {
|
export interface Env {
|
||||||
SERVER: HttpServer;
|
SERVER: HttpServer;
|
||||||
CORS: Cors;
|
CORS: Cors;
|
||||||
SSL_CONF: SslConf;
|
SSL_CONF: SslConf;
|
||||||
STORE: StoreConf;
|
STORE: StoreConf;
|
||||||
CLEAN_STORE: CleanStoreConf;
|
CLEAN_STORE: CleanStoreConf;
|
||||||
DATABASE: Database;
|
DATABASE: Database;
|
||||||
REDIS: Redis;
|
REDIS: Redis;
|
||||||
LOG: Log;
|
LOG: Log;
|
||||||
DEL_INSTANCE: DelInstance;
|
DEL_INSTANCE: DelInstance;
|
||||||
WEBHOOK: Webhook;
|
WEBHOOK: Webhook;
|
||||||
CONFIG_SESSION_PHONE: ConfigSessionPhone;
|
CONFIG_SESSION_PHONE: ConfigSessionPhone;
|
||||||
QRCODE: QrCode;
|
QRCODE: QrCode;
|
||||||
AUTHENTICATION: Auth;
|
AUTHENTICATION: Auth;
|
||||||
PRODUCTION?: Production;
|
PRODUCTION?: Production;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Key = keyof Env;
|
export type Key = keyof Env;
|
||||||
|
|
||||||
export class ConfigService {
|
export class ConfigService {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.loadEnv();
|
this.loadEnv();
|
||||||
}
|
|
||||||
|
|
||||||
private env: Env;
|
|
||||||
|
|
||||||
public get<T = any>(key: Key) {
|
|
||||||
return this.env[key] as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
private loadEnv() {
|
|
||||||
this.env = !(process.env?.DOCKER_ENV === 'true') ? this.envYaml() : this.envProcess();
|
|
||||||
this.env.PRODUCTION = process.env?.NODE_ENV === 'PROD';
|
|
||||||
if (process.env?.DOCKER_ENV === 'true') {
|
|
||||||
this.env.SERVER.TYPE = 'http';
|
|
||||||
this.env.SERVER.PORT = 8080;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private envYaml(): Env {
|
private env: Env;
|
||||||
return load(
|
|
||||||
readFileSync(join(process.cwd(), 'src', 'env.yml'), { encoding: 'utf-8' }),
|
|
||||||
) as Env;
|
|
||||||
}
|
|
||||||
|
|
||||||
private envProcess(): Env {
|
public get<T = any>(key: Key) {
|
||||||
return {
|
return this.env[key] as T;
|
||||||
SERVER: {
|
}
|
||||||
TYPE: process.env.SERVER_TYPE as 'http' | 'https',
|
|
||||||
PORT: Number.parseInt(process.env.SERVER_PORT),
|
private loadEnv() {
|
||||||
URL: process.env.SERVER_URL,
|
this.env = !(process.env?.DOCKER_ENV === 'true') ? this.envYaml() : this.envProcess();
|
||||||
},
|
this.env.PRODUCTION = process.env?.NODE_ENV === 'PROD';
|
||||||
CORS: {
|
if (process.env?.DOCKER_ENV === 'true') {
|
||||||
ORIGIN: process.env.CORS_ORIGIN.split(','),
|
this.env.SERVER.TYPE = 'http';
|
||||||
METHODS: process.env.CORS_METHODS.split(',') as HttpMethods[],
|
this.env.SERVER.PORT = 8080;
|
||||||
CREDENTIALS: process.env?.CORS_CREDENTIALS === 'true',
|
}
|
||||||
},
|
}
|
||||||
SSL_CONF: {
|
|
||||||
PRIVKEY: process.env?.SSL_CONF_PRIVKEY,
|
private envYaml(): Env {
|
||||||
FULLCHAIN: process.env?.SSL_CONF_FULLCHAIN,
|
return load(readFileSync(join(process.cwd(), 'src', 'env.yml'), { encoding: 'utf-8' })) as Env;
|
||||||
},
|
}
|
||||||
STORE: {
|
|
||||||
MESSAGES: process.env?.STORE_MESSAGES === 'true',
|
private envProcess(): Env {
|
||||||
MESSAGE_UP: process.env?.STORE_MESSAGE_UP === 'true',
|
return {
|
||||||
CONTACTS: process.env?.STORE_CONTACTS === 'true',
|
SERVER: {
|
||||||
CHATS: process.env?.STORE_CHATS === 'true',
|
TYPE: process.env.SERVER_TYPE as 'http' | 'https',
|
||||||
},
|
PORT: Number.parseInt(process.env.SERVER_PORT),
|
||||||
CLEAN_STORE: {
|
URL: process.env.SERVER_URL,
|
||||||
CLEANING_INTERVAL: Number.isInteger(process.env?.CLEAN_STORE_CLEANING_TERMINAL)
|
},
|
||||||
? Number.parseInt(process.env.CLEAN_STORE_CLEANING_TERMINAL)
|
CORS: {
|
||||||
: 7200,
|
ORIGIN: process.env.CORS_ORIGIN.split(','),
|
||||||
MESSAGES: process.env?.CLEAN_STORE_MESSAGES === 'true',
|
METHODS: process.env.CORS_METHODS.split(',') as HttpMethods[],
|
||||||
MESSAGE_UP: process.env?.CLEAN_STORE_MESSAGE_UP === 'true',
|
CREDENTIALS: process.env?.CORS_CREDENTIALS === 'true',
|
||||||
CONTACTS: process.env?.CLEAN_STORE_CONTACTS === 'true',
|
},
|
||||||
CHATS: process.env?.CLEAN_STORE_CHATS === 'true',
|
SSL_CONF: {
|
||||||
},
|
PRIVKEY: process.env?.SSL_CONF_PRIVKEY,
|
||||||
DATABASE: {
|
FULLCHAIN: process.env?.SSL_CONF_FULLCHAIN,
|
||||||
CONNECTION: {
|
},
|
||||||
URI: process.env.DATABASE_CONNECTION_URI,
|
STORE: {
|
||||||
DB_PREFIX_NAME: process.env.DATABASE_CONNECTION_DB_PREFIX_NAME,
|
MESSAGES: process.env?.STORE_MESSAGES === 'true',
|
||||||
},
|
MESSAGE_UP: process.env?.STORE_MESSAGE_UP === 'true',
|
||||||
ENABLED: process.env?.DATABASE_ENABLED === 'true',
|
CONTACTS: process.env?.STORE_CONTACTS === 'true',
|
||||||
SAVE_DATA: {
|
CHATS: process.env?.STORE_CHATS === 'true',
|
||||||
INSTANCE: process.env?.DATABASE_SAVE_DATA_INSTANCE === 'true',
|
},
|
||||||
NEW_MESSAGE: process.env?.DATABASE_SAVE_DATA_NEW_MESSAGE === 'true',
|
CLEAN_STORE: {
|
||||||
MESSAGE_UPDATE: process.env?.DATABASE_SAVE_MESSAGE_UPDATE === 'true',
|
CLEANING_INTERVAL: Number.isInteger(process.env?.CLEAN_STORE_CLEANING_TERMINAL)
|
||||||
CONTACTS: process.env?.DATABASE_SAVE_DATA_CONTACTS === 'true',
|
? Number.parseInt(process.env.CLEAN_STORE_CLEANING_TERMINAL)
|
||||||
CHATS: process.env?.DATABASE_SAVE_DATA_CHATS === 'true',
|
: 7200,
|
||||||
},
|
MESSAGES: process.env?.CLEAN_STORE_MESSAGES === 'true',
|
||||||
},
|
MESSAGE_UP: process.env?.CLEAN_STORE_MESSAGE_UP === 'true',
|
||||||
REDIS: {
|
CONTACTS: process.env?.CLEAN_STORE_CONTACTS === 'true',
|
||||||
ENABLED: process.env?.REDIS_ENABLED === 'true',
|
CHATS: process.env?.CLEAN_STORE_CHATS === 'true',
|
||||||
URI: process.env.REDIS_URI,
|
},
|
||||||
PREFIX_KEY: process.env.REDIS_PREFIX_KEY,
|
DATABASE: {
|
||||||
},
|
CONNECTION: {
|
||||||
LOG: {
|
URI: process.env.DATABASE_CONNECTION_URI,
|
||||||
LEVEL: process.env?.LOG_LEVEL.split(',') as LogLevel[],
|
DB_PREFIX_NAME: process.env.DATABASE_CONNECTION_DB_PREFIX_NAME,
|
||||||
COLOR: process.env?.LOG_COLOR === 'true',
|
},
|
||||||
BAILEYS: (process.env?.LOG_BAILEYS as LogBaileys) || 'error',
|
ENABLED: process.env?.DATABASE_ENABLED === 'true',
|
||||||
},
|
SAVE_DATA: {
|
||||||
DEL_INSTANCE: isBooleanString(process.env?.DEL_INSTANCE)
|
INSTANCE: process.env?.DATABASE_SAVE_DATA_INSTANCE === 'true',
|
||||||
? process.env.DEL_INSTANCE === 'true'
|
NEW_MESSAGE: process.env?.DATABASE_SAVE_DATA_NEW_MESSAGE === 'true',
|
||||||
: Number.parseInt(process.env.DEL_INSTANCE) || false,
|
MESSAGE_UPDATE: process.env?.DATABASE_SAVE_MESSAGE_UPDATE === 'true',
|
||||||
WEBHOOK: {
|
CONTACTS: process.env?.DATABASE_SAVE_DATA_CONTACTS === 'true',
|
||||||
GLOBAL: {
|
CHATS: process.env?.DATABASE_SAVE_DATA_CHATS === 'true',
|
||||||
URL: process.env?.WEBHOOK_GLOBAL_URL,
|
},
|
||||||
ENABLED: process.env?.WEBHOOK_GLOBAL_ENABLED === 'true',
|
},
|
||||||
WEBHOOK_BY_EVENTS: process.env?.WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS === 'true',
|
REDIS: {
|
||||||
},
|
ENABLED: process.env?.REDIS_ENABLED === 'true',
|
||||||
EVENTS: {
|
URI: process.env.REDIS_URI,
|
||||||
APPLICATION_STARTUP: process.env?.WEBHOOK_EVENTS_APPLICATION_STARTUP === 'true',
|
PREFIX_KEY: process.env.REDIS_PREFIX_KEY,
|
||||||
QRCODE_UPDATED: process.env?.WEBHOOK_EVENTS_QRCODE_UPDATED === 'true',
|
},
|
||||||
MESSAGES_SET: process.env?.WEBHOOK_EVENTS_MESSAGES_SET === 'true',
|
LOG: {
|
||||||
MESSAGES_UPSERT: process.env?.WEBHOOK_EVENTS_MESSAGES_UPSERT === 'true',
|
LEVEL: process.env?.LOG_LEVEL.split(',') as LogLevel[],
|
||||||
MESSAGES_UPDATE: process.env?.WEBHOOK_EVENTS_MESSAGES_UPDATE === 'true',
|
COLOR: process.env?.LOG_COLOR === 'true',
|
||||||
MESSAGES_DELETE: process.env?.WEBHOOK_EVENTS_MESSAGES_DELETE === 'true',
|
BAILEYS: (process.env?.LOG_BAILEYS as LogBaileys) || 'error',
|
||||||
SEND_MESSAGE: process.env?.WEBHOOK_EVENTS_SEND_MESSAGE === 'true',
|
},
|
||||||
CONTACTS_SET: process.env?.WEBHOOK_EVENTS_CONTACTS_SET === 'true',
|
DEL_INSTANCE: isBooleanString(process.env?.DEL_INSTANCE)
|
||||||
CONTACTS_UPDATE: process.env?.WEBHOOK_EVENTS_CONTACTS_UPDATE === 'true',
|
? process.env.DEL_INSTANCE === 'true'
|
||||||
CONTACTS_UPSERT: process.env?.WEBHOOK_EVENTS_CONTACTS_UPSERT === 'true',
|
: Number.parseInt(process.env.DEL_INSTANCE) || false,
|
||||||
PRESENCE_UPDATE: process.env?.WEBHOOK_EVENTS_PRESENCE_UPDATE === 'true',
|
WEBHOOK: {
|
||||||
CHATS_SET: process.env?.WEBHOOK_EVENTS_CHATS_SET === 'true',
|
GLOBAL: {
|
||||||
CHATS_UPDATE: process.env?.WEBHOOK_EVENTS_CHATS_UPDATE === 'true',
|
URL: process.env?.WEBHOOK_GLOBAL_URL,
|
||||||
CHATS_UPSERT: process.env?.WEBHOOK_EVENTS_CHATS_UPSERT === 'true',
|
ENABLED: process.env?.WEBHOOK_GLOBAL_ENABLED === 'true',
|
||||||
CHATS_DELETE: process.env?.WEBHOOK_EVENTS_CHATS_DELETE === 'true',
|
WEBHOOK_BY_EVENTS: process.env?.WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS === 'true',
|
||||||
CONNECTION_UPDATE: process.env?.WEBHOOK_EVENTS_CONNECTION_UPDATE === 'true',
|
},
|
||||||
GROUPS_UPSERT: process.env?.WEBHOOK_EVENTS_GROUPS_UPSERT === 'true',
|
EVENTS: {
|
||||||
GROUP_UPDATE: process.env?.WEBHOOK_EVENTS_GROUPS_UPDATE === 'true',
|
APPLICATION_STARTUP: process.env?.WEBHOOK_EVENTS_APPLICATION_STARTUP === 'true',
|
||||||
GROUP_PARTICIPANTS_UPDATE:
|
QRCODE_UPDATED: process.env?.WEBHOOK_EVENTS_QRCODE_UPDATED === 'true',
|
||||||
process.env?.WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE === 'true',
|
MESSAGES_SET: process.env?.WEBHOOK_EVENTS_MESSAGES_SET === 'true',
|
||||||
CALL: process.env?.WEBHOOK_EVENTS_CALL === 'true',
|
MESSAGES_UPSERT: process.env?.WEBHOOK_EVENTS_MESSAGES_UPSERT === 'true',
|
||||||
NEW_JWT_TOKEN: process.env?.WEBHOOK_EVENTS_NEW_JWT_TOKEN === 'true',
|
MESSAGES_UPDATE: process.env?.WEBHOOK_EVENTS_MESSAGES_UPDATE === 'true',
|
||||||
},
|
MESSAGES_DELETE: process.env?.WEBHOOK_EVENTS_MESSAGES_DELETE === 'true',
|
||||||
},
|
SEND_MESSAGE: process.env?.WEBHOOK_EVENTS_SEND_MESSAGE === 'true',
|
||||||
CONFIG_SESSION_PHONE: {
|
CONTACTS_SET: process.env?.WEBHOOK_EVENTS_CONTACTS_SET === 'true',
|
||||||
CLIENT: process.env?.CONFIG_SESSION_PHONE_CLIENT || 'Evolution API',
|
CONTACTS_UPDATE: process.env?.WEBHOOK_EVENTS_CONTACTS_UPDATE === 'true',
|
||||||
NAME: process.env?.CONFIG_SESSION_PHONE_NAME || 'chrome',
|
CONTACTS_UPSERT: process.env?.WEBHOOK_EVENTS_CONTACTS_UPSERT === 'true',
|
||||||
},
|
PRESENCE_UPDATE: process.env?.WEBHOOK_EVENTS_PRESENCE_UPDATE === 'true',
|
||||||
QRCODE: {
|
CHATS_SET: process.env?.WEBHOOK_EVENTS_CHATS_SET === 'true',
|
||||||
LIMIT: Number.parseInt(process.env.QRCODE_LIMIT) || 30,
|
CHATS_UPDATE: process.env?.WEBHOOK_EVENTS_CHATS_UPDATE === 'true',
|
||||||
},
|
CHATS_UPSERT: process.env?.WEBHOOK_EVENTS_CHATS_UPSERT === 'true',
|
||||||
AUTHENTICATION: {
|
CHATS_DELETE: process.env?.WEBHOOK_EVENTS_CHATS_DELETE === 'true',
|
||||||
TYPE: process.env.AUTHENTICATION_TYPE as 'jwt',
|
CONNECTION_UPDATE: process.env?.WEBHOOK_EVENTS_CONNECTION_UPDATE === 'true',
|
||||||
API_KEY: {
|
GROUPS_UPSERT: process.env?.WEBHOOK_EVENTS_GROUPS_UPSERT === 'true',
|
||||||
KEY: process.env.AUTHENTICATION_API_KEY,
|
GROUP_UPDATE: process.env?.WEBHOOK_EVENTS_GROUPS_UPDATE === 'true',
|
||||||
},
|
GROUP_PARTICIPANTS_UPDATE: process.env?.WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE === 'true',
|
||||||
EXPOSE_IN_FETCH_INSTANCES:
|
CALL: process.env?.WEBHOOK_EVENTS_CALL === 'true',
|
||||||
process.env?.AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES === 'true',
|
NEW_JWT_TOKEN: process.env?.WEBHOOK_EVENTS_NEW_JWT_TOKEN === 'true',
|
||||||
JWT: {
|
},
|
||||||
EXPIRIN_IN: Number.isInteger(process.env?.AUTHENTICATION_JWT_EXPIRIN_IN)
|
},
|
||||||
? Number.parseInt(process.env.AUTHENTICATION_JWT_EXPIRIN_IN)
|
CONFIG_SESSION_PHONE: {
|
||||||
: 3600,
|
CLIENT: process.env?.CONFIG_SESSION_PHONE_CLIENT || 'Evolution API',
|
||||||
SECRET: process.env.AUTHENTICATION_JWT_SECRET,
|
NAME: process.env?.CONFIG_SESSION_PHONE_NAME || 'chrome',
|
||||||
},
|
},
|
||||||
},
|
QRCODE: {
|
||||||
};
|
LIMIT: Number.parseInt(process.env.QRCODE_LIMIT) || 30,
|
||||||
}
|
},
|
||||||
|
AUTHENTICATION: {
|
||||||
|
TYPE: process.env.AUTHENTICATION_TYPE as 'jwt',
|
||||||
|
API_KEY: {
|
||||||
|
KEY: process.env.AUTHENTICATION_API_KEY,
|
||||||
|
},
|
||||||
|
EXPOSE_IN_FETCH_INSTANCES: process.env?.AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES === 'true',
|
||||||
|
JWT: {
|
||||||
|
EXPIRIN_IN: Number.isInteger(process.env?.AUTHENTICATION_JWT_EXPIRIN_IN)
|
||||||
|
? Number.parseInt(process.env.AUTHENTICATION_JWT_EXPIRIN_IN)
|
||||||
|
: 3600,
|
||||||
|
SECRET: process.env.AUTHENTICATION_JWT_SECRET,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const configService = new ConfigService();
|
export const configService = new ConfigService();
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,105 +1,99 @@
|
|||||||
import { isURL } from 'class-validator';
|
import { isURL } from 'class-validator';
|
||||||
import { BadRequestException } from '../../exceptions';
|
|
||||||
import { InstanceDto } from '../dto/instance.dto';
|
|
||||||
import { ChatwootDto } from '../dto/chatwoot.dto';
|
|
||||||
import { ChatwootService } from '../services/chatwoot.service';
|
|
||||||
import { Logger } from '../../config/logger.config';
|
|
||||||
import { waMonitor } from '../whatsapp.module';
|
|
||||||
import { ConfigService, HttpServer } from '../../config/env.config';
|
import { ConfigService, HttpServer } from '../../config/env.config';
|
||||||
|
import { Logger } from '../../config/logger.config';
|
||||||
|
import { BadRequestException } from '../../exceptions';
|
||||||
|
import { ChatwootDto } from '../dto/chatwoot.dto';
|
||||||
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
|
import { ChatwootService } from '../services/chatwoot.service';
|
||||||
|
import { waMonitor } from '../whatsapp.module';
|
||||||
|
|
||||||
const logger = new Logger('ChatwootController');
|
const logger = new Logger('ChatwootController');
|
||||||
|
|
||||||
export class ChatwootController {
|
export class ChatwootController {
|
||||||
constructor(
|
constructor(private readonly chatwootService: ChatwootService, private readonly configService: ConfigService) {}
|
||||||
private readonly chatwootService: ChatwootService,
|
|
||||||
private readonly configService: ConfigService,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
public async createChatwoot(instance: InstanceDto, data: ChatwootDto) {
|
public async createChatwoot(instance: InstanceDto, data: ChatwootDto) {
|
||||||
logger.verbose(
|
logger.verbose('requested createChatwoot from ' + instance.instanceName + ' instance');
|
||||||
'requested createChatwoot from ' + instance.instanceName + ' instance',
|
|
||||||
);
|
|
||||||
|
|
||||||
if (data.enabled) {
|
if (data.enabled) {
|
||||||
if (!isURL(data.url, { require_tld: false })) {
|
if (!isURL(data.url, { require_tld: false })) {
|
||||||
throw new BadRequestException('url is not valid');
|
throw new BadRequestException('url is not valid');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.account_id) {
|
if (!data.account_id) {
|
||||||
throw new BadRequestException('account_id is required');
|
throw new BadRequestException('account_id is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.token) {
|
if (!data.token) {
|
||||||
throw new BadRequestException('token is required');
|
throw new BadRequestException('token is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.sign_msg !== true && data.sign_msg !== false) {
|
if (data.sign_msg !== true && data.sign_msg !== false) {
|
||||||
throw new BadRequestException('sign_msg is required');
|
throw new BadRequestException('sign_msg is required');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data.enabled) {
|
||||||
|
logger.verbose('chatwoot disabled');
|
||||||
|
data.account_id = '';
|
||||||
|
data.token = '';
|
||||||
|
data.url = '';
|
||||||
|
data.sign_msg = false;
|
||||||
|
data.reopen_conversation = false;
|
||||||
|
data.conversation_pending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
data.name_inbox = instance.instanceName;
|
||||||
|
|
||||||
|
const result = this.chatwootService.create(instance, data);
|
||||||
|
|
||||||
|
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
|
|
||||||
|
const response = {
|
||||||
|
...result,
|
||||||
|
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||||
|
};
|
||||||
|
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.enabled) {
|
public async findChatwoot(instance: InstanceDto) {
|
||||||
logger.verbose('chatwoot disabled');
|
logger.verbose('requested findChatwoot from ' + instance.instanceName + ' instance');
|
||||||
data.account_id = '';
|
const result = await this.chatwootService.find(instance);
|
||||||
data.token = '';
|
|
||||||
data.url = '';
|
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
data.sign_msg = false;
|
|
||||||
data.reopen_conversation = false;
|
if (Object.keys(result).length === 0) {
|
||||||
data.conversation_pending = false;
|
return {
|
||||||
|
enabled: false,
|
||||||
|
url: '',
|
||||||
|
account_id: '',
|
||||||
|
token: '',
|
||||||
|
sign_msg: false,
|
||||||
|
name_inbox: '',
|
||||||
|
webhook_url: '',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = {
|
||||||
|
...result,
|
||||||
|
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||||
|
};
|
||||||
|
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
data.name_inbox = instance.instanceName;
|
public async receiveWebhook(instance: InstanceDto, data: any) {
|
||||||
|
logger.verbose('requested receiveWebhook from ' + instance.instanceName + ' instance');
|
||||||
|
const chatwootService = new ChatwootService(waMonitor, this.configService);
|
||||||
|
|
||||||
const result = this.chatwootService.create(instance, data);
|
return chatwootService.receiveWebhook(instance, data);
|
||||||
|
|
||||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
|
||||||
|
|
||||||
const response = {
|
|
||||||
...result,
|
|
||||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
|
||||||
};
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async findChatwoot(instance: InstanceDto) {
|
|
||||||
logger.verbose('requested findChatwoot from ' + instance.instanceName + ' instance');
|
|
||||||
const result = await this.chatwootService.find(instance);
|
|
||||||
|
|
||||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
|
||||||
|
|
||||||
if (Object.keys(result).length === 0) {
|
|
||||||
return {
|
|
||||||
enabled: false,
|
|
||||||
url: '',
|
|
||||||
account_id: '',
|
|
||||||
token: '',
|
|
||||||
sign_msg: false,
|
|
||||||
name_inbox: '',
|
|
||||||
webhook_url: '',
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = {
|
public async newInstance(data: any) {
|
||||||
...result,
|
const chatwootService = new ChatwootService(waMonitor, this.configService);
|
||||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
|
||||||
};
|
|
||||||
|
|
||||||
return response;
|
return chatwootService.newInstance(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async receiveWebhook(instance: InstanceDto, data: any) {
|
|
||||||
logger.verbose(
|
|
||||||
'requested receiveWebhook from ' + instance.instanceName + ' instance',
|
|
||||||
);
|
|
||||||
const chatwootService = new ChatwootService(waMonitor, this.configService);
|
|
||||||
|
|
||||||
return chatwootService.receiveWebhook(instance, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async newInstance(data: any) {
|
|
||||||
const chatwootService = new ChatwootService(waMonitor, this.configService);
|
|
||||||
|
|
||||||
return chatwootService.newInstance(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,376 +1,356 @@
|
|||||||
import { delay } from '@whiskeysockets/baileys';
|
import { delay } from '@whiskeysockets/baileys';
|
||||||
|
import { isURL } from 'class-validator';
|
||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
import { Auth, ConfigService, HttpServer } from '../../config/env.config';
|
|
||||||
|
import { ConfigService, HttpServer } from '../../config/env.config';
|
||||||
|
import { Logger } from '../../config/logger.config';
|
||||||
|
import { RedisCache } from '../../db/redis.client';
|
||||||
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
|
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
|
||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { RepositoryBroker } from '../repository/repository.manager';
|
import { RepositoryBroker } from '../repository/repository.manager';
|
||||||
import { AuthService, OldToken } from '../services/auth.service';
|
import { AuthService, OldToken } from '../services/auth.service';
|
||||||
import { WAMonitoringService } from '../services/monitor.service';
|
|
||||||
import { WAStartupService } from '../services/whatsapp.service';
|
|
||||||
import { WebhookService } from '../services/webhook.service';
|
|
||||||
import { ChatwootService } from '../services/chatwoot.service';
|
import { ChatwootService } from '../services/chatwoot.service';
|
||||||
import { Logger } from '../../config/logger.config';
|
import { WAMonitoringService } from '../services/monitor.service';
|
||||||
import { wa } from '../types/wa.types';
|
|
||||||
import { RedisCache } from '../../db/redis.client';
|
|
||||||
import { isURL } from 'class-validator';
|
|
||||||
import { SettingsService } from '../services/settings.service';
|
import { SettingsService } from '../services/settings.service';
|
||||||
|
import { WebhookService } from '../services/webhook.service';
|
||||||
|
import { WAStartupService } from '../services/whatsapp.service';
|
||||||
|
import { wa } from '../types/wa.types';
|
||||||
|
|
||||||
export class InstanceController {
|
export class InstanceController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly waMonitor: WAMonitoringService,
|
private readonly waMonitor: WAMonitoringService,
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly repository: RepositoryBroker,
|
private readonly repository: RepositoryBroker,
|
||||||
private readonly eventEmitter: EventEmitter2,
|
private readonly eventEmitter: EventEmitter2,
|
||||||
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 settingsService: SettingsService,
|
||||||
private readonly cache: RedisCache,
|
private readonly cache: RedisCache,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
private readonly logger = new Logger(InstanceController.name);
|
private readonly logger = new Logger(InstanceController.name);
|
||||||
|
|
||||||
public async createInstance({
|
public async createInstance({
|
||||||
instanceName,
|
instanceName,
|
||||||
webhook,
|
|
||||||
webhook_by_events,
|
|
||||||
events,
|
|
||||||
qrcode,
|
|
||||||
number,
|
|
||||||
token,
|
|
||||||
chatwoot_account_id,
|
|
||||||
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');
|
|
||||||
|
|
||||||
if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) {
|
|
||||||
throw new BadRequestException(
|
|
||||||
'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');
|
|
||||||
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,
|
|
||||||
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');
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
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,
|
|
||||||
reopen_conversation: chatwoot_reopen_conversation || false,
|
|
||||||
conversation_pending: chatwoot_conversation_pending || false,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.chatwootService.initInstanceChatwoot(
|
|
||||||
instance,
|
|
||||||
instance.instanceName,
|
|
||||||
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
|
||||||
qrcode,
|
|
||||||
number,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.log(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
instance: {
|
|
||||||
instanceName: instance.instanceName,
|
|
||||||
status: 'created',
|
|
||||||
},
|
|
||||||
hash,
|
|
||||||
webhook,
|
webhook,
|
||||||
webhook_by_events,
|
webhook_by_events,
|
||||||
events: getEvents,
|
events,
|
||||||
settings,
|
qrcode,
|
||||||
chatwoot: {
|
number,
|
||||||
enabled: true,
|
token,
|
||||||
account_id: chatwoot_account_id,
|
chatwoot_account_id,
|
||||||
token: chatwoot_token,
|
chatwoot_token,
|
||||||
url: chatwoot_url,
|
chatwoot_url,
|
||||||
sign_msg: chatwoot_sign_msg || false,
|
chatwoot_sign_msg,
|
||||||
reopen_conversation: chatwoot_reopen_conversation || false,
|
chatwoot_reopen_conversation,
|
||||||
conversation_pending: chatwoot_conversation_pending || false,
|
chatwoot_conversation_pending,
|
||||||
number,
|
reject_call,
|
||||||
name_inbox: instance.instanceName,
|
msg_call,
|
||||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
groups_ignore,
|
||||||
},
|
always_online,
|
||||||
};
|
read_messages,
|
||||||
} catch (error) {
|
read_status,
|
||||||
console.log(error);
|
}: InstanceDto) {
|
||||||
return { error: true, message: error.toString() };
|
try {
|
||||||
}
|
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
|
||||||
}
|
|
||||||
|
|
||||||
public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) {
|
if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) {
|
||||||
try {
|
throw new BadRequestException('The instance name must be lowercase and without special characters');
|
||||||
this.logger.verbose(
|
}
|
||||||
'requested connectToWhatsapp from ' + instanceName + ' instance',
|
|
||||||
);
|
|
||||||
|
|
||||||
const instance = this.waMonitor.waInstances[instanceName];
|
this.logger.verbose('checking duplicate token');
|
||||||
const state = instance?.connectionStatus?.state;
|
await this.authService.checkDuplicateToken(token);
|
||||||
|
|
||||||
this.logger.verbose('state: ' + state);
|
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(' ', '');
|
||||||
|
|
||||||
if (state == 'open') {
|
this.logger.verbose('instance: ' + instance.instanceName + ' created');
|
||||||
return await this.connectionState({ instanceName });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == 'connecting') {
|
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||||
return instance.qrCode;
|
this.waMonitor.delInstanceTime(instance.instanceName);
|
||||||
}
|
|
||||||
|
|
||||||
if (state == 'close') {
|
this.logger.verbose('generating hash');
|
||||||
this.logger.verbose('connecting');
|
const hash = await this.authService.generateHash(
|
||||||
await instance.connectToWhatsapp(number);
|
{
|
||||||
|
instanceName: instance.instanceName,
|
||||||
|
},
|
||||||
|
token,
|
||||||
|
);
|
||||||
|
|
||||||
await delay(5000);
|
this.logger.verbose('hash: ' + hash + ' generated');
|
||||||
return instance.qrCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
let getEvents: string[];
|
||||||
instance: {
|
|
||||||
instanceName: instanceName,
|
|
||||||
status: state,
|
|
||||||
},
|
|
||||||
qrcode: instance?.qrCode,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async restartInstance({ instanceName }: InstanceDto) {
|
if (webhook) {
|
||||||
try {
|
if (!isURL(webhook, { require_tld: false })) {
|
||||||
this.logger.verbose('requested restartInstance from ' + instanceName + ' instance');
|
throw new BadRequestException('Invalid "url" property in webhook');
|
||||||
|
}
|
||||||
|
|
||||||
this.logger.verbose('logging out instance: ' + instanceName);
|
this.logger.verbose('creating webhook');
|
||||||
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
try {
|
||||||
|
this.webhookService.create(instance, {
|
||||||
|
enabled: true,
|
||||||
|
url: webhook,
|
||||||
|
events,
|
||||||
|
webhook_by_events,
|
||||||
|
});
|
||||||
|
|
||||||
return { error: false, message: 'Instance restarted' };
|
getEvents = (await this.webhookService.find(instance)).events;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connectionState({ instanceName }: InstanceDto) {
|
this.logger.verbose('creating settings');
|
||||||
this.logger.verbose('requested connectionState from ' + instanceName + ' instance');
|
const settings: wa.LocalSettings = {
|
||||||
return {
|
reject_call: reject_call || false,
|
||||||
instance: {
|
msg_call: msg_call || '',
|
||||||
instanceName: instanceName,
|
groups_ignore: groups_ignore || false,
|
||||||
state: this.waMonitor.waInstances[instanceName]?.connectionStatus?.state,
|
always_online: always_online || false,
|
||||||
},
|
read_messages: read_messages || false,
|
||||||
};
|
read_status: read_status || false,
|
||||||
}
|
};
|
||||||
|
|
||||||
public async fetchInstances({ instanceName }: InstanceDto) {
|
this.logger.verbose('settings: ' + JSON.stringify(settings));
|
||||||
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
|
|
||||||
if (instanceName) {
|
this.settingsService.create(instance, settings);
|
||||||
this.logger.verbose('instanceName: ' + instanceName);
|
|
||||||
return this.waMonitor.instanceInfo(instanceName);
|
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,
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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,
|
||||||
|
reopen_conversation: chatwoot_reopen_conversation || false,
|
||||||
|
conversation_pending: chatwoot_conversation_pending || false,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.chatwootService.initInstanceChatwoot(
|
||||||
|
instance,
|
||||||
|
instance.instanceName,
|
||||||
|
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||||
|
qrcode,
|
||||||
|
number,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.log(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
instance: {
|
||||||
|
instanceName: instance.instanceName,
|
||||||
|
status: 'created',
|
||||||
|
},
|
||||||
|
hash,
|
||||||
|
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}`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return { error: true, message: error.toString() };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.waMonitor.instanceInfo();
|
public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) {
|
||||||
}
|
try {
|
||||||
|
this.logger.verbose('requested connectToWhatsapp from ' + instanceName + ' instance');
|
||||||
|
|
||||||
public async logout({ instanceName }: InstanceDto) {
|
const instance = this.waMonitor.waInstances[instanceName];
|
||||||
this.logger.verbose('requested logout from ' + instanceName + ' instance');
|
const state = instance?.connectionStatus?.state;
|
||||||
const { instance } = await this.connectionState({ instanceName });
|
|
||||||
|
|
||||||
if (instance.state === 'close') {
|
this.logger.verbose('state: ' + state);
|
||||||
throw new BadRequestException(
|
|
||||||
'The "' + instanceName + '" instance is not connected',
|
if (state == 'open') {
|
||||||
);
|
return await this.connectionState({ instanceName });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == 'connecting') {
|
||||||
|
return instance.qrCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == 'close') {
|
||||||
|
this.logger.verbose('connecting');
|
||||||
|
await instance.connectToWhatsapp(number);
|
||||||
|
|
||||||
|
await delay(5000);
|
||||||
|
return instance.qrCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
instance: {
|
||||||
|
instanceName: instanceName,
|
||||||
|
status: state,
|
||||||
|
},
|
||||||
|
qrcode: instance?.qrCode,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
public async restartInstance({ instanceName }: InstanceDto) {
|
||||||
this.logger.verbose('logging out instance: ' + instanceName);
|
try {
|
||||||
await this.waMonitor.waInstances[instanceName]?.client?.logout(
|
this.logger.verbose('requested restartInstance from ' + instanceName + ' instance');
|
||||||
'Log out instance: ' + instanceName,
|
|
||||||
);
|
|
||||||
|
|
||||||
this.logger.verbose('close connection instance: ' + instanceName);
|
this.logger.verbose('logging out instance: ' + instanceName);
|
||||||
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
||||||
|
|
||||||
return { error: false, message: 'Instance logged out' };
|
return { error: false, message: 'Instance restarted' };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new InternalServerErrorException(error.toString());
|
this.logger.error(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public async deleteInstance({ instanceName }: InstanceDto) {
|
public async connectionState({ instanceName }: InstanceDto) {
|
||||||
this.logger.verbose('requested deleteInstance from ' + instanceName + ' instance');
|
this.logger.verbose('requested connectionState from ' + instanceName + ' instance');
|
||||||
const { instance } = await this.connectionState({ instanceName });
|
return {
|
||||||
|
instance: {
|
||||||
if (instance.state === 'open') {
|
instanceName: instanceName,
|
||||||
throw new BadRequestException(
|
state: this.waMonitor.waInstances[instanceName]?.connectionStatus?.state,
|
||||||
'The "' + instanceName + '" instance needs to be disconnected',
|
},
|
||||||
);
|
};
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
if (instance.state === 'connecting') {
|
|
||||||
this.logger.verbose('logging out instance: ' + instanceName);
|
|
||||||
|
|
||||||
await this.logout({ instanceName });
|
public async fetchInstances({ instanceName }: InstanceDto) {
|
||||||
delete this.waMonitor.waInstances[instanceName];
|
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
|
||||||
return { error: false, message: 'Instance deleted' };
|
if (instanceName) {
|
||||||
} else {
|
this.logger.verbose('instanceName: ' + instanceName);
|
||||||
this.logger.verbose('deleting instance: ' + instanceName);
|
return this.waMonitor.instanceInfo(instanceName);
|
||||||
|
}
|
||||||
|
|
||||||
delete this.waMonitor.waInstances[instanceName];
|
return this.waMonitor.instanceInfo();
|
||||||
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
|
|
||||||
return { error: false, message: 'Instance deleted' };
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw new BadRequestException(error.toString());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public async refreshToken(_: InstanceDto, oldToken: OldToken) {
|
public async logout({ instanceName }: InstanceDto) {
|
||||||
this.logger.verbose('requested refreshToken');
|
this.logger.verbose('requested logout from ' + instanceName + ' instance');
|
||||||
return await this.authService.refreshToken(oldToken);
|
const { instance } = await this.connectionState({ instanceName });
|
||||||
}
|
|
||||||
|
if (instance.state === 'close') {
|
||||||
|
throw new BadRequestException('The "' + instanceName + '" instance is not connected');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.logger.verbose('logging out instance: ' + instanceName);
|
||||||
|
await this.waMonitor.waInstances[instanceName]?.client?.logout('Log out instance: ' + instanceName);
|
||||||
|
|
||||||
|
this.logger.verbose('close connection instance: ' + instanceName);
|
||||||
|
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
||||||
|
|
||||||
|
return { error: false, message: 'Instance logged out' };
|
||||||
|
} catch (error) {
|
||||||
|
throw new InternalServerErrorException(error.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async deleteInstance({ instanceName }: InstanceDto) {
|
||||||
|
this.logger.verbose('requested deleteInstance from ' + instanceName + ' instance');
|
||||||
|
const { instance } = await this.connectionState({ instanceName });
|
||||||
|
|
||||||
|
if (instance.state === 'open') {
|
||||||
|
throw new BadRequestException('The "' + instanceName + '" instance needs to be disconnected');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (instance.state === 'connecting') {
|
||||||
|
this.logger.verbose('logging out instance: ' + instanceName);
|
||||||
|
|
||||||
|
await this.logout({ instanceName });
|
||||||
|
delete this.waMonitor.waInstances[instanceName];
|
||||||
|
return { error: false, message: 'Instance deleted' };
|
||||||
|
} else {
|
||||||
|
this.logger.verbose('deleting instance: ' + instanceName);
|
||||||
|
|
||||||
|
delete this.waMonitor.waInstances[instanceName];
|
||||||
|
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
|
||||||
|
return { error: false, message: 'Instance deleted' };
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw new BadRequestException(error.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async refreshToken(_: InstanceDto, oldToken: OldToken) {
|
||||||
|
this.logger.verbose('requested refreshToken');
|
||||||
|
return await this.authService.refreshToken(oldToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,24 @@
|
|||||||
import { isURL } from 'class-validator';
|
// import { isURL } from 'class-validator';
|
||||||
import { BadRequestException } from '../../exceptions';
|
|
||||||
|
import { Logger } from '../../config/logger.config';
|
||||||
|
// import { BadRequestException } from '../../exceptions';
|
||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { SettingsDto } from '../dto/settings.dto';
|
import { SettingsDto } from '../dto/settings.dto';
|
||||||
import { SettingsService } from '../services/settings.service';
|
import { SettingsService } from '../services/settings.service';
|
||||||
import { Logger } from '../../config/logger.config';
|
|
||||||
|
|
||||||
const logger = new Logger('SettingsController');
|
const logger = new Logger('SettingsController');
|
||||||
|
|
||||||
export class SettingsController {
|
export class SettingsController {
|
||||||
constructor(private readonly settingsService: SettingsService) {}
|
constructor(private readonly settingsService: SettingsService) {}
|
||||||
|
|
||||||
public async createSettings(instance: InstanceDto, data: SettingsDto) {
|
public async createSettings(instance: InstanceDto, data: SettingsDto) {
|
||||||
logger.verbose(
|
logger.verbose('requested createSettings from ' + instance.instanceName + ' instance');
|
||||||
'requested createSettings from ' + instance.instanceName + ' instance',
|
|
||||||
);
|
|
||||||
|
|
||||||
return this.settingsService.create(instance, data);
|
return this.settingsService.create(instance, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async findSettings(instance: InstanceDto) {
|
public async findSettings(instance: InstanceDto) {
|
||||||
logger.verbose('requested findSettings from ' + instance.instanceName + ' instance');
|
logger.verbose('requested findSettings from ' + instance.instanceName + ' instance');
|
||||||
return this.settingsService.find(instance);
|
return this.settingsService.find(instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,93 +1,84 @@
|
|||||||
import {
|
import { proto, WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue } from '@whiskeysockets/baileys';
|
||||||
WAPrivacyOnlineValue,
|
|
||||||
WAPrivacyValue,
|
|
||||||
WAReadReceiptsValue,
|
|
||||||
proto,
|
|
||||||
} from '@whiskeysockets/baileys';
|
|
||||||
|
|
||||||
export class OnWhatsAppDto {
|
export class OnWhatsAppDto {
|
||||||
constructor(
|
constructor(public readonly jid: string, public readonly exists: boolean, public readonly name?: string) {}
|
||||||
public readonly jid: string,
|
|
||||||
public readonly exists: boolean,
|
|
||||||
public readonly name?: string,
|
|
||||||
) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class getBase64FromMediaMessageDto {
|
export class getBase64FromMediaMessageDto {
|
||||||
message: proto.WebMessageInfo;
|
message: proto.WebMessageInfo;
|
||||||
convertToMp4?: boolean;
|
convertToMp4?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WhatsAppNumberDto {
|
export class WhatsAppNumberDto {
|
||||||
numbers: string[];
|
numbers: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NumberDto {
|
export class NumberDto {
|
||||||
number: string;
|
number: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NumberBusiness {
|
export class NumberBusiness {
|
||||||
wid?: string;
|
wid?: string;
|
||||||
jid?: string;
|
jid?: string;
|
||||||
exists?: boolean;
|
exists?: boolean;
|
||||||
isBusiness: boolean;
|
isBusiness: boolean;
|
||||||
name?: string;
|
name?: string;
|
||||||
message?: string;
|
message?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
website?: string[];
|
website?: string[];
|
||||||
address?: string;
|
address?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ProfileNameDto {
|
export class ProfileNameDto {
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ProfileStatusDto {
|
export class ProfileStatusDto {
|
||||||
status: string;
|
status: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ProfilePictureDto {
|
export class ProfilePictureDto {
|
||||||
number?: string;
|
number?: string;
|
||||||
// url or base64
|
// url or base64
|
||||||
picture?: string;
|
picture?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Key {
|
class Key {
|
||||||
id: string;
|
id: string;
|
||||||
fromMe: boolean;
|
fromMe: boolean;
|
||||||
remoteJid: string;
|
remoteJid: string;
|
||||||
}
|
}
|
||||||
export class ReadMessageDto {
|
export class ReadMessageDto {
|
||||||
read_messages: Key[];
|
read_messages: Key[];
|
||||||
}
|
}
|
||||||
|
|
||||||
class LastMessage {
|
class LastMessage {
|
||||||
key: Key;
|
key: Key;
|
||||||
messageTimestamp?: number;
|
messageTimestamp?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ArchiveChatDto {
|
export class ArchiveChatDto {
|
||||||
lastMessage: LastMessage;
|
lastMessage: LastMessage;
|
||||||
archive: boolean;
|
archive: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
class PrivacySetting {
|
class PrivacySetting {
|
||||||
readreceipts: WAReadReceiptsValue;
|
readreceipts: WAReadReceiptsValue;
|
||||||
profile: WAPrivacyValue;
|
profile: WAPrivacyValue;
|
||||||
status: WAPrivacyValue;
|
status: WAPrivacyValue;
|
||||||
online: WAPrivacyOnlineValue;
|
online: WAPrivacyOnlineValue;
|
||||||
last: WAPrivacyValue;
|
last: WAPrivacyValue;
|
||||||
groupadd: WAPrivacyValue;
|
groupadd: WAPrivacyValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class PrivacySettingDto {
|
export class PrivacySettingDto {
|
||||||
privacySettings: PrivacySetting;
|
privacySettings: PrivacySetting;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DeleteMessage {
|
export class DeleteMessage {
|
||||||
id: string;
|
id: string;
|
||||||
fromMe: boolean;
|
fromMe: boolean;
|
||||||
remoteJid: string;
|
remoteJid: string;
|
||||||
participant?: string;
|
participant?: string;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
export class ChatwootDto {
|
export class ChatwootDto {
|
||||||
enabled?: boolean;
|
enabled?: boolean;
|
||||||
account_id?: string;
|
account_id?: string;
|
||||||
token?: string;
|
token?: string;
|
||||||
url?: string;
|
url?: string;
|
||||||
name_inbox?: string;
|
name_inbox?: string;
|
||||||
sign_msg?: boolean;
|
sign_msg?: boolean;
|
||||||
number?: string;
|
number?: string;
|
||||||
reopen_conversation?: boolean;
|
reopen_conversation?: boolean;
|
||||||
conversation_pending?: boolean;
|
conversation_pending?: boolean;
|
||||||
}
|
}
|
||||||
|
@ -1,52 +1,52 @@
|
|||||||
export class CreateGroupDto {
|
export class CreateGroupDto {
|
||||||
subject: string;
|
subject: string;
|
||||||
participants: string[];
|
participants: string[];
|
||||||
description?: string;
|
description?: string;
|
||||||
promoteParticipants?: boolean;
|
promoteParticipants?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupPictureDto {
|
export class GroupPictureDto {
|
||||||
groupJid: string;
|
groupJid: string;
|
||||||
image: string;
|
image: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupSubjectDto {
|
export class GroupSubjectDto {
|
||||||
groupJid: string;
|
groupJid: string;
|
||||||
subject: string;
|
subject: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupDescriptionDto {
|
export class GroupDescriptionDto {
|
||||||
groupJid: string;
|
groupJid: string;
|
||||||
description: string;
|
description: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupJid {
|
export class GroupJid {
|
||||||
groupJid: string;
|
groupJid: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GetParticipant {
|
export class GetParticipant {
|
||||||
getParticipants: string;
|
getParticipants: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupInvite {
|
export class GroupInvite {
|
||||||
inviteCode: string;
|
inviteCode: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupSendInvite {
|
export class GroupSendInvite {
|
||||||
groupJid: string;
|
groupJid: string;
|
||||||
description: string;
|
description: string;
|
||||||
numbers: string[];
|
numbers: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupUpdateParticipantDto extends GroupJid {
|
export class GroupUpdateParticipantDto extends GroupJid {
|
||||||
action: 'add' | 'remove' | 'promote' | 'demote';
|
action: 'add' | 'remove' | 'promote' | 'demote';
|
||||||
participants: string[];
|
participants: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupUpdateSettingDto extends GroupJid {
|
export class GroupUpdateSettingDto extends GroupJid {
|
||||||
action: 'announcement' | 'not_announcement' | 'unlocked' | 'locked';
|
action: 'announcement' | 'not_announcement' | 'unlocked' | 'locked';
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupToggleEphemeralDto extends GroupJid {
|
export class GroupToggleEphemeralDto extends GroupJid {
|
||||||
expiration: 0 | 86400 | 604800 | 7776000;
|
expiration: 0 | 86400 | 604800 | 7776000;
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
export class InstanceDto {
|
export class InstanceDto {
|
||||||
instanceName: string;
|
instanceName: string;
|
||||||
qrcode?: boolean;
|
qrcode?: boolean;
|
||||||
number?: string;
|
number?: string;
|
||||||
token?: string;
|
token?: string;
|
||||||
webhook?: string;
|
webhook?: string;
|
||||||
webhook_by_events?: boolean;
|
webhook_by_events?: boolean;
|
||||||
events?: string[];
|
events?: string[];
|
||||||
reject_call?: boolean;
|
reject_call?: boolean;
|
||||||
msg_call?: string;
|
msg_call?: string;
|
||||||
groups_ignore?: boolean;
|
groups_ignore?: boolean;
|
||||||
always_online?: boolean;
|
always_online?: boolean;
|
||||||
read_messages?: boolean;
|
read_messages?: boolean;
|
||||||
read_status?: boolean;
|
read_status?: boolean;
|
||||||
chatwoot_account_id?: string;
|
chatwoot_account_id?: string;
|
||||||
chatwoot_token?: string;
|
chatwoot_token?: string;
|
||||||
chatwoot_url?: string;
|
chatwoot_url?: string;
|
||||||
chatwoot_sign_msg?: boolean;
|
chatwoot_sign_msg?: boolean;
|
||||||
chatwoot_reopen_conversation?: boolean;
|
chatwoot_reopen_conversation?: boolean;
|
||||||
chatwoot_conversation_pending?: boolean;
|
chatwoot_conversation_pending?: boolean;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
export class SettingsDto {
|
export class SettingsDto {
|
||||||
reject_call?: boolean;
|
reject_call?: boolean;
|
||||||
msg_call?: string;
|
msg_call?: string;
|
||||||
groups_ignore?: boolean;
|
groups_ignore?: boolean;
|
||||||
always_online?: boolean;
|
always_online?: boolean;
|
||||||
read_messages?: boolean;
|
read_messages?: boolean;
|
||||||
read_status?: boolean;
|
read_status?: boolean;
|
||||||
}
|
}
|
||||||
|
@ -1,33 +1,30 @@
|
|||||||
import { Schema } from 'mongoose';
|
import { Schema } from 'mongoose';
|
||||||
|
|
||||||
import { dbserver } from '../../db/db.connect';
|
import { dbserver } from '../../db/db.connect';
|
||||||
|
|
||||||
export class ChatwootRaw {
|
export class ChatwootRaw {
|
||||||
_id?: string;
|
_id?: string;
|
||||||
enabled?: boolean;
|
enabled?: boolean;
|
||||||
account_id?: string;
|
account_id?: string;
|
||||||
token?: string;
|
token?: string;
|
||||||
url?: string;
|
url?: string;
|
||||||
name_inbox?: string;
|
name_inbox?: string;
|
||||||
sign_msg?: boolean;
|
sign_msg?: boolean;
|
||||||
number?: string;
|
number?: string;
|
||||||
reopen_conversation?: boolean;
|
reopen_conversation?: boolean;
|
||||||
conversation_pending?: boolean;
|
conversation_pending?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chatwootSchema = new Schema<ChatwootRaw>({
|
const chatwootSchema = new Schema<ChatwootRaw>({
|
||||||
_id: { type: String, _id: true },
|
_id: { type: String, _id: true },
|
||||||
enabled: { type: Boolean, required: true },
|
enabled: { type: Boolean, required: true },
|
||||||
account_id: { type: String, required: true },
|
account_id: { type: String, required: true },
|
||||||
token: { type: String, required: true },
|
token: { type: String, required: true },
|
||||||
url: { type: String, required: true },
|
url: { type: String, required: true },
|
||||||
name_inbox: { type: String, required: true },
|
name_inbox: { type: String, required: true },
|
||||||
sign_msg: { type: Boolean, required: true },
|
sign_msg: { type: Boolean, required: true },
|
||||||
number: { type: String, required: true },
|
number: { type: String, required: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
export const ChatwootModel = dbserver?.model(
|
export const ChatwootModel = dbserver?.model(ChatwootRaw.name, chatwootSchema, 'chatwoot');
|
||||||
ChatwootRaw.name,
|
|
||||||
chatwootSchema,
|
|
||||||
'chatwoot',
|
|
||||||
);
|
|
||||||
export type IChatwootModel = typeof ChatwootModel;
|
export type IChatwootModel = typeof ChatwootModel;
|
||||||
|
@ -1,29 +1,26 @@
|
|||||||
import { Schema } from 'mongoose';
|
import { Schema } from 'mongoose';
|
||||||
|
|
||||||
import { dbserver } from '../../db/db.connect';
|
import { dbserver } from '../../db/db.connect';
|
||||||
|
|
||||||
export class SettingsRaw {
|
export class SettingsRaw {
|
||||||
_id?: string;
|
_id?: string;
|
||||||
reject_call?: boolean;
|
reject_call?: boolean;
|
||||||
msg_call?: string;
|
msg_call?: string;
|
||||||
groups_ignore?: boolean;
|
groups_ignore?: boolean;
|
||||||
always_online?: boolean;
|
always_online?: boolean;
|
||||||
read_messages?: boolean;
|
read_messages?: boolean;
|
||||||
read_status?: boolean;
|
read_status?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const settingsSchema = new Schema<SettingsRaw>({
|
const settingsSchema = new Schema<SettingsRaw>({
|
||||||
_id: { type: String, _id: true },
|
_id: { type: String, _id: true },
|
||||||
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 },
|
always_online: { type: Boolean, required: true },
|
||||||
read_messages: { type: Boolean, required: true },
|
read_messages: { type: Boolean, required: true },
|
||||||
read_status: { type: Boolean, required: true },
|
read_status: { type: Boolean, required: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
export const SettingsModel = dbserver?.model(
|
export const SettingsModel = dbserver?.model(SettingsRaw.name, settingsSchema, 'settings');
|
||||||
SettingsRaw.name,
|
|
||||||
settingsSchema,
|
|
||||||
'settings',
|
|
||||||
);
|
|
||||||
export type ISettingsModel = typeof SettingsModel;
|
export type ISettingsModel = typeof SettingsModel;
|
||||||
|
@ -1,121 +1,117 @@
|
|||||||
import { MessageRepository } from './message.repository';
|
|
||||||
import { ChatRepository } from './chat.repository';
|
|
||||||
import { ContactRepository } from './contact.repository';
|
|
||||||
import { MessageUpRepository } from './messageUp.repository';
|
|
||||||
import { MongoClient } from 'mongodb';
|
|
||||||
import { WebhookRepository } from './webhook.repository';
|
|
||||||
import { ChatwootRepository } from './chatwoot.repository';
|
|
||||||
import { SettingsRepository } from './settings.repository';
|
|
||||||
|
|
||||||
import { AuthRepository } from './auth.repository';
|
|
||||||
import { Auth, ConfigService, Database } from '../../config/env.config';
|
|
||||||
import { join } from 'path';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import { MongoClient } from 'mongodb';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
|
import { Auth, ConfigService, Database } from '../../config/env.config';
|
||||||
import { Logger } from '../../config/logger.config';
|
import { Logger } from '../../config/logger.config';
|
||||||
|
import { AuthRepository } from './auth.repository';
|
||||||
|
import { ChatRepository } from './chat.repository';
|
||||||
|
import { ChatwootRepository } from './chatwoot.repository';
|
||||||
|
import { ContactRepository } from './contact.repository';
|
||||||
|
import { MessageRepository } from './message.repository';
|
||||||
|
import { MessageUpRepository } from './messageUp.repository';
|
||||||
|
import { SettingsRepository } from './settings.repository';
|
||||||
|
import { WebhookRepository } from './webhook.repository';
|
||||||
export class RepositoryBroker {
|
export class RepositoryBroker {
|
||||||
constructor(
|
constructor(
|
||||||
public readonly message: MessageRepository,
|
public readonly message: MessageRepository,
|
||||||
public readonly chat: ChatRepository,
|
public readonly chat: ChatRepository,
|
||||||
public readonly contact: ContactRepository,
|
public readonly contact: ContactRepository,
|
||||||
public readonly messageUpdate: MessageUpRepository,
|
public readonly messageUpdate: MessageUpRepository,
|
||||||
public readonly webhook: WebhookRepository,
|
public readonly webhook: WebhookRepository,
|
||||||
public readonly chatwoot: ChatwootRepository,
|
public readonly chatwoot: ChatwootRepository,
|
||||||
public readonly settings: SettingsRepository,
|
public readonly settings: SettingsRepository,
|
||||||
public readonly auth: AuthRepository,
|
public readonly auth: AuthRepository,
|
||||||
private configService: ConfigService,
|
private configService: ConfigService,
|
||||||
dbServer?: MongoClient,
|
dbServer?: MongoClient,
|
||||||
) {
|
) {
|
||||||
this.dbClient = dbServer;
|
this.dbClient = dbServer;
|
||||||
this.__init_repo_without_db__();
|
this.__init_repo_without_db__();
|
||||||
}
|
}
|
||||||
|
|
||||||
private dbClient?: MongoClient;
|
private dbClient?: MongoClient;
|
||||||
private readonly logger = new Logger('RepositoryBroker');
|
private readonly logger = new Logger('RepositoryBroker');
|
||||||
|
|
||||||
public get dbServer() {
|
public get dbServer() {
|
||||||
return this.dbClient;
|
return this.dbClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
private __init_repo_without_db__() {
|
private __init_repo_without_db__() {
|
||||||
this.logger.verbose('initializing repository without db');
|
this.logger.verbose('initializing repository without db');
|
||||||
if (!this.configService.get<Database>('DATABASE').ENABLED) {
|
if (!this.configService.get<Database>('DATABASE').ENABLED) {
|
||||||
const storePath = join(process.cwd(), 'store');
|
const storePath = join(process.cwd(), 'store');
|
||||||
|
|
||||||
this.logger.verbose('creating store path: ' + storePath);
|
this.logger.verbose('creating store path: ' + storePath);
|
||||||
try {
|
try {
|
||||||
const authDir = join(
|
const authDir = join(storePath, 'auth', this.configService.get<Auth>('AUTHENTICATION').TYPE);
|
||||||
storePath,
|
const chatsDir = join(storePath, 'chats');
|
||||||
'auth',
|
const contactsDir = join(storePath, 'contacts');
|
||||||
this.configService.get<Auth>('AUTHENTICATION').TYPE,
|
const messagesDir = join(storePath, 'messages');
|
||||||
);
|
const messageUpDir = join(storePath, 'message-up');
|
||||||
const chatsDir = join(storePath, 'chats');
|
const webhookDir = join(storePath, 'webhook');
|
||||||
const contactsDir = join(storePath, 'contacts');
|
const chatwootDir = join(storePath, 'chatwoot');
|
||||||
const messagesDir = join(storePath, 'messages');
|
const settingsDir = join(storePath, 'settings');
|
||||||
const messageUpDir = join(storePath, 'message-up');
|
const tempDir = join(storePath, 'temp');
|
||||||
const webhookDir = join(storePath, 'webhook');
|
|
||||||
const chatwootDir = join(storePath, 'chatwoot');
|
if (!fs.existsSync(authDir)) {
|
||||||
const settingsDir = join(storePath, 'settings');
|
this.logger.verbose('creating auth dir: ' + authDir);
|
||||||
const tempDir = join(storePath, 'temp');
|
fs.mkdirSync(authDir, { recursive: true });
|
||||||
|
}
|
||||||
if (!fs.existsSync(authDir)) {
|
if (!fs.existsSync(chatsDir)) {
|
||||||
this.logger.verbose('creating auth dir: ' + authDir);
|
this.logger.verbose('creating chats dir: ' + chatsDir);
|
||||||
fs.mkdirSync(authDir, { recursive: true });
|
fs.mkdirSync(chatsDir, { recursive: true });
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(chatsDir)) {
|
if (!fs.existsSync(contactsDir)) {
|
||||||
this.logger.verbose('creating chats dir: ' + chatsDir);
|
this.logger.verbose('creating contacts dir: ' + contactsDir);
|
||||||
fs.mkdirSync(chatsDir, { recursive: true });
|
fs.mkdirSync(contactsDir, { recursive: true });
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(contactsDir)) {
|
if (!fs.existsSync(messagesDir)) {
|
||||||
this.logger.verbose('creating contacts dir: ' + contactsDir);
|
this.logger.verbose('creating messages dir: ' + messagesDir);
|
||||||
fs.mkdirSync(contactsDir, { recursive: true });
|
fs.mkdirSync(messagesDir, { recursive: true });
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(messagesDir)) {
|
if (!fs.existsSync(messageUpDir)) {
|
||||||
this.logger.verbose('creating messages dir: ' + messagesDir);
|
this.logger.verbose('creating message-up dir: ' + messageUpDir);
|
||||||
fs.mkdirSync(messagesDir, { recursive: true });
|
fs.mkdirSync(messageUpDir, { recursive: true });
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(messageUpDir)) {
|
if (!fs.existsSync(webhookDir)) {
|
||||||
this.logger.verbose('creating message-up dir: ' + messageUpDir);
|
this.logger.verbose('creating webhook dir: ' + webhookDir);
|
||||||
fs.mkdirSync(messageUpDir, { recursive: true });
|
fs.mkdirSync(webhookDir, { recursive: true });
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(webhookDir)) {
|
if (!fs.existsSync(chatwootDir)) {
|
||||||
this.logger.verbose('creating webhook dir: ' + webhookDir);
|
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
|
||||||
fs.mkdirSync(webhookDir, { recursive: true });
|
fs.mkdirSync(chatwootDir, { recursive: true });
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(chatwootDir)) {
|
if (!fs.existsSync(settingsDir)) {
|
||||||
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
|
this.logger.verbose('creating settings dir: ' + settingsDir);
|
||||||
fs.mkdirSync(chatwootDir, { recursive: true });
|
fs.mkdirSync(settingsDir, { recursive: true });
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(settingsDir)) {
|
if (!fs.existsSync(tempDir)) {
|
||||||
this.logger.verbose('creating settings dir: ' + settingsDir);
|
this.logger.verbose('creating temp dir: ' + tempDir);
|
||||||
fs.mkdirSync(settingsDir, { recursive: true });
|
fs.mkdirSync(tempDir, { recursive: true });
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(tempDir)) {
|
} catch (error) {
|
||||||
this.logger.verbose('creating temp dir: ' + tempDir);
|
this.logger.error(error);
|
||||||
fs.mkdirSync(tempDir, { recursive: true });
|
}
|
||||||
}
|
} else {
|
||||||
} catch (error) {
|
try {
|
||||||
this.logger.error(error);
|
const storePath = join(process.cwd(), 'store');
|
||||||
}
|
|
||||||
} else {
|
this.logger.verbose('creating store path: ' + storePath);
|
||||||
const storePath = join(process.cwd(), 'store');
|
|
||||||
|
const tempDir = join(storePath, 'temp');
|
||||||
this.logger.verbose('creating store path: ' + storePath);
|
const chatwootDir = join(storePath, 'chatwoot');
|
||||||
|
|
||||||
const tempDir = join(storePath, 'temp');
|
if (!fs.existsSync(chatwootDir)) {
|
||||||
const chatwootDir = join(storePath, 'chatwoot');
|
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
|
||||||
|
fs.mkdirSync(chatwootDir, { recursive: true });
|
||||||
if (!fs.existsSync(chatwootDir)) {
|
}
|
||||||
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
|
if (!fs.existsSync(tempDir)) {
|
||||||
fs.mkdirSync(chatwootDir, { recursive: true });
|
this.logger.verbose('creating temp dir: ' + tempDir);
|
||||||
}
|
fs.mkdirSync(tempDir, { recursive: true });
|
||||||
if (!fs.existsSync(tempDir)) {
|
}
|
||||||
this.logger.verbose('creating temp dir: ' + tempDir);
|
} catch (error) {
|
||||||
fs.mkdirSync(tempDir, { recursive: true });
|
this.logger.error(error);
|
||||||
}
|
}
|
||||||
try {
|
}
|
||||||
} catch (error) {
|
|
||||||
this.logger.error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import { chatwootSchema, instanceNameSchema } from '../../validate/validate.sche
|
|||||||
import { RouterBroker } from '../abstract/abstract.router';
|
import { RouterBroker } from '../abstract/abstract.router';
|
||||||
import { ChatwootDto } from '../dto/chatwoot.dto';
|
import { ChatwootDto } from '../dto/chatwoot.dto';
|
||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { ChatwootService } from '../services/chatwoot.service';
|
// import { ChatwootService } from '../services/chatwoot.service';
|
||||||
import { chatwootController } from '../whatsapp.module';
|
import { chatwootController } from '../whatsapp.module';
|
||||||
import { HttpStatus } from './index.router';
|
import { HttpStatus } from './index.router';
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import { instanceNameSchema, settingsSchema } from '../../validate/validate.sche
|
|||||||
import { RouterBroker } from '../abstract/abstract.router';
|
import { RouterBroker } from '../abstract/abstract.router';
|
||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { SettingsDto } from '../dto/settings.dto';
|
import { SettingsDto } from '../dto/settings.dto';
|
||||||
import { SettingsService } from '../services/settings.service';
|
// import { SettingsService } from '../services/settings.service';
|
||||||
import { settingsController } from '../whatsapp.module';
|
import { settingsController } from '../whatsapp.module';
|
||||||
import { HttpStatus } from './index.router';
|
import { HttpStatus } from './index.router';
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -2,7 +2,6 @@ import { execSync } from 'child_process';
|
|||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
import { opendirSync, readdirSync, rmSync } from 'fs';
|
import { opendirSync, readdirSync, rmSync } from 'fs';
|
||||||
import { Db } from 'mongodb';
|
import { Db } from 'mongodb';
|
||||||
import mongoose from 'mongoose';
|
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
|
|
||||||
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
|
import { Auth, ConfigService, Database, DelInstance, HttpServer, Redis } from '../../config/env.config';
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -2,101 +2,88 @@
|
|||||||
import { AuthenticationState, WAConnectionState } from '@whiskeysockets/baileys';
|
import { AuthenticationState, WAConnectionState } from '@whiskeysockets/baileys';
|
||||||
|
|
||||||
export enum Events {
|
export enum Events {
|
||||||
APPLICATION_STARTUP = 'application.startup',
|
APPLICATION_STARTUP = 'application.startup',
|
||||||
QRCODE_UPDATED = 'qrcode.updated',
|
QRCODE_UPDATED = 'qrcode.updated',
|
||||||
CONNECTION_UPDATE = 'connection.update',
|
CONNECTION_UPDATE = 'connection.update',
|
||||||
STATUS_INSTANCE = 'status.instance',
|
STATUS_INSTANCE = 'status.instance',
|
||||||
MESSAGES_SET = 'messages.set',
|
MESSAGES_SET = 'messages.set',
|
||||||
MESSAGES_UPSERT = 'messages.upsert',
|
MESSAGES_UPSERT = 'messages.upsert',
|
||||||
MESSAGES_UPDATE = 'messages.update',
|
MESSAGES_UPDATE = 'messages.update',
|
||||||
MESSAGES_DELETE = 'messages.delete',
|
MESSAGES_DELETE = 'messages.delete',
|
||||||
SEND_MESSAGE = 'send.message',
|
SEND_MESSAGE = 'send.message',
|
||||||
CONTACTS_SET = 'contacts.set',
|
CONTACTS_SET = 'contacts.set',
|
||||||
CONTACTS_UPSERT = 'contacts.upsert',
|
CONTACTS_UPSERT = 'contacts.upsert',
|
||||||
CONTACTS_UPDATE = 'contacts.update',
|
CONTACTS_UPDATE = 'contacts.update',
|
||||||
PRESENCE_UPDATE = 'presence.update',
|
PRESENCE_UPDATE = 'presence.update',
|
||||||
CHATS_SET = 'chats.set',
|
CHATS_SET = 'chats.set',
|
||||||
CHATS_UPDATE = 'chats.update',
|
CHATS_UPDATE = 'chats.update',
|
||||||
CHATS_UPSERT = 'chats.upsert',
|
CHATS_UPSERT = 'chats.upsert',
|
||||||
CHATS_DELETE = 'chats.delete',
|
CHATS_DELETE = 'chats.delete',
|
||||||
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',
|
CALL = 'call',
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare namespace wa {
|
export declare namespace wa {
|
||||||
export type QrCode = {
|
export type QrCode = {
|
||||||
count?: number;
|
count?: number;
|
||||||
pairingCode?: string;
|
pairingCode?: string;
|
||||||
base64?: string;
|
base64?: string;
|
||||||
code?: string;
|
code?: string;
|
||||||
};
|
};
|
||||||
export type Instance = {
|
export type Instance = {
|
||||||
qrcode?: QrCode;
|
qrcode?: QrCode;
|
||||||
pairingCode?: string;
|
pairingCode?: string;
|
||||||
authState?: { state: AuthenticationState; saveCreds: () => void };
|
authState?: { state: AuthenticationState; saveCreds: () => void };
|
||||||
name?: string;
|
name?: string;
|
||||||
wuid?: string;
|
wuid?: string;
|
||||||
profileName?: string;
|
profileName?: string;
|
||||||
profilePictureUrl?: string;
|
profilePictureUrl?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LocalWebHook = {
|
export type LocalWebHook = {
|
||||||
enabled?: boolean;
|
enabled?: boolean;
|
||||||
url?: string;
|
url?: string;
|
||||||
events?: string[];
|
events?: string[];
|
||||||
webhook_by_events?: boolean;
|
webhook_by_events?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LocalChatwoot = {
|
export type LocalChatwoot = {
|
||||||
enabled?: boolean;
|
enabled?: boolean;
|
||||||
account_id?: string;
|
account_id?: string;
|
||||||
token?: string;
|
token?: string;
|
||||||
url?: string;
|
url?: string;
|
||||||
name_inbox?: string;
|
name_inbox?: string;
|
||||||
sign_msg?: boolean;
|
sign_msg?: boolean;
|
||||||
number?: string;
|
number?: string;
|
||||||
reopen_conversation?: boolean;
|
reopen_conversation?: boolean;
|
||||||
conversation_pending?: boolean;
|
conversation_pending?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LocalSettings = {
|
export type LocalSettings = {
|
||||||
reject_call?: boolean;
|
reject_call?: boolean;
|
||||||
msg_call?: string;
|
msg_call?: string;
|
||||||
groups_ignore?: boolean;
|
groups_ignore?: boolean;
|
||||||
always_online?: boolean;
|
always_online?: boolean;
|
||||||
read_messages?: boolean;
|
read_messages?: boolean;
|
||||||
read_status?: boolean;
|
read_status?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type StateConnection = {
|
export type StateConnection = {
|
||||||
instance?: string;
|
instance?: string;
|
||||||
state?: WAConnectionState | 'refused';
|
state?: WAConnectionState | 'refused';
|
||||||
statusReason?: number;
|
statusReason?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type StatusMessage =
|
export type StatusMessage = 'ERROR' | 'PENDING' | 'SERVER_ACK' | 'DELIVERY_ACK' | 'READ' | 'DELETED' | 'PLAYED';
|
||||||
| 'ERROR'
|
|
||||||
| 'PENDING'
|
|
||||||
| 'SERVER_ACK'
|
|
||||||
| 'DELIVERY_ACK'
|
|
||||||
| 'READ'
|
|
||||||
| 'DELETED'
|
|
||||||
| 'PLAYED';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TypeMediaMessage = [
|
export const TypeMediaMessage = ['imageMessage', 'documentMessage', 'audioMessage', 'videoMessage', 'stickerMessage'];
|
||||||
'imageMessage',
|
|
||||||
'documentMessage',
|
|
||||||
'audioMessage',
|
|
||||||
'videoMessage',
|
|
||||||
'stickerMessage',
|
|
||||||
];
|
|
||||||
|
|
||||||
export const MessageSubtype = [
|
export const MessageSubtype = [
|
||||||
'ephemeralMessage',
|
'ephemeralMessage',
|
||||||
'documentWithCaptionMessage',
|
'documentWithCaptionMessage',
|
||||||
'viewOnceMessage',
|
'viewOnceMessage',
|
||||||
'viewOnceMessageV2',
|
'viewOnceMessageV2',
|
||||||
];
|
];
|
||||||
|
@ -1,43 +1,40 @@
|
|||||||
import { Auth, configService } from '../config/env.config';
|
import { configService } from '../config/env.config';
|
||||||
import { Logger } from '../config/logger.config';
|
|
||||||
import { eventEmitter } from '../config/event.config';
|
import { eventEmitter } from '../config/event.config';
|
||||||
import { MessageRepository } from './repository/message.repository';
|
import { Logger } from '../config/logger.config';
|
||||||
import { WAMonitoringService } from './services/monitor.service';
|
import { dbserver } from '../db/db.connect';
|
||||||
import { ChatRepository } from './repository/chat.repository';
|
import { RedisCache } from '../db/redis.client';
|
||||||
import { ContactRepository } from './repository/contact.repository';
|
|
||||||
import { MessageUpRepository } from './repository/messageUp.repository';
|
|
||||||
import { ChatController } from './controllers/chat.controller';
|
import { ChatController } from './controllers/chat.controller';
|
||||||
|
import { ChatwootController } from './controllers/chatwoot.controller';
|
||||||
|
import { GroupController } from './controllers/group.controller';
|
||||||
import { InstanceController } from './controllers/instance.controller';
|
import { InstanceController } from './controllers/instance.controller';
|
||||||
import { SendMessageController } from './controllers/sendMessage.controller';
|
import { SendMessageController } from './controllers/sendMessage.controller';
|
||||||
import { AuthService } from './services/auth.service';
|
|
||||||
import { GroupController } from './controllers/group.controller';
|
|
||||||
import { ViewsController } from './controllers/views.controller';
|
|
||||||
import { WebhookService } from './services/webhook.service';
|
|
||||||
import { WebhookController } from './controllers/webhook.controller';
|
|
||||||
import { ChatwootService } from './services/chatwoot.service';
|
|
||||||
import { ChatwootController } from './controllers/chatwoot.controller';
|
|
||||||
import { RepositoryBroker } from './repository/repository.manager';
|
|
||||||
import {
|
|
||||||
AuthModel,
|
|
||||||
ChatModel,
|
|
||||||
ContactModel,
|
|
||||||
MessageModel,
|
|
||||||
MessageUpModel,
|
|
||||||
ChatwootModel,
|
|
||||||
WebhookModel,
|
|
||||||
SettingsModel,
|
|
||||||
} from './models';
|
|
||||||
import { dbserver } from '../db/db.connect';
|
|
||||||
import { WebhookRepository } from './repository/webhook.repository';
|
|
||||||
import { ChatwootRepository } from './repository/chatwoot.repository';
|
|
||||||
import { AuthRepository } from './repository/auth.repository';
|
|
||||||
import { WAStartupService } from './services/whatsapp.service';
|
|
||||||
import { delay } from '@whiskeysockets/baileys';
|
|
||||||
import { Events } from './types/wa.types';
|
|
||||||
import { RedisCache } from '../db/redis.client';
|
|
||||||
import { SettingsRepository } from './repository/settings.repository';
|
|
||||||
import { SettingsService } from './services/settings.service';
|
|
||||||
import { SettingsController } from './controllers/settings.controller';
|
import { SettingsController } from './controllers/settings.controller';
|
||||||
|
import { ViewsController } from './controllers/views.controller';
|
||||||
|
import { WebhookController } from './controllers/webhook.controller';
|
||||||
|
import {
|
||||||
|
AuthModel,
|
||||||
|
ChatModel,
|
||||||
|
ChatwootModel,
|
||||||
|
ContactModel,
|
||||||
|
MessageModel,
|
||||||
|
MessageUpModel,
|
||||||
|
SettingsModel,
|
||||||
|
WebhookModel,
|
||||||
|
} from './models';
|
||||||
|
import { AuthRepository } from './repository/auth.repository';
|
||||||
|
import { ChatRepository } from './repository/chat.repository';
|
||||||
|
import { ChatwootRepository } from './repository/chatwoot.repository';
|
||||||
|
import { ContactRepository } from './repository/contact.repository';
|
||||||
|
import { MessageRepository } from './repository/message.repository';
|
||||||
|
import { MessageUpRepository } from './repository/messageUp.repository';
|
||||||
|
import { RepositoryBroker } from './repository/repository.manager';
|
||||||
|
import { SettingsRepository } from './repository/settings.repository';
|
||||||
|
import { WebhookRepository } from './repository/webhook.repository';
|
||||||
|
import { AuthService } from './services/auth.service';
|
||||||
|
import { ChatwootService } from './services/chatwoot.service';
|
||||||
|
import { WAMonitoringService } from './services/monitor.service';
|
||||||
|
import { SettingsService } from './services/settings.service';
|
||||||
|
import { WebhookService } from './services/webhook.service';
|
||||||
|
|
||||||
const logger = new Logger('WA MODULE');
|
const logger = new Logger('WA MODULE');
|
||||||
|
|
||||||
@ -51,26 +48,21 @@ const settingsRepository = new SettingsRepository(SettingsModel, configService);
|
|||||||
const authRepository = new AuthRepository(AuthModel, configService);
|
const authRepository = new AuthRepository(AuthModel, configService);
|
||||||
|
|
||||||
export const repository = new RepositoryBroker(
|
export const repository = new RepositoryBroker(
|
||||||
messageRepository,
|
messageRepository,
|
||||||
chatRepository,
|
chatRepository,
|
||||||
contactRepository,
|
contactRepository,
|
||||||
messageUpdateRepository,
|
messageUpdateRepository,
|
||||||
webhookRepository,
|
webhookRepository,
|
||||||
chatwootRepository,
|
chatwootRepository,
|
||||||
settingsRepository,
|
settingsRepository,
|
||||||
authRepository,
|
authRepository,
|
||||||
configService,
|
configService,
|
||||||
dbserver?.getClient(),
|
dbserver?.getClient(),
|
||||||
);
|
);
|
||||||
|
|
||||||
export const cache = new RedisCache();
|
export const cache = new RedisCache();
|
||||||
|
|
||||||
export const waMonitor = new WAMonitoringService(
|
export const waMonitor = new WAMonitoringService(eventEmitter, configService, repository, cache);
|
||||||
eventEmitter,
|
|
||||||
configService,
|
|
||||||
repository,
|
|
||||||
cache,
|
|
||||||
);
|
|
||||||
|
|
||||||
const authService = new AuthService(configService, waMonitor, repository);
|
const authService = new AuthService(configService, waMonitor, repository);
|
||||||
|
|
||||||
@ -87,15 +79,15 @@ const settingsService = new SettingsService(waMonitor);
|
|||||||
export const settingsController = new SettingsController(settingsService);
|
export const settingsController = new SettingsController(settingsService);
|
||||||
|
|
||||||
export const instanceController = new InstanceController(
|
export const instanceController = new InstanceController(
|
||||||
waMonitor,
|
waMonitor,
|
||||||
configService,
|
configService,
|
||||||
repository,
|
repository,
|
||||||
eventEmitter,
|
eventEmitter,
|
||||||
authService,
|
authService,
|
||||||
webhookService,
|
webhookService,
|
||||||
chatwootService,
|
chatwootService,
|
||||||
settingsService,
|
settingsService,
|
||||||
cache,
|
cache,
|
||||||
);
|
);
|
||||||
export const viewsController = new ViewsController(waMonitor, configService);
|
export const viewsController = new ViewsController(waMonitor, configService);
|
||||||
export const sendMessageController = new SendMessageController(waMonitor);
|
export const sendMessageController = new SendMessageController(waMonitor);
|
||||||
|
Loading…
Reference in New Issue
Block a user