mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 12:12:55 -06:00
Merge branch 'release/1.1.3'
This commit is contained in:
commit
97abb256d5
4
.gitignore
vendored
4
.gitignore
vendored
@ -11,7 +11,6 @@ yarn-debug.log*
|
|||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
lerna-debug.log*
|
lerna-debug.log*
|
||||||
|
|
||||||
/store/*
|
|
||||||
/docker-compose-data
|
/docker-compose-data
|
||||||
|
|
||||||
# Package
|
# Package
|
||||||
@ -33,3 +32,6 @@ lerna-debug.log*
|
|||||||
!/instances/.gitkeep
|
!/instances/.gitkeep
|
||||||
/test/
|
/test/
|
||||||
/src/env.yml
|
/src/env.yml
|
||||||
|
/store
|
||||||
|
|
||||||
|
/temp/*
|
||||||
|
22
CHANGELOG.md
22
CHANGELOG.md
@ -1,3 +1,25 @@
|
|||||||
|
# 1.1.3 (2023-07-06 11:43)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* Added configuration for Baileys log level in env
|
||||||
|
* Added audio to mp4 converter in optionally get Base64 From MediaMessage
|
||||||
|
* Added organization name in vcard
|
||||||
|
* Added email in vcard
|
||||||
|
* Added url in vcard
|
||||||
|
* Added verbose logs
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Added timestamp internally in urls to avoid caching
|
||||||
|
* Correction in decryption of poll votes
|
||||||
|
* Change in the way the api sent and saved the sent messages, now it goes in the messages.upsert event
|
||||||
|
* Fixed cash when sending stickers via url
|
||||||
|
* Improved how Redis works for instances
|
||||||
|
* Fixed problem when disconnecting the instance it removes the instance
|
||||||
|
* Fixed problem sending ack when preview is done by me
|
||||||
|
* Adjust in store files
|
||||||
|
|
||||||
# 1.1.2 (2023-06-28 13:43)
|
# 1.1.2 (2023-06-28 13:43)
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
FROM node:16.18-alpine
|
FROM node:16.18-alpine
|
||||||
|
|
||||||
|
LABEL version="1.1.3" description="Api to control whatsapp features through http requests."
|
||||||
|
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
|
||||||
|
LABEL contact="contato@agenciadgcode.com"
|
||||||
|
|
||||||
RUN apk update && apk upgrade && \
|
RUN apk update && apk upgrade && \
|
||||||
apk add --no-cache git
|
apk add --no-cache git
|
||||||
|
|
||||||
@ -16,7 +20,7 @@ ENV CORS_ORIGIN="*"
|
|||||||
ENV CORS_METHODS="POST,GET,PUT,DELETE"
|
ENV CORS_METHODS="POST,GET,PUT,DELETE"
|
||||||
ENV CORS_CREDENTIALS=true
|
ENV CORS_CREDENTIALS=true
|
||||||
|
|
||||||
ENV LOG_LEVEL="ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK"
|
ENV LOG_LEVEL=$LOG_LEVEL
|
||||||
ENV LOG_COLOR=true
|
ENV LOG_COLOR=true
|
||||||
|
|
||||||
ENV DEL_INSTANCE=$DEL_INSTANCE
|
ENV DEL_INSTANCE=$DEL_INSTANCE
|
||||||
@ -54,7 +58,6 @@ ENV WEBHOOK_EVENTS_QRCODE_UPDATED=$WEBHOOK_EVENTS_QRCODE_UPDATED
|
|||||||
ENV WEBHOOK_EVENTS_MESSAGES_SET=$WEBHOOK_EVENTS_MESSAGES_SET
|
ENV WEBHOOK_EVENTS_MESSAGES_SET=$WEBHOOK_EVENTS_MESSAGES_SET
|
||||||
ENV WEBHOOK_EVENTS_MESSAGES_UPDATE=$WEBHOOK_EVENTS_MESSAGES_UPDATE
|
ENV WEBHOOK_EVENTS_MESSAGES_UPDATE=$WEBHOOK_EVENTS_MESSAGES_UPDATE
|
||||||
ENV WEBHOOK_EVENTS_MESSAGES_UPSERT=$WEBHOOK_EVENTS_MESSAGES_UPSERT
|
ENV WEBHOOK_EVENTS_MESSAGES_UPSERT=$WEBHOOK_EVENTS_MESSAGES_UPSERT
|
||||||
ENV WEBHOOK_EVENTS_SEND_MESSAGE=$WEBHOOK_EVENTS_SEND_MESSAGE
|
|
||||||
ENV WEBHOOK_EVENTS_CONTACTS_SET=$WEBHOOK_EVENTS_CONTACTS_SET
|
ENV WEBHOOK_EVENTS_CONTACTS_SET=$WEBHOOK_EVENTS_CONTACTS_SET
|
||||||
ENV WEBHOOK_EVENTS_CONTACTS_UPSERT=$WEBHOOK_EVENTS_CONTACTS_UPSERT
|
ENV WEBHOOK_EVENTS_CONTACTS_UPSERT=$WEBHOOK_EVENTS_CONTACTS_UPSERT
|
||||||
ENV WEBHOOK_EVENTS_CONTACTS_UPDATE=$WEBHOOK_EVENTS_CONTACTS_UPDATE
|
ENV WEBHOOK_EVENTS_CONTACTS_UPDATE=$WEBHOOK_EVENTS_CONTACTS_UPDATE
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
<div align="center">
|
<div align="center">
|
||||||
|
|
||||||
[](https://doc.evolution-api.com)
|
[](https://evolution-api.com/whatsapp)
|
||||||
[](https://doc.evolution-api.com)
|
[](https://evolution-api.com/docs/evolution-documentation/getting-started/postman-collection/)
|
||||||
|
[](https://evolution-api.com)
|
||||||
[](./LICENSE)
|
[](./LICENSE)
|
||||||
[](https://app.picpay.com/user/davidsongomes1998)
|
[](https://app.picpay.com/user/davidsongomes1998)
|
||||||
|
|
||||||
|
@ -14,10 +14,12 @@ services:
|
|||||||
- evolution_instances:/evolution/instances
|
- evolution_instances:/evolution/instances
|
||||||
- evolution_store:/evolution/store
|
- evolution_store:/evolution/store
|
||||||
environment:
|
environment:
|
||||||
|
- LOG_LEVEL=ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK,WEBHOOKS
|
||||||
|
- LOG_BAILEYS=error
|
||||||
# Determine how long the instance should be deleted from memory in case of no connection.
|
# Determine how long the instance should be deleted from memory in case of no connection.
|
||||||
# Default time: 5 minutes
|
# Default time: 5 minutes
|
||||||
# If you don't even want an expiration, enter the value false
|
# If you don't even want an expiration, enter the value false
|
||||||
- DEL_INSTANCE=5 # or false
|
- DEL_INSTANCE=false # 5 or false
|
||||||
# Temporary data storage
|
# Temporary data storage
|
||||||
- STORE_MESSAGES=true
|
- STORE_MESSAGES=true
|
||||||
- STORE_MESSAGE_UP=true
|
- STORE_MESSAGE_UP=true
|
||||||
@ -39,7 +41,7 @@ services:
|
|||||||
- DATABASE_SAVE_MESSAGE_UPDATE=true
|
- DATABASE_SAVE_MESSAGE_UPDATE=true
|
||||||
- DATABASE_SAVE_DATA_CONTACTS=true
|
- DATABASE_SAVE_DATA_CONTACTS=true
|
||||||
- DATABASE_SAVE_DATA_CHATS=true
|
- DATABASE_SAVE_DATA_CHATS=true
|
||||||
- REDIS_ENABLED=false
|
- REDIS_ENABLED=true
|
||||||
- REDIS_URI=redis://redis:6379/1
|
- REDIS_URI=redis://redis:6379/1
|
||||||
- REDIS_PREFIX_KEY=evolution
|
- REDIS_PREFIX_KEY=evolution
|
||||||
# Webhook Settings
|
# Webhook Settings
|
||||||
@ -55,7 +57,6 @@ services:
|
|||||||
- WEBHOOK_EVENTS_MESSAGES_SET=true
|
- WEBHOOK_EVENTS_MESSAGES_SET=true
|
||||||
- WEBHOOK_EVENTS_MESSAGES_UPSERT=true
|
- WEBHOOK_EVENTS_MESSAGES_UPSERT=true
|
||||||
- WEBHOOK_EVENTS_MESSAGES_UPDATE=true
|
- WEBHOOK_EVENTS_MESSAGES_UPDATE=true
|
||||||
- WEBHOOK_EVENTS_SEND_MESSAGE=true
|
|
||||||
- WEBHOOK_EVENTS_CONTACTS_SET=true
|
- WEBHOOK_EVENTS_CONTACTS_SET=true
|
||||||
- WEBHOOK_EVENTS_CONTACTS_UPSERT=true
|
- WEBHOOK_EVENTS_CONTACTS_UPSERT=true
|
||||||
- WEBHOOK_EVENTS_CONTACTS_UPDATE=true
|
- WEBHOOK_EVENTS_CONTACTS_UPDATE=true
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "evolution-api",
|
"name": "evolution-api",
|
||||||
"version": "1.2.0",
|
"version": "1.1.3",
|
||||||
"description": "Rest api for communication with WhatsApp",
|
"description": "Rest api for communication with WhatsApp",
|
||||||
"main": "index.js",
|
"main": "./dist/src/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "ts-node --files --transpile-only ./src/main.ts",
|
"start": "ts-node --files --transpile-only ./src/main.ts",
|
||||||
@ -41,9 +41,9 @@
|
|||||||
"homepage": "https://github.com/DavidsonGomes/evolution-api#readme",
|
"homepage": "https://github.com/DavidsonGomes/evolution-api#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@adiwajshing/keyed-db": "^0.2.4",
|
"@adiwajshing/keyed-db": "^0.2.4",
|
||||||
"@whiskeysockets/baileys": "^6.3.0",
|
|
||||||
"@ffmpeg-installer/ffmpeg": "^1.1.0",
|
"@ffmpeg-installer/ffmpeg": "^1.1.0",
|
||||||
"@hapi/boom": "^10.0.1",
|
"@hapi/boom": "^10.0.1",
|
||||||
|
"@whiskeysockets/baileys": "^6.3.0",
|
||||||
"axios": "^1.3.5",
|
"axios": "^1.3.5",
|
||||||
"class-validator": "^0.13.2",
|
"class-validator": "^0.13.2",
|
||||||
"compression": "^1.7.4",
|
"compression": "^1.7.4",
|
||||||
|
2034
postman.json
2034
postman.json
File diff suppressed because one or more lines are too long
@ -13,10 +13,22 @@ export type Cors = {
|
|||||||
CREDENTIALS: boolean;
|
CREDENTIALS: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LogLevel = 'ERROR' | 'WARN' | 'DEBUG' | 'INFO' | 'LOG' | 'VERBOSE' | 'DARK';
|
export type LogBaileys = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
||||||
|
|
||||||
|
export type LogLevel =
|
||||||
|
| 'ERROR'
|
||||||
|
| 'WARN'
|
||||||
|
| 'DEBUG'
|
||||||
|
| 'INFO'
|
||||||
|
| 'LOG'
|
||||||
|
| 'VERBOSE'
|
||||||
|
| 'DARK'
|
||||||
|
| 'WEBHOOKS';
|
||||||
|
|
||||||
export type Log = {
|
export type Log = {
|
||||||
LEVEL: LogLevel[];
|
LEVEL: LogLevel[];
|
||||||
COLOR: boolean;
|
COLOR: boolean;
|
||||||
|
BAILEYS: LogBaileys;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SaveData = {
|
export type SaveData = {
|
||||||
@ -65,7 +77,6 @@ export type EventsWebhook = {
|
|||||||
MESSAGES_SET: boolean;
|
MESSAGES_SET: boolean;
|
||||||
MESSAGES_UPSERT: boolean;
|
MESSAGES_UPSERT: boolean;
|
||||||
MESSAGES_UPDATE: boolean;
|
MESSAGES_UPDATE: boolean;
|
||||||
SEND_MESSAGE: boolean;
|
|
||||||
CONTACTS_SET: boolean;
|
CONTACTS_SET: boolean;
|
||||||
CONTACTS_UPDATE: boolean;
|
CONTACTS_UPDATE: boolean;
|
||||||
CONTACTS_UPSERT: boolean;
|
CONTACTS_UPSERT: boolean;
|
||||||
@ -149,7 +160,9 @@ export class ConfigService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private envYaml(): Env {
|
private envYaml(): Env {
|
||||||
return load(readFileSync(join(SRC_DIR, 'env.yml'), { encoding: 'utf-8' })) as Env;
|
return load(
|
||||||
|
readFileSync(join(process.cwd(), 'src', 'env.yml'), { encoding: 'utf-8' }),
|
||||||
|
) as Env;
|
||||||
}
|
}
|
||||||
|
|
||||||
private envProcess(): Env {
|
private envProcess(): Env {
|
||||||
@ -205,6 +218,7 @@ export class ConfigService {
|
|||||||
LOG: {
|
LOG: {
|
||||||
LEVEL: process.env?.LOG_LEVEL.split(',') as LogLevel[],
|
LEVEL: process.env?.LOG_LEVEL.split(',') as LogLevel[],
|
||||||
COLOR: process.env?.LOG_COLOR === 'true',
|
COLOR: process.env?.LOG_COLOR === 'true',
|
||||||
|
BAILEYS: (process.env?.LOG_BAILEYS as LogBaileys) || 'error',
|
||||||
},
|
},
|
||||||
DEL_INSTANCE: isBooleanString(process.env?.DEL_INSTANCE)
|
DEL_INSTANCE: isBooleanString(process.env?.DEL_INSTANCE)
|
||||||
? process.env.DEL_INSTANCE === 'true'
|
? process.env.DEL_INSTANCE === 'true'
|
||||||
@ -221,7 +235,6 @@ export class ConfigService {
|
|||||||
MESSAGES_SET: process.env?.WEBHOOK_EVENTS_MESSAGES_SET === 'true',
|
MESSAGES_SET: process.env?.WEBHOOK_EVENTS_MESSAGES_SET === 'true',
|
||||||
MESSAGES_UPSERT: process.env?.WEBHOOK_EVENTS_MESSAGES_UPSERT === 'true',
|
MESSAGES_UPSERT: process.env?.WEBHOOK_EVENTS_MESSAGES_UPSERT === 'true',
|
||||||
MESSAGES_UPDATE: process.env?.WEBHOOK_EVENTS_MESSAGES_UPDATE === 'true',
|
MESSAGES_UPDATE: process.env?.WEBHOOK_EVENTS_MESSAGES_UPDATE === 'true',
|
||||||
SEND_MESSAGE: process.env?.WEBHOOK_EVENTS_SEND_MESSAGE === 'true',
|
|
||||||
CONTACTS_SET: process.env?.WEBHOOK_EVENTS_CONTACTS_SET === 'true',
|
CONTACTS_SET: process.env?.WEBHOOK_EVENTS_CONTACTS_SET === 'true',
|
||||||
CONTACTS_UPDATE: process.env?.WEBHOOK_EVENTS_CONTACTS_UPDATE === 'true',
|
CONTACTS_UPDATE: process.env?.WEBHOOK_EVENTS_CONTACTS_UPDATE === 'true',
|
||||||
CONTACTS_UPSERT: process.env?.WEBHOOK_EVENTS_CONTACTS_UPSERT === 'true',
|
CONTACTS_UPSERT: process.env?.WEBHOOK_EVENTS_CONTACTS_UPSERT === 'true',
|
||||||
|
@ -67,7 +67,6 @@ export class Logger {
|
|||||||
this.configService.get<Log>('LOG').LEVEL.forEach((level) => types.push(Type[level]));
|
this.configService.get<Log>('LOG').LEVEL.forEach((level) => types.push(Type[level]));
|
||||||
|
|
||||||
const typeValue = typeof value;
|
const typeValue = typeof value;
|
||||||
|
|
||||||
if (types.includes(type)) {
|
if (types.includes(type)) {
|
||||||
if (configService.get<Log>('LOG').COLOR) {
|
if (configService.get<Log>('LOG').COLOR) {
|
||||||
console.log(
|
console.log(
|
||||||
|
@ -2,13 +2,23 @@ import mongoose from 'mongoose';
|
|||||||
import { configService, Database } from '../config/env.config';
|
import { configService, Database } from '../config/env.config';
|
||||||
import { Logger } from '../config/logger.config';
|
import { Logger } from '../config/logger.config';
|
||||||
|
|
||||||
const logger = new Logger('Db Connection');
|
const logger = new Logger('MongoDB');
|
||||||
|
|
||||||
const db = configService.get<Database>('DATABASE');
|
const db = configService.get<Database>('DATABASE');
|
||||||
export const dbserver = db.ENABLED
|
export const dbserver = (() => {
|
||||||
? mongoose.createConnection(db.CONNECTION.URI, {
|
if (db.ENABLED) {
|
||||||
|
logger.verbose('connecting');
|
||||||
|
const dbs = mongoose.createConnection(db.CONNECTION.URI, {
|
||||||
dbName: db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api',
|
dbName: db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api',
|
||||||
})
|
});
|
||||||
: null;
|
logger.verbose('connected in ' + db.CONNECTION.URI);
|
||||||
|
logger.info('ON - dbName: ' + dbs['$dbName']);
|
||||||
|
|
||||||
db.ENABLED ? logger.info('ON - dbName: ' + dbserver['$dbName']) : null;
|
process.on('beforeExit', () => {
|
||||||
|
logger.verbose('instance destroyed');
|
||||||
|
dbserver.destroy(true, (error) => logger.error(error));
|
||||||
|
});
|
||||||
|
|
||||||
|
return dbs;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
@ -4,21 +4,41 @@ import { BufferJSON } from '@whiskeysockets/baileys';
|
|||||||
import { Redis } from '../config/env.config';
|
import { Redis } from '../config/env.config';
|
||||||
|
|
||||||
export class RedisCache {
|
export class RedisCache {
|
||||||
constructor(private readonly redisEnv: Partial<Redis>, private instanceName?: string) {
|
constructor() {
|
||||||
this.client = createClient({ url: this.redisEnv.URI });
|
this.logger.verbose('instance created');
|
||||||
|
process.on('beforeExit', async () => {
|
||||||
this.client.connect();
|
this.logger.verbose('instance destroyed');
|
||||||
|
if (this.statusConnection) {
|
||||||
|
this.logger.verbose('instance disconnect');
|
||||||
|
await this.client.disconnect();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private statusConnection = false;
|
||||||
|
private instanceName: string;
|
||||||
|
private redisEnv: Redis;
|
||||||
|
|
||||||
public set reference(reference: string) {
|
public set reference(reference: string) {
|
||||||
|
this.logger.verbose('set reference: ' + reference);
|
||||||
this.instanceName = reference;
|
this.instanceName = reference;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async connect(redisEnv: Redis) {
|
||||||
|
this.logger.verbose('connecting');
|
||||||
|
this.client = createClient({ url: redisEnv.URI });
|
||||||
|
this.logger.verbose('connected in ' + redisEnv.URI);
|
||||||
|
await this.client.connect();
|
||||||
|
this.statusConnection = true;
|
||||||
|
this.redisEnv = redisEnv;
|
||||||
|
}
|
||||||
|
|
||||||
private readonly logger = new Logger(RedisCache.name);
|
private readonly logger = new Logger(RedisCache.name);
|
||||||
private client: RedisClientType;
|
private client: RedisClientType;
|
||||||
|
|
||||||
public async instanceKeys(): Promise<string[]> {
|
public async instanceKeys(): Promise<string[]> {
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('instance keys: ' + this.redisEnv.PREFIX_KEY + ':*');
|
||||||
return await this.client.sendCommand(['keys', this.redisEnv.PREFIX_KEY + ':*']);
|
return await this.client.sendCommand(['keys', this.redisEnv.PREFIX_KEY + ':*']);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
@ -27,14 +47,18 @@ export class RedisCache {
|
|||||||
|
|
||||||
public async keyExists(key?: string) {
|
public async keyExists(key?: string) {
|
||||||
if (key) {
|
if (key) {
|
||||||
|
this.logger.verbose('keyExists: ' + key);
|
||||||
return !!(await this.instanceKeys()).find((i) => i === key);
|
return !!(await this.instanceKeys()).find((i) => i === key);
|
||||||
}
|
}
|
||||||
|
this.logger.verbose('keyExists: ' + this.instanceName);
|
||||||
return !!(await this.instanceKeys()).find((i) => i === this.instanceName);
|
return !!(await this.instanceKeys()).find((i) => i === this.instanceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async writeData(field: string, data: any) {
|
public async writeData(field: string, data: any) {
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('writeData: ' + field);
|
||||||
const json = JSON.stringify(data, BufferJSON.replacer);
|
const json = JSON.stringify(data, BufferJSON.replacer);
|
||||||
|
|
||||||
return await this.client.hSet(
|
return await this.client.hSet(
|
||||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||||
field,
|
field,
|
||||||
@ -47,13 +71,19 @@ export class RedisCache {
|
|||||||
|
|
||||||
public async readData(field: string) {
|
public async readData(field: string) {
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('readData: ' + field);
|
||||||
const data = await this.client.hGet(
|
const data = await this.client.hGet(
|
||||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||||
field,
|
field,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
|
this.logger.verbose('readData: ' + field + ' success');
|
||||||
return JSON.parse(data, BufferJSON.reviver);
|
return JSON.parse(data, BufferJSON.reviver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('readData: ' + field + ' not found');
|
||||||
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
}
|
}
|
||||||
@ -61,6 +91,7 @@ export class RedisCache {
|
|||||||
|
|
||||||
public async removeData(field: string) {
|
public async removeData(field: string) {
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('removeData: ' + field);
|
||||||
return await this.client.hDel(
|
return await this.client.hDel(
|
||||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||||
field,
|
field,
|
||||||
@ -72,6 +103,7 @@ export class RedisCache {
|
|||||||
|
|
||||||
public async delAll(hash?: string) {
|
public async delAll(hash?: string) {
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('instance delAll: ' + hash);
|
||||||
return await this.client.del(
|
return await this.client.del(
|
||||||
hash || this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
hash || this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||||
);
|
);
|
||||||
|
@ -36,12 +36,14 @@ LOG:
|
|||||||
- LOG
|
- LOG
|
||||||
- VERBOSE
|
- VERBOSE
|
||||||
- DARK
|
- DARK
|
||||||
|
- WEBHOOKS
|
||||||
COLOR: true
|
COLOR: true
|
||||||
|
BAILEYS: error # "fatal" | "error" | "warn" | "info" | "debug" | "trace"
|
||||||
|
|
||||||
# Determine how long the instance should be deleted from memory in case of no connection.
|
# Determine how long the instance should be deleted from memory in case of no connection.
|
||||||
# Default time: 5 minutes
|
# Default time: 5 minutes
|
||||||
# If you don't even want an expiration, enter the value false
|
# If you don't even want an expiration, enter the value false
|
||||||
DEL_INSTANCE: 5 # or false
|
DEL_INSTANCE: false # or false
|
||||||
|
|
||||||
# Temporary data storage
|
# Temporary data storage
|
||||||
STORE:
|
STORE:
|
||||||
@ -93,7 +95,6 @@ WEBHOOK:
|
|||||||
MESSAGES_SET: true
|
MESSAGES_SET: true
|
||||||
MESSAGES_UPSERT: true
|
MESSAGES_UPSERT: true
|
||||||
MESSAGES_UPDATE: true
|
MESSAGES_UPDATE: true
|
||||||
SEND_MESSAGE: true
|
|
||||||
CONTACTS_SET: true
|
CONTACTS_SET: true
|
||||||
CONTACTS_UPSERT: true
|
CONTACTS_UPSERT: true
|
||||||
CONTACTS_UPDATE: true
|
CONTACTS_UPDATE: true
|
||||||
|
@ -16,8 +16,6 @@ function initWA() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function bootstrap() {
|
function bootstrap() {
|
||||||
initWA();
|
|
||||||
|
|
||||||
const logger = new Logger('SERVER');
|
const logger = new Logger('SERVER');
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
@ -34,8 +32,8 @@ function bootstrap() {
|
|||||||
methods: [...configService.get<Cors>('CORS').METHODS],
|
methods: [...configService.get<Cors>('CORS').METHODS],
|
||||||
credentials: configService.get<Cors>('CORS').CREDENTIALS,
|
credentials: configService.get<Cors>('CORS').CREDENTIALS,
|
||||||
}),
|
}),
|
||||||
urlencoded({ extended: true, limit: '50mb' }),
|
urlencoded({ extended: true, limit: '136mb' }),
|
||||||
json({ limit: '50mb' }),
|
json({ limit: '136mb' }),
|
||||||
compression(),
|
compression(),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -73,6 +71,8 @@ function bootstrap() {
|
|||||||
logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT),
|
logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
initWA();
|
||||||
|
|
||||||
onUnexpectedError();
|
onUnexpectedError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,17 +9,12 @@ import { RedisCache } from '../db/redis.client';
|
|||||||
import { Logger } from '../config/logger.config';
|
import { Logger } from '../config/logger.config';
|
||||||
import { Redis } from '../config/env.config';
|
import { Redis } from '../config/env.config';
|
||||||
|
|
||||||
export async function useMultiFileAuthStateRedisDb(
|
export async function useMultiFileAuthStateRedisDb(cache: RedisCache): Promise<{
|
||||||
redisEnv: Partial<Redis>,
|
|
||||||
instanceName: string,
|
|
||||||
): Promise<{
|
|
||||||
state: AuthenticationState;
|
state: AuthenticationState;
|
||||||
saveCreds: () => Promise<void>;
|
saveCreds: () => Promise<void>;
|
||||||
}> {
|
}> {
|
||||||
const logger = new Logger(useMultiFileAuthStateRedisDb.name);
|
const logger = new Logger(useMultiFileAuthStateRedisDb.name);
|
||||||
|
|
||||||
const cache = new RedisCache(redisEnv, instanceName);
|
|
||||||
|
|
||||||
const writeData = async (data: any, key: string): Promise<any> => {
|
const writeData = async (data: any, key: string): Promise<any> => {
|
||||||
try {
|
try {
|
||||||
return await cache.writeData(key, data);
|
return await cache.writeData(key, data);
|
||||||
|
@ -39,7 +39,6 @@ export const instanceNameSchema: JSONSchema7 = {
|
|||||||
'MESSAGES_SET',
|
'MESSAGES_SET',
|
||||||
'MESSAGES_UPSERT',
|
'MESSAGES_UPSERT',
|
||||||
'MESSAGES_UPDATE',
|
'MESSAGES_UPDATE',
|
||||||
'SEND_MESSAGE',
|
|
||||||
'CONTACTS_SET',
|
'CONTACTS_SET',
|
||||||
'CONTACTS_UPSERT',
|
'CONTACTS_UPSERT',
|
||||||
'CONTACTS_UPDATE',
|
'CONTACTS_UPDATE',
|
||||||
@ -380,6 +379,9 @@ export const contactMessageSchema: JSONSchema7 = {
|
|||||||
description: '"wuid" must be a numeric string',
|
description: '"wuid" must be a numeric string',
|
||||||
},
|
},
|
||||||
phoneNumber: { type: 'string', minLength: 10 },
|
phoneNumber: { type: 'string', minLength: 10 },
|
||||||
|
organization: { type: 'string' },
|
||||||
|
email: { type: 'string' },
|
||||||
|
url: { type: 'string' },
|
||||||
},
|
},
|
||||||
required: ['fullName', 'wuid', 'phoneNumber'],
|
required: ['fullName', 'wuid', 'phoneNumber'],
|
||||||
...isNotEmpty('fullName'),
|
...isNotEmpty('fullName'),
|
||||||
@ -802,7 +804,6 @@ export const webhookSchema: JSONSchema7 = {
|
|||||||
'MESSAGES_SET',
|
'MESSAGES_SET',
|
||||||
'MESSAGES_UPSERT',
|
'MESSAGES_UPSERT',
|
||||||
'MESSAGES_UPDATE',
|
'MESSAGES_UPDATE',
|
||||||
'SEND_MESSAGE',
|
|
||||||
'CONTACTS_SET',
|
'CONTACTS_SET',
|
||||||
'CONTACTS_UPSERT',
|
'CONTACTS_UPSERT',
|
||||||
'CONTACTS_UPDATE',
|
'CONTACTS_UPDATE',
|
||||||
|
@ -9,6 +9,7 @@ import {
|
|||||||
ProfileStatusDto,
|
ProfileStatusDto,
|
||||||
ReadMessageDto,
|
ReadMessageDto,
|
||||||
WhatsAppNumberDto,
|
WhatsAppNumberDto,
|
||||||
|
getBase64FromMediaMessageDto,
|
||||||
} from '../dto/chat.dto';
|
} from '../dto/chat.dto';
|
||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { ContactQuery } from '../repository/contact.repository';
|
import { ContactQuery } from '../repository/contact.repository';
|
||||||
@ -45,11 +46,9 @@ export class ChatController {
|
|||||||
|
|
||||||
public async getBase64FromMediaMessage(
|
public async getBase64FromMediaMessage(
|
||||||
{ instanceName }: InstanceDto,
|
{ instanceName }: InstanceDto,
|
||||||
message: proto.IWebMessageInfo,
|
data: getBase64FromMediaMessageDto,
|
||||||
) {
|
) {
|
||||||
return await this.waMonitor.waInstances[instanceName].getBase64FromMediaMessage(
|
return await this.waMonitor.waInstances[instanceName].getBase64FromMediaMessage(data);
|
||||||
message,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchMessages({ instanceName }: InstanceDto, query: MessageQuery) {
|
public async fetchMessages({ instanceName }: InstanceDto, query: MessageQuery) {
|
||||||
|
@ -10,6 +10,7 @@ import { WAStartupService } from '../services/whatsapp.service';
|
|||||||
import { WebhookService } from '../services/webhook.service';
|
import { WebhookService } from '../services/webhook.service';
|
||||||
import { Logger } from '../../config/logger.config';
|
import { Logger } from '../../config/logger.config';
|
||||||
import { wa } from '../types/wa.types';
|
import { wa } from '../types/wa.types';
|
||||||
|
import { RedisCache } from '../../db/redis.client';
|
||||||
|
|
||||||
export class InstanceController {
|
export class InstanceController {
|
||||||
constructor(
|
constructor(
|
||||||
@ -19,6 +20,7 @@ export class InstanceController {
|
|||||||
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 cache: RedisCache,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
private readonly logger = new Logger(InstanceController.name);
|
private readonly logger = new Logger(InstanceController.name);
|
||||||
@ -47,6 +49,7 @@ export class InstanceController {
|
|||||||
this.configService,
|
this.configService,
|
||||||
this.eventEmitter,
|
this.eventEmitter,
|
||||||
this.repository,
|
this.repository,
|
||||||
|
this.cache,
|
||||||
);
|
);
|
||||||
instance.instanceName = instanceName;
|
instance.instanceName = instanceName;
|
||||||
this.waMonitor.waInstances[instance.instanceName] = instance;
|
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||||
@ -92,6 +95,7 @@ export class InstanceController {
|
|||||||
this.configService,
|
this.configService,
|
||||||
this.eventEmitter,
|
this.eventEmitter,
|
||||||
this.repository,
|
this.repository,
|
||||||
|
this.cache,
|
||||||
);
|
);
|
||||||
instance.instanceName = instanceName;
|
instance.instanceName = instanceName;
|
||||||
this.waMonitor.waInstances[instance.instanceName] = instance;
|
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||||
@ -171,6 +175,7 @@ export class InstanceController {
|
|||||||
this.configService,
|
this.configService,
|
||||||
this.eventEmitter,
|
this.eventEmitter,
|
||||||
this.repository,
|
this.repository,
|
||||||
|
this.cache,
|
||||||
);
|
);
|
||||||
|
|
||||||
instance.instanceName = instanceName;
|
instance.instanceName = instanceName;
|
||||||
@ -202,7 +207,6 @@ export class InstanceController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
||||||
this.waMonitor.waInstances[instanceName]?.client?.end(undefined);
|
|
||||||
|
|
||||||
return { error: false, message: 'Instance logged out' };
|
return { error: false, message: 'Instance logged out' };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -2,6 +2,7 @@ import {
|
|||||||
WAPrivacyOnlineValue,
|
WAPrivacyOnlineValue,
|
||||||
WAPrivacyValue,
|
WAPrivacyValue,
|
||||||
WAReadReceiptsValue,
|
WAReadReceiptsValue,
|
||||||
|
proto,
|
||||||
} from '@whiskeysockets/baileys';
|
} from '@whiskeysockets/baileys';
|
||||||
|
|
||||||
export class OnWhatsAppDto {
|
export class OnWhatsAppDto {
|
||||||
@ -12,6 +13,11 @@ export class OnWhatsAppDto {
|
|||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class getBase64FromMediaMessageDto {
|
||||||
|
message: proto.WebMessageInfo;
|
||||||
|
convertToMp4?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export class WhatsAppNumberDto {
|
export class WhatsAppNumberDto {
|
||||||
numbers: string[];
|
numbers: string[];
|
||||||
}
|
}
|
||||||
|
@ -125,6 +125,9 @@ export class ContactMessage {
|
|||||||
fullName: string;
|
fullName: string;
|
||||||
wuid: string;
|
wuid: string;
|
||||||
phoneNumber: string;
|
phoneNumber: string;
|
||||||
|
organization?: string;
|
||||||
|
email?: string;
|
||||||
|
url?: string;
|
||||||
}
|
}
|
||||||
export class SendContactDto extends Metadata {
|
export class SendContactDto extends Metadata {
|
||||||
contactMessage: ContactMessage[];
|
contactMessage: ContactMessage[];
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '../../exceptions';
|
} from '../../exceptions';
|
||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { waMonitor } from '../whatsapp.module';
|
import { cache, waMonitor } from '../whatsapp.module';
|
||||||
import { Database, Redis, configService } from '../../config/env.config';
|
import { Database, Redis, configService } from '../../config/env.config';
|
||||||
import { RedisCache } from '../../db/redis.client';
|
import { RedisCache } from '../../db/redis.client';
|
||||||
|
|
||||||
@ -20,7 +20,6 @@ async function getInstance(instanceName: string) {
|
|||||||
const exists = !!waMonitor.waInstances[instanceName];
|
const exists = !!waMonitor.waInstances[instanceName];
|
||||||
|
|
||||||
if (redisConf.ENABLED) {
|
if (redisConf.ENABLED) {
|
||||||
const cache = new RedisCache(redisConf, instanceName);
|
|
||||||
const keyExists = await cache.keyExists();
|
const keyExists = await cache.keyExists();
|
||||||
return exists || keyExists;
|
return exists || keyExists;
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,7 @@ export class MessageUpdateRaw {
|
|||||||
datetime?: number;
|
datetime?: number;
|
||||||
status?: wa.StatusMessage;
|
status?: wa.StatusMessage;
|
||||||
owner: string;
|
owner: string;
|
||||||
|
pollUpdates?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
const messageUpdateSchema = new Schema<MessageUpdateRaw>({
|
const messageUpdateSchema = new Schema<MessageUpdateRaw>({
|
||||||
|
@ -5,6 +5,9 @@ import { MessageUpRepository } from './messageUp.repository';
|
|||||||
import { MongoClient } from 'mongodb';
|
import { MongoClient } from 'mongodb';
|
||||||
import { WebhookRepository } from './webhook.repository';
|
import { WebhookRepository } from './webhook.repository';
|
||||||
import { AuthRepository } from './auth.repository';
|
import { AuthRepository } from './auth.repository';
|
||||||
|
import { Auth, ConfigService, Database } from '../../config/env.config';
|
||||||
|
import { execSync } from 'child_process';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
export class RepositoryBroker {
|
export class RepositoryBroker {
|
||||||
constructor(
|
constructor(
|
||||||
@ -14,9 +17,11 @@ export class RepositoryBroker {
|
|||||||
public readonly messageUpdate: MessageUpRepository,
|
public readonly messageUpdate: MessageUpRepository,
|
||||||
public readonly webhook: WebhookRepository,
|
public readonly webhook: WebhookRepository,
|
||||||
public readonly auth: AuthRepository,
|
public readonly auth: AuthRepository,
|
||||||
|
private configService: ConfigService,
|
||||||
dbServer?: MongoClient,
|
dbServer?: MongoClient,
|
||||||
) {
|
) {
|
||||||
this.dbClient = dbServer;
|
this.dbClient = dbServer;
|
||||||
|
this.__init_repo_without_db__();
|
||||||
}
|
}
|
||||||
|
|
||||||
private dbClient?: MongoClient;
|
private dbClient?: MongoClient;
|
||||||
@ -24,4 +29,22 @@ export class RepositoryBroker {
|
|||||||
public get dbServer() {
|
public get dbServer() {
|
||||||
return this.dbClient;
|
return this.dbClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private __init_repo_without_db__() {
|
||||||
|
if (!this.configService.get<Database>('DATABASE').ENABLED) {
|
||||||
|
const storePath = join(process.cwd(), 'store');
|
||||||
|
execSync(
|
||||||
|
`mkdir -p ${join(
|
||||||
|
storePath,
|
||||||
|
'auth',
|
||||||
|
this.configService.get<Auth>('AUTHENTICATION').TYPE,
|
||||||
|
)}`,
|
||||||
|
);
|
||||||
|
execSync(`mkdir -p ${join(storePath, 'chats')}`);
|
||||||
|
execSync(`mkdir -p ${join(storePath, 'contacts')}`);
|
||||||
|
execSync(`mkdir -p ${join(storePath, 'messages')}`);
|
||||||
|
execSync(`mkdir -p ${join(storePath, 'message-up')}`);
|
||||||
|
execSync(`mkdir -p ${join(storePath, 'webhook')}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import {
|
|||||||
ProfileStatusDto,
|
ProfileStatusDto,
|
||||||
ReadMessageDto,
|
ReadMessageDto,
|
||||||
WhatsAppNumberDto,
|
WhatsAppNumberDto,
|
||||||
|
getBase64FromMediaMessageDto,
|
||||||
} from '../dto/chat.dto';
|
} from '../dto/chat.dto';
|
||||||
import { ContactQuery } from '../repository/contact.repository';
|
import { ContactQuery } from '../repository/contact.repository';
|
||||||
import { MessageQuery } from '../repository/message.repository';
|
import { MessageQuery } from '../repository/message.repository';
|
||||||
@ -101,10 +102,10 @@ export class ChatRouter extends RouterBroker {
|
|||||||
return res.status(HttpStatus.OK).json(response);
|
return res.status(HttpStatus.OK).json(response);
|
||||||
})
|
})
|
||||||
.post(this.routerPath('getBase64FromMediaMessage'), ...guards, async (req, res) => {
|
.post(this.routerPath('getBase64FromMediaMessage'), ...guards, async (req, res) => {
|
||||||
const response = await this.dataValidate<proto.IWebMessageInfo>({
|
const response = await this.dataValidate<getBase64FromMediaMessageDto>({
|
||||||
request: req,
|
request: req,
|
||||||
schema: null,
|
schema: null,
|
||||||
ClassRef: Object,
|
ClassRef: getBase64FromMediaMessageDto,
|
||||||
execute: (instance, data) =>
|
execute: (instance, data) =>
|
||||||
chatController.getBase64FromMediaMessage(instance, data),
|
chatController.getBase64FromMediaMessage(instance, data),
|
||||||
});
|
});
|
||||||
|
@ -43,8 +43,12 @@ export class AuthService {
|
|||||||
{ expiresIn: jwtOpts.EXPIRIN_IN, encoding: 'utf8', subject: 'g-t' },
|
{ expiresIn: jwtOpts.EXPIRIN_IN, encoding: 'utf8', subject: 'g-t' },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.logger.verbose('JWT token created: ' + token);
|
||||||
|
|
||||||
const auth = await this.repository.auth.create({ jwt: token }, instance.instanceName);
|
const auth = await this.repository.auth.create({ jwt: token }, instance.instanceName);
|
||||||
|
|
||||||
|
this.logger.verbose('JWT token saved in database');
|
||||||
|
|
||||||
if (auth['error']) {
|
if (auth['error']) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
localError: AuthService.name + '.jwt',
|
localError: AuthService.name + '.jwt',
|
||||||
@ -59,8 +63,14 @@ export class AuthService {
|
|||||||
private async apikey(instance: InstanceDto, token?: string) {
|
private async apikey(instance: InstanceDto, token?: string) {
|
||||||
const apikey = token ? token : v4().toUpperCase();
|
const apikey = token ? token : v4().toUpperCase();
|
||||||
|
|
||||||
|
this.logger.verbose(
|
||||||
|
token ? 'APIKEY defined: ' + apikey : 'APIKEY created: ' + apikey,
|
||||||
|
);
|
||||||
|
|
||||||
const auth = await this.repository.auth.create({ apikey }, instance.instanceName);
|
const auth = await this.repository.auth.create({ apikey }, instance.instanceName);
|
||||||
|
|
||||||
|
this.logger.verbose('APIKEY saved in database');
|
||||||
|
|
||||||
if (auth['error']) {
|
if (auth['error']) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
localError: AuthService.name + '.apikey',
|
localError: AuthService.name + '.apikey',
|
||||||
@ -75,54 +85,77 @@ export class AuthService {
|
|||||||
public async checkDuplicateToken(token: string) {
|
public async checkDuplicateToken(token: string) {
|
||||||
const instances = await this.waMonitor.instanceInfo();
|
const instances = await this.waMonitor.instanceInfo();
|
||||||
|
|
||||||
|
this.logger.verbose('checking duplicate token');
|
||||||
|
|
||||||
const instance = instances.find((instance) => instance.instance.apikey === token);
|
const instance = instances.find((instance) => instance.instance.apikey === token);
|
||||||
|
|
||||||
if (instance) {
|
if (instance) {
|
||||||
throw new BadRequestException('Token already exists');
|
throw new BadRequestException('Token already exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('available token');
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async generateHash(instance: InstanceDto, token?: string) {
|
public async generateHash(instance: InstanceDto, token?: string) {
|
||||||
const options = this.configService.get<Auth>('AUTHENTICATION');
|
const options = this.configService.get<Auth>('AUTHENTICATION');
|
||||||
|
|
||||||
|
this.logger.verbose(
|
||||||
|
'generating hash ' + options.TYPE + ' to instance: ' + instance.instanceName,
|
||||||
|
);
|
||||||
|
|
||||||
return (await this[options.TYPE](instance, token)) as
|
return (await this[options.TYPE](instance, token)) as
|
||||||
| { jwt: string }
|
| { jwt: string }
|
||||||
| { apikey: string };
|
| { apikey: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
public async refreshToken({ oldToken }: OldToken) {
|
public async refreshToken({ oldToken }: OldToken) {
|
||||||
|
this.logger.verbose('refreshing token');
|
||||||
|
|
||||||
if (!isJWT(oldToken)) {
|
if (!isJWT(oldToken)) {
|
||||||
throw new BadRequestException('Invalid "oldToken"');
|
throw new BadRequestException('Invalid "oldToken"');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const jwtOpts = this.configService.get<Auth>('AUTHENTICATION').JWT;
|
const jwtOpts = this.configService.get<Auth>('AUTHENTICATION').JWT;
|
||||||
|
|
||||||
|
this.logger.verbose('checking oldToken');
|
||||||
|
|
||||||
const decode = verify(oldToken, jwtOpts.SECRET, {
|
const decode = verify(oldToken, jwtOpts.SECRET, {
|
||||||
ignoreExpiration: true,
|
ignoreExpiration: true,
|
||||||
}) as Pick<JwtPayload, 'apiName' | 'instanceName' | 'tokenId'>;
|
}) as Pick<JwtPayload, 'apiName' | 'instanceName' | 'tokenId'>;
|
||||||
|
|
||||||
|
this.logger.verbose('checking token in database');
|
||||||
|
|
||||||
const tokenStore = await this.repository.auth.find(decode.instanceName);
|
const tokenStore = await this.repository.auth.find(decode.instanceName);
|
||||||
|
|
||||||
const decodeTokenStore = verify(tokenStore.jwt, jwtOpts.SECRET, {
|
const decodeTokenStore = verify(tokenStore.jwt, jwtOpts.SECRET, {
|
||||||
ignoreExpiration: true,
|
ignoreExpiration: true,
|
||||||
}) as Pick<JwtPayload, 'apiName' | 'instanceName' | 'tokenId'>;
|
}) as Pick<JwtPayload, 'apiName' | 'instanceName' | 'tokenId'>;
|
||||||
|
|
||||||
|
this.logger.verbose('checking tokenId');
|
||||||
|
|
||||||
if (decode.tokenId !== decodeTokenStore.tokenId) {
|
if (decode.tokenId !== decodeTokenStore.tokenId) {
|
||||||
throw new BadRequestException('Invalid "oldToken"');
|
throw new BadRequestException('Invalid "oldToken"');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('generating new token');
|
||||||
|
|
||||||
const token = {
|
const token = {
|
||||||
jwt: (await this.jwt({ instanceName: decode.instanceName })).jwt,
|
jwt: (await this.jwt({ instanceName: decode.instanceName })).jwt,
|
||||||
instanceName: decode.instanceName,
|
instanceName: decode.instanceName,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('checking webhook');
|
||||||
const webhook = await this.repository.webhook.find(decode.instanceName);
|
const webhook = await this.repository.webhook.find(decode.instanceName);
|
||||||
if (
|
if (
|
||||||
webhook?.enabled &&
|
webhook?.enabled &&
|
||||||
this.configService.get<Webhook>('WEBHOOK').EVENTS.NEW_JWT_TOKEN
|
this.configService.get<Webhook>('WEBHOOK').EVENTS.NEW_JWT_TOKEN
|
||||||
) {
|
) {
|
||||||
|
this.logger.verbose('sending webhook');
|
||||||
|
|
||||||
const httpService = axios.create({ baseURL: webhook.url });
|
const httpService = axios.create({ baseURL: webhook.url });
|
||||||
await httpService.post(
|
await httpService.post(
|
||||||
'',
|
'',
|
||||||
@ -138,6 +171,8 @@ export class AuthService {
|
|||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('token refreshed');
|
||||||
|
|
||||||
return token;
|
return token;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
|
@ -14,15 +14,18 @@ import {
|
|||||||
import { RepositoryBroker } from '../repository/repository.manager';
|
import { RepositoryBroker } from '../repository/repository.manager';
|
||||||
import { NotFoundException } from '../../exceptions';
|
import { NotFoundException } from '../../exceptions';
|
||||||
import { Db } from 'mongodb';
|
import { Db } from 'mongodb';
|
||||||
import { RedisCache } from '../../db/redis.client';
|
|
||||||
import { initInstance } from '../whatsapp.module';
|
import { initInstance } from '../whatsapp.module';
|
||||||
|
import { RedisCache } from '../../db/redis.client';
|
||||||
|
|
||||||
export class WAMonitoringService {
|
export class WAMonitoringService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly eventEmitter: EventEmitter2,
|
private readonly eventEmitter: EventEmitter2,
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly repository: RepositoryBroker,
|
private readonly repository: RepositoryBroker,
|
||||||
|
private readonly cache: RedisCache,
|
||||||
) {
|
) {
|
||||||
|
this.logger.verbose('instance created');
|
||||||
|
|
||||||
this.removeInstance();
|
this.removeInstance();
|
||||||
this.noConnection();
|
this.noConnection();
|
||||||
this.delInstanceFiles();
|
this.delInstanceFiles();
|
||||||
@ -33,15 +36,12 @@ export class WAMonitoringService {
|
|||||||
this.dbInstance = this.db.ENABLED
|
this.dbInstance = this.db.ENABLED
|
||||||
? this.repository.dbServer?.db(this.db.CONNECTION.DB_PREFIX_NAME + '-instances')
|
? this.repository.dbServer?.db(this.db.CONNECTION.DB_PREFIX_NAME + '-instances')
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
this.redisCache = this.redis.ENABLED ? new RedisCache(this.redis) : undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly db: Partial<Database> = {};
|
private readonly db: Partial<Database> = {};
|
||||||
private readonly redis: Partial<Redis> = {};
|
private readonly redis: Partial<Redis> = {};
|
||||||
|
|
||||||
private dbInstance: Db;
|
private dbInstance: Db;
|
||||||
private redisCache: RedisCache;
|
|
||||||
|
|
||||||
private readonly logger = new Logger(WAMonitoringService.name);
|
private readonly logger = new Logger(WAMonitoringService.name);
|
||||||
public readonly waInstances: Record<string, WAStartupService> = {};
|
public readonly waInstances: Record<string, WAStartupService> = {};
|
||||||
@ -49,6 +49,10 @@ export class WAMonitoringService {
|
|||||||
public delInstanceTime(instance: string) {
|
public delInstanceTime(instance: string) {
|
||||||
const time = this.configService.get<DelInstance>('DEL_INSTANCE');
|
const time = this.configService.get<DelInstance>('DEL_INSTANCE');
|
||||||
if (typeof time === 'number' && time > 0) {
|
if (typeof time === 'number' && time > 0) {
|
||||||
|
this.logger.verbose(
|
||||||
|
`Instance "${instance}" don't have connection, will be removed in ${time} minutes`,
|
||||||
|
);
|
||||||
|
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
if (this.waInstances[instance]?.connectionStatus?.state !== 'open') {
|
if (this.waInstances[instance]?.connectionStatus?.state !== 'open') {
|
||||||
if (this.waInstances[instance]?.connectionStatus?.state === 'connecting') {
|
if (this.waInstances[instance]?.connectionStatus?.state === 'connecting') {
|
||||||
@ -68,6 +72,7 @@ export class WAMonitoringService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async instanceInfo(instanceName?: string) {
|
public async instanceInfo(instanceName?: string) {
|
||||||
|
this.logger.verbose('get instance info');
|
||||||
if (instanceName && !this.waInstances[instanceName]) {
|
if (instanceName && !this.waInstances[instanceName]) {
|
||||||
throw new NotFoundException(`Instance "${instanceName}" not found`);
|
throw new NotFoundException(`Instance "${instanceName}" not found`);
|
||||||
}
|
}
|
||||||
@ -76,9 +81,14 @@ export class WAMonitoringService {
|
|||||||
|
|
||||||
for await (const [key, value] of Object.entries(this.waInstances)) {
|
for await (const [key, value] of Object.entries(this.waInstances)) {
|
||||||
if (value) {
|
if (value) {
|
||||||
|
this.logger.verbose('get instance info: ' + key);
|
||||||
if (value.connectionStatus.state === 'open') {
|
if (value.connectionStatus.state === 'open') {
|
||||||
|
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
|
||||||
let apikey: string;
|
let apikey: string;
|
||||||
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
||||||
|
this.logger.verbose(
|
||||||
|
'instance: ' + key + ' - hash exposed in fetch instances',
|
||||||
|
);
|
||||||
const tokenStore = await this.repository.auth.find(key);
|
const tokenStore = await this.repository.auth.find(key);
|
||||||
apikey = tokenStore.apikey || 'Apikey not found';
|
apikey = tokenStore.apikey || 'Apikey not found';
|
||||||
|
|
||||||
@ -93,6 +103,9 @@ export class WAMonitoringService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
this.logger.verbose(
|
||||||
|
'instance: ' + key + ' - hash not exposed in fetch instances',
|
||||||
|
);
|
||||||
instances.push({
|
instances.push({
|
||||||
instance: {
|
instance: {
|
||||||
instanceName: key,
|
instanceName: key,
|
||||||
@ -104,8 +117,14 @@ export class WAMonitoringService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
this.logger.verbose(
|
||||||
|
'instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state,
|
||||||
|
);
|
||||||
let apikey: string;
|
let apikey: string;
|
||||||
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
||||||
|
this.logger.verbose(
|
||||||
|
'instance: ' + key + ' - hash exposed in fetch instances',
|
||||||
|
);
|
||||||
const tokenStore = await this.repository.auth.find(key);
|
const tokenStore = await this.repository.auth.find(key);
|
||||||
apikey = tokenStore.apikey || 'Apikey not found';
|
apikey = tokenStore.apikey || 'Apikey not found';
|
||||||
|
|
||||||
@ -117,6 +136,9 @@ export class WAMonitoringService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
this.logger.verbose(
|
||||||
|
'instance: ' + key + ' - hash not exposed in fetch instances',
|
||||||
|
);
|
||||||
instances.push({
|
instances.push({
|
||||||
instance: {
|
instance: {
|
||||||
instanceName: key,
|
instanceName: key,
|
||||||
@ -128,10 +150,13 @@ export class WAMonitoringService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('return instance info: ' + instances.length);
|
||||||
|
|
||||||
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
|
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
|
||||||
}
|
}
|
||||||
|
|
||||||
private delInstanceFiles() {
|
private delInstanceFiles() {
|
||||||
|
this.logger.verbose('cron to delete instance files started');
|
||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
||||||
const collections = await this.dbInstance.collections();
|
const collections = await this.dbInstance.collections();
|
||||||
@ -143,7 +168,9 @@ export class WAMonitoringService {
|
|||||||
{ _id: { $regex: /^session-.*/ } },
|
{ _id: { $regex: /^session-.*/ } },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
this.logger.verbose('instance files deleted: ' + name);
|
||||||
});
|
});
|
||||||
|
} else if (this.redis.ENABLED) {
|
||||||
} else {
|
} else {
|
||||||
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
||||||
for await (const dirent of dir) {
|
for await (const dirent of dir) {
|
||||||
@ -159,6 +186,7 @@ export class WAMonitoringService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.logger.verbose('instance files deleted: ' + dirent.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -166,7 +194,9 @@ export class WAMonitoringService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async cleaningUp(instanceName: string) {
|
public async cleaningUp(instanceName: string) {
|
||||||
|
this.logger.verbose('cleaning up instance: ' + instanceName);
|
||||||
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
||||||
|
this.logger.verbose('cleaning up instance in database: ' + instanceName);
|
||||||
await this.repository.dbServer.connect();
|
await this.repository.dbServer.connect();
|
||||||
const collections: any[] = await this.dbInstance.collections();
|
const collections: any[] = await this.dbInstance.collections();
|
||||||
if (collections.length > 0) {
|
if (collections.length > 0) {
|
||||||
@ -176,52 +206,70 @@ export class WAMonitoringService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.redis.ENABLED) {
|
if (this.redis.ENABLED) {
|
||||||
this.redisCache.reference = instanceName;
|
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
|
||||||
await this.redisCache.delAll();
|
this.cache.reference = instanceName;
|
||||||
|
await this.cache.delAll();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('cleaning up instance in files: ' + instanceName);
|
||||||
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
|
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
public async loadInstance() {
|
public async loadInstance() {
|
||||||
|
this.logger.verbose('load instances');
|
||||||
const set = async (name: string) => {
|
const set = async (name: string) => {
|
||||||
const instance = new WAStartupService(
|
const instance = new WAStartupService(
|
||||||
this.configService,
|
this.configService,
|
||||||
this.eventEmitter,
|
this.eventEmitter,
|
||||||
this.repository,
|
this.repository,
|
||||||
|
this.cache,
|
||||||
);
|
);
|
||||||
instance.instanceName = name;
|
instance.instanceName = name;
|
||||||
|
this.logger.verbose('instance loaded: ' + name);
|
||||||
|
|
||||||
await instance.connectToWhatsapp();
|
await instance.connectToWhatsapp();
|
||||||
|
this.logger.verbose('connectToWhatsapp: ' + name);
|
||||||
|
|
||||||
this.waInstances[name] = instance;
|
this.waInstances[name] = instance;
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (this.redis.ENABLED) {
|
if (this.redis.ENABLED) {
|
||||||
const keys = await this.redisCache.instanceKeys();
|
this.logger.verbose('redis enabled');
|
||||||
|
await this.cache.connect(this.redis as Redis);
|
||||||
|
const keys = await this.cache.instanceKeys();
|
||||||
if (keys?.length > 0) {
|
if (keys?.length > 0) {
|
||||||
|
this.logger.verbose('reading instance keys and setting instances');
|
||||||
keys.forEach(async (k) => await set(k.split(':')[1]));
|
keys.forEach(async (k) => await set(k.split(':')[1]));
|
||||||
} else {
|
} else {
|
||||||
|
this.logger.verbose('no instance keys found');
|
||||||
initInstance();
|
initInstance();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
||||||
|
this.logger.verbose('database enabled');
|
||||||
await this.repository.dbServer.connect();
|
await this.repository.dbServer.connect();
|
||||||
const collections: any[] = await this.dbInstance.collections();
|
const collections: any[] = await this.dbInstance.collections();
|
||||||
if (collections.length > 0) {
|
if (collections.length > 0) {
|
||||||
|
this.logger.verbose('reading collections and setting instances');
|
||||||
collections.forEach(
|
collections.forEach(
|
||||||
async (coll) => await set(coll.namespace.replace(/^[\w-]+\./, '')),
|
async (coll) => await set(coll.namespace.replace(/^[\w-]+\./, '')),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
this.logger.verbose('no collections found');
|
||||||
initInstance();
|
initInstance();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('store in files enabled');
|
||||||
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
||||||
for await (const dirent of dir) {
|
for await (const dirent of dir) {
|
||||||
if (dirent.isDirectory()) {
|
if (dirent.isDirectory()) {
|
||||||
|
this.logger.verbose('reading instance files and setting instances');
|
||||||
const files = readdirSync(join(INSTANCE_DIR, dirent.name), {
|
const files = readdirSync(join(INSTANCE_DIR, dirent.name), {
|
||||||
encoding: 'utf-8',
|
encoding: 'utf-8',
|
||||||
});
|
});
|
||||||
@ -232,6 +280,7 @@ export class WAMonitoringService {
|
|||||||
|
|
||||||
await set(dirent.name);
|
await set(dirent.name);
|
||||||
} else {
|
} else {
|
||||||
|
this.logger.verbose('no instance files found');
|
||||||
initInstance();
|
initInstance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -242,22 +291,38 @@ export class WAMonitoringService {
|
|||||||
|
|
||||||
private removeInstance() {
|
private removeInstance() {
|
||||||
this.eventEmitter.on('remove.instance', async (instanceName: string) => {
|
this.eventEmitter.on('remove.instance', async (instanceName: string) => {
|
||||||
|
this.logger.verbose('remove instance: ' + instanceName);
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('instance: ' + instanceName + ' - removing from memory');
|
||||||
this.waInstances[instanceName] = undefined;
|
this.waInstances[instanceName] = undefined;
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('request cleaning up instance: ' + instanceName);
|
||||||
this.cleaningUp(instanceName);
|
this.cleaningUp(instanceName);
|
||||||
} finally {
|
} finally {
|
||||||
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
|
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.eventEmitter.on('logout.instance', async (instanceName: string) => {
|
||||||
|
this.logger.verbose('logout instance: ' + instanceName);
|
||||||
|
try {
|
||||||
|
this.logger.verbose('request cleaning up instance: ' + instanceName);
|
||||||
|
this.cleaningUp(instanceName);
|
||||||
|
} finally {
|
||||||
|
this.logger.warn(`Instance "${instanceName}" - LOGOUT`);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private noConnection() {
|
private noConnection() {
|
||||||
|
this.logger.verbose('checking instances without connection');
|
||||||
this.eventEmitter.on('no.connection', async (instanceName) => {
|
this.eventEmitter.on('no.connection', async (instanceName) => {
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('instance: ' + instanceName + ' - removing from memory');
|
||||||
this.waInstances[instanceName] = undefined;
|
this.waInstances[instanceName] = undefined;
|
||||||
|
|
||||||
|
this.logger.verbose('request cleaning up instance: ' + instanceName);
|
||||||
this.cleaningUp(instanceName);
|
this.cleaningUp(instanceName);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { WebhookDto } from '../dto/webhook.dto';
|
import { WebhookDto } from '../dto/webhook.dto';
|
||||||
import { WAMonitoringService } from './monitor.service';
|
import { WAMonitoringService } from './monitor.service';
|
||||||
|
import { Logger } from '../../config/logger.config';
|
||||||
|
|
||||||
export class WebhookService {
|
export class WebhookService {
|
||||||
constructor(private readonly waMonitor: WAMonitoringService) {}
|
constructor(private readonly waMonitor: WAMonitoringService) {}
|
||||||
|
|
||||||
|
private readonly logger = new Logger(WebhookService.name);
|
||||||
|
|
||||||
public create(instance: InstanceDto, data: WebhookDto) {
|
public create(instance: InstanceDto, data: WebhookDto) {
|
||||||
|
this.logger.verbose('create webhook: ' + instance.instanceName);
|
||||||
this.waMonitor.waInstances[instance.instanceName].setWebhook(data);
|
this.waMonitor.waInstances[instance.instanceName].setWebhook(data);
|
||||||
|
|
||||||
return { webhook: { ...instance, webhook: data } };
|
return { webhook: { ...instance, webhook: data } };
|
||||||
@ -13,6 +17,7 @@ export class WebhookService {
|
|||||||
|
|
||||||
public async find(instance: InstanceDto): Promise<WebhookDto> {
|
public async find(instance: InstanceDto): Promise<WebhookDto> {
|
||||||
try {
|
try {
|
||||||
|
this.logger.verbose('find webhook: ' + instance.instanceName);
|
||||||
return await this.waMonitor.waInstances[instance.instanceName].findWebhook();
|
return await this.waMonitor.waInstances[instance.instanceName].findWebhook();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return { enabled: null, url: '' };
|
return { enabled: null, url: '' };
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -9,7 +9,6 @@ export enum Events {
|
|||||||
MESSAGES_SET = 'messages.set',
|
MESSAGES_SET = 'messages.set',
|
||||||
MESSAGES_UPSERT = 'messages.upsert',
|
MESSAGES_UPSERT = 'messages.upsert',
|
||||||
MESSAGES_UPDATE = 'messages.update',
|
MESSAGES_UPDATE = 'messages.update',
|
||||||
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',
|
||||||
|
@ -29,6 +29,7 @@ import { AuthRepository } from './repository/auth.repository';
|
|||||||
import { WAStartupService } from './services/whatsapp.service';
|
import { WAStartupService } from './services/whatsapp.service';
|
||||||
import { delay } from '@whiskeysockets/baileys';
|
import { delay } from '@whiskeysockets/baileys';
|
||||||
import { Events } from './types/wa.types';
|
import { Events } from './types/wa.types';
|
||||||
|
import { RedisCache } from '../db/redis.client';
|
||||||
|
|
||||||
const logger = new Logger('WA MODULE');
|
const logger = new Logger('WA MODULE');
|
||||||
|
|
||||||
@ -46,10 +47,18 @@ export const repository = new RepositoryBroker(
|
|||||||
messageUpdateRepository,
|
messageUpdateRepository,
|
||||||
webhookRepository,
|
webhookRepository,
|
||||||
authRepository,
|
authRepository,
|
||||||
|
configService,
|
||||||
dbserver?.getClient(),
|
dbserver?.getClient(),
|
||||||
);
|
);
|
||||||
|
|
||||||
export const waMonitor = new WAMonitoringService(eventEmitter, configService, repository);
|
export const cache = new RedisCache();
|
||||||
|
|
||||||
|
export const waMonitor = new WAMonitoringService(
|
||||||
|
eventEmitter,
|
||||||
|
configService,
|
||||||
|
repository,
|
||||||
|
cache,
|
||||||
|
);
|
||||||
|
|
||||||
const authService = new AuthService(configService, waMonitor, repository);
|
const authService = new AuthService(configService, waMonitor, repository);
|
||||||
|
|
||||||
@ -64,6 +73,7 @@ export const instanceController = new InstanceController(
|
|||||||
eventEmitter,
|
eventEmitter,
|
||||||
authService,
|
authService,
|
||||||
webhookService,
|
webhookService,
|
||||||
|
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);
|
||||||
@ -71,10 +81,11 @@ export const chatController = new ChatController(waMonitor);
|
|||||||
export const groupController = new GroupController(waMonitor);
|
export const groupController = new GroupController(waMonitor);
|
||||||
|
|
||||||
export async function initInstance() {
|
export async function initInstance() {
|
||||||
const instance = new WAStartupService(configService, eventEmitter, repository);
|
const instance = new WAStartupService(configService, eventEmitter, repository, cache);
|
||||||
|
|
||||||
const mode = configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
|
const mode = configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
|
||||||
|
|
||||||
|
logger.verbose('Sending data webhook for event: ' + Events.APPLICATION_STARTUP);
|
||||||
instance.sendDataWebhook(
|
instance.sendDataWebhook(
|
||||||
Events.APPLICATION_STARTUP,
|
Events.APPLICATION_STARTUP,
|
||||||
{
|
{
|
||||||
@ -85,9 +96,14 @@ export async function initInstance() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (mode === 'container') {
|
if (mode === 'container') {
|
||||||
|
logger.verbose('Application startup in container mode');
|
||||||
|
|
||||||
const instanceName = configService.get<Auth>('AUTHENTICATION').INSTANCE.NAME;
|
const instanceName = configService.get<Auth>('AUTHENTICATION').INSTANCE.NAME;
|
||||||
|
logger.verbose('Instance name: ' + instanceName);
|
||||||
|
|
||||||
const instanceWebhook =
|
const instanceWebhook =
|
||||||
configService.get<Auth>('AUTHENTICATION').INSTANCE.WEBHOOK_URL;
|
configService.get<Auth>('AUTHENTICATION').INSTANCE.WEBHOOK_URL;
|
||||||
|
logger.verbose('Instance webhook: ' + instanceWebhook);
|
||||||
|
|
||||||
instance.instanceName = instanceName;
|
instance.instanceName = instanceName;
|
||||||
|
|
||||||
@ -96,13 +112,17 @@ export async function initInstance() {
|
|||||||
|
|
||||||
const hash = await authService.generateHash({
|
const hash = await authService.generateHash({
|
||||||
instanceName: instance.instanceName,
|
instanceName: instance.instanceName,
|
||||||
|
token: configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
|
||||||
});
|
});
|
||||||
|
logger.verbose('Hash generated: ' + hash);
|
||||||
|
|
||||||
if (instanceWebhook) {
|
if (instanceWebhook) {
|
||||||
|
logger.verbose('Creating webhook for instance: ' + instanceName);
|
||||||
try {
|
try {
|
||||||
webhookService.create(instance, { enabled: true, url: instanceWebhook });
|
webhookService.create(instance, { enabled: true, url: instanceWebhook });
|
||||||
|
logger.verbose('Webhook created');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.log(error);
|
logger.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +140,7 @@ export async function initInstance() {
|
|||||||
return await this.connectionState({ instanceName });
|
return await this.connectionState({ instanceName });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.log(error);
|
logger.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = {
|
const result = {
|
||||||
|
BIN
temp/audio.mp4
BIN
temp/audio.mp4
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 8.6 KiB |
Binary file not shown.
Before Width: | Height: | Size: 8.4 KiB |
Loading…
Reference in New Issue
Block a user