mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-19 11:52:20 -06:00
Compare commits
111 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb6e58b3c4 | ||
|
|
67e98456bb | ||
|
|
3e47420534 | ||
|
|
1d3d557c43 | ||
|
|
3f41974a75 | ||
|
|
3e3c7397a5 | ||
|
|
dcb51702e7 | ||
|
|
de0c9a1eff | ||
|
|
dd0c1e20a7 | ||
|
|
4769d75dc3 | ||
|
|
6b926dc697 | ||
|
|
ecae077c6d | ||
|
|
78ab1bed35 | ||
|
|
2b6dbfde6b | ||
|
|
6bb1abd7f0 | ||
|
|
aef92240cc | ||
|
|
f0d8c2d095 | ||
|
|
14529f2c35 | ||
|
|
89f40d54d9 | ||
|
|
4c006970a2 | ||
|
|
de676041df | ||
|
|
82b1567ae5 | ||
|
|
1cd7291068 | ||
|
|
fdee1df5b3 | ||
|
|
c314d00ccd | ||
|
|
62e2a8a6e3 | ||
|
|
a12231a0aa | ||
|
|
183efd427a | ||
|
|
f95f3126c3 | ||
|
|
4d9ca4b451 | ||
|
|
c76334a68a | ||
|
|
f475391ba6 | ||
|
|
b77f22790b | ||
|
|
757a578c6e | ||
|
|
c5824767c8 | ||
|
|
84f3f07279 | ||
|
|
f8e1892eee | ||
|
|
036a8edca0 | ||
|
|
58ed6f395f | ||
|
|
ef4be6a612 | ||
|
|
3d8e6f4394 | ||
|
|
0cc1f18a7e | ||
|
|
8b6e577b8f | ||
|
|
cc91f2e5db | ||
|
|
8d1f2313ac | ||
|
|
f9abd90cc9 | ||
|
|
f7293255cf | ||
|
|
7d6a130cf9 | ||
|
|
be7bb2e39f | ||
|
|
1c30728880 | ||
|
|
8d91e7cb1d | ||
|
|
68d980795a | ||
|
|
45c11a5a8e | ||
|
|
4d00351db7 | ||
|
|
fff420b652 | ||
|
|
7103a95305 | ||
|
|
bcada5d553 | ||
|
|
c9b24ff612 | ||
|
|
854c7ed04d | ||
|
|
c0054959cd | ||
|
|
1aa837d220 | ||
|
|
95df402c4c | ||
|
|
2f3d6f7e63 | ||
|
|
ffe1523170 | ||
|
|
a73d5f4b4d | ||
|
|
f35b62ed12 | ||
|
|
20abdd2908 | ||
|
|
28c2c7285c | ||
|
|
3ca8ab12a4 | ||
|
|
fd82aa143c | ||
|
|
1fcbd4f9fd | ||
|
|
73d9cd62a5 | ||
|
|
be699d24a1 | ||
|
|
798eb90bed | ||
|
|
c252f5f8d9 | ||
|
|
76d77ad76f | ||
|
|
69f5cdd61a | ||
|
|
9f52f20660 | ||
|
|
90048afa9d | ||
|
|
1ec3ed32ee | ||
|
|
16ed5821e2 | ||
|
|
b681e33944 | ||
|
|
8f4d44a212 | ||
|
|
93a5d07f9a | ||
|
|
40c230c7db | ||
|
|
d0fa3b92f8 | ||
|
|
2a7727cf5f | ||
|
|
98722e7acf | ||
|
|
683fe4c3db | ||
|
|
e851696430 | ||
|
|
b2ccf965bb | ||
|
|
f847f38812 | ||
|
|
19039aa281 | ||
|
|
091b920a22 | ||
|
|
d7f264c1c2 | ||
|
|
897f8164b9 | ||
|
|
796287a776 | ||
|
|
763e30bd1d | ||
|
|
5121374d60 | ||
|
|
3e3a175bdc | ||
|
|
4a1aa9130b | ||
|
|
5a3f5f60b6 | ||
|
|
8c1600be55 | ||
|
|
7d3ae2347b | ||
|
|
27add47db4 | ||
|
|
836bcab036 | ||
|
|
2d20a07dfb | ||
|
|
24c5c70466 | ||
|
|
22ead22499 | ||
|
|
65e1620b43 | ||
|
|
956c391f13 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -16,6 +16,8 @@ lerna-debug.log*
|
|||||||
/docker-compose-data
|
/docker-compose-data
|
||||||
/docker-data
|
/docker-data
|
||||||
|
|
||||||
|
docker-compose.yaml
|
||||||
|
|
||||||
# Package
|
# Package
|
||||||
/yarn.lock
|
/yarn.lock
|
||||||
/package-lock.json
|
/package-lock.json
|
||||||
|
|||||||
94
CHANGELOG.md
94
CHANGELOG.md
@@ -1,10 +1,102 @@
|
|||||||
|
# 1.4.5 (2023-07-26 09:32)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fixed problems in localization template in chatwoot
|
||||||
|
* Fix mids going duplicated in chatwoot
|
||||||
|
|
||||||
|
# 1.4.4 (2023-07-25 15:24)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fixed chatwoot line wrap issue
|
||||||
|
* Solved receive location in chatwoot
|
||||||
|
* When requesting the pairing code, it also brings the qr code
|
||||||
|
* Option reopen_conversation in chatwoot endpoint
|
||||||
|
* Option conversation_pending in chatwoot endpoint
|
||||||
|
|
||||||
|
# 1.4.3 (2023-07-25 10:51)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Adjusts in settings with options always_online, read_messages and read_status
|
||||||
|
* Fixed send webhook for event CALL
|
||||||
|
* Create instance with settings
|
||||||
|
|
||||||
|
# 1.4.2 (2023-07-24 20:52)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fixed validation is set settings
|
||||||
|
* Adjusts in group validations
|
||||||
|
* Ajusts in sticker message to chatwoot
|
||||||
|
|
||||||
|
# 1.4.1 (2023-07-24 18:28)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fixed reconnect with pairing code or qrcode
|
||||||
|
* Fixed problem in createJid
|
||||||
|
|
||||||
|
# 1.4.0 (2023-07-24 17:03)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* Added connection functionality via pairing code
|
||||||
|
* Added fetch profile endpoint in chat controller
|
||||||
|
* Created settings controller
|
||||||
|
* Added reject call and send text message when receiving a call
|
||||||
|
* Added setting to ignore group messages
|
||||||
|
* Added connection with pairing code in chatwoot with command /init:{NUMBER}
|
||||||
|
* Added encoding option in endpoint sendWhatsAppAudio
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Added link preview option in send text message
|
||||||
|
* Fixed problem with fileSha256 appearing when sending a sticker in chatwoot
|
||||||
|
* Fixed issue where it was not possible to open a conversation when sent at first by me on my cell phone in chatwoot
|
||||||
|
* Now it only updates the contact name if it is the same as the phone number in chatwoot
|
||||||
|
* Now accepts all chatwoot inbox templates
|
||||||
|
* Command to create new instances set to /new_instance:{NAME}:{NUMBER}
|
||||||
|
* Fix in chatwoot set, sign msg can now be disabled
|
||||||
|
|
||||||
|
### Integrations
|
||||||
|
|
||||||
|
- Chatwoot: v2.18.0 - v3.0.0 (Beta)
|
||||||
|
|
||||||
|
# 1.3.2 (2023-07-21 17:19)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Fix in update settings that needed to restart after updated
|
||||||
|
* Correction in the use of the api with mongodb
|
||||||
|
* Adjustments to search endpoint for contacts, chats, messages and Status messages
|
||||||
|
* Now when deleting the instance, the data referring to it in mongodb is also deleted
|
||||||
|
* It is now validated if the instance name contains uppercase and special characters
|
||||||
|
* For compatibility reasons, container mode has been removed
|
||||||
|
* Added docker-compose files example
|
||||||
|
|
||||||
|
### Integrations
|
||||||
|
|
||||||
|
- Chatwoot: v2.18.0
|
||||||
|
|
||||||
|
# 1.3.1 (2023-07-20 07:48)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* Adjust in create store files
|
||||||
|
|
||||||
|
### Integrations
|
||||||
|
|
||||||
|
- Chatwoot: v2.18.0
|
||||||
|
|
||||||
# 1.3.0 (2023-07-19 11:33)
|
# 1.3.0 (2023-07-19 11:33)
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
* Added messages.delete event
|
* Added messages.delete event
|
||||||
* Added restart instance endpoint
|
* Added restart instance endpoint
|
||||||
* Created automation for creating instances in the chatwoot bot with the command '#inbox_whatsapp:<INSTANCE_NAME>'
|
* Created automation for creating instances in the chatwoot bot with the command '#inbox_whatsapp:{INSTANCE_NAME}
|
||||||
* Change Baileys version to: 6.4.0
|
* Change Baileys version to: 6.4.0
|
||||||
* Send contact in chatwoot
|
* Send contact in chatwoot
|
||||||
* Send contact array in chatwoot
|
* Send contact array in chatwoot
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
SERVER_URL='http://localhost:8080' # http://localhost:3333 | http://localhost:3333/api/v1
|
# Server URL - Set your application url
|
||||||
|
SERVER_URL=http://localhost:8080
|
||||||
|
|
||||||
CORS_ORIGIN='*' # Or separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'
|
# Cors - * for all or set separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'
|
||||||
CORS_METHODS='POST,GET,PUT,DELETE'
|
CORS_ORIGIN=*
|
||||||
|
CORS_METHODS=POST,GET,PUT,DELETE
|
||||||
CORS_CREDENTIALS=true
|
CORS_CREDENTIALS=true
|
||||||
|
|
||||||
# Determine the logs to be displayed
|
# Determine the logs to be displayed
|
||||||
LOG_LEVEL='ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK,WEBHOOKS'
|
LOG_LEVEL=ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK,WEBHOOKS
|
||||||
LOG_COLOR=true
|
LOG_COLOR=true
|
||||||
LOG_BAILEYS=error # fatal | error | warn | info | debug | trace
|
# Log Baileys - "fatal" | "error" | "warn" | "info" | "debug" | "trace"
|
||||||
|
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
|
||||||
@@ -20,16 +23,17 @@ STORE_MESSAGE_UP=true
|
|||||||
STORE_CONTACTS=true
|
STORE_CONTACTS=true
|
||||||
STORE_CHATS=true
|
STORE_CHATS=true
|
||||||
|
|
||||||
CLEAN_STORE_CLEANING_INTERVAL=7200 # seconds === 2h
|
# Set Store Interval in Seconds (7200 = 2h)
|
||||||
|
CLEAN_STORE_CLEANING_INTERVAL=7200
|
||||||
CLEAN_STORE_MESSAGES=true
|
CLEAN_STORE_MESSAGES=true
|
||||||
CLEAN_STORE_MESSAGE_UP=true
|
CLEAN_STORE_MESSAGE_UP=true
|
||||||
CLEAN_STORE_CONTACTS=true
|
CLEAN_STORE_CONTACTS=true
|
||||||
CLEAN_STORE_CHATS=true
|
CLEAN_STORE_CHATS=true
|
||||||
|
|
||||||
# Permanent data storage
|
# Permanent data storage
|
||||||
DATABASE_ENABLED=false
|
DATABASE_ENABLED=true
|
||||||
DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true
|
DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true
|
||||||
DATABASE_CONNECTION_DB_PREFIX_NAME=evolution
|
DATABASE_CONNECTION_DB_PREFIX_NAME=evdocker
|
||||||
|
|
||||||
# Choose the data you want to save in the application's database or store
|
# Choose the data you want to save in the application's database or store
|
||||||
DATABASE_SAVE_DATA_INSTANCE=false
|
DATABASE_SAVE_DATA_INSTANCE=false
|
||||||
@@ -38,14 +42,14 @@ DATABASE_SAVE_MESSAGE_UPDATE=false
|
|||||||
DATABASE_SAVE_DATA_CONTACTS=false
|
DATABASE_SAVE_DATA_CONTACTS=false
|
||||||
DATABASE_SAVE_DATA_CHATS=false
|
DATABASE_SAVE_DATA_CHATS=false
|
||||||
|
|
||||||
REDIS_ENABLED=false
|
REDIS_ENABLED=true
|
||||||
REDIS_URI=redis://redis:6379
|
REDIS_URI=redis://redis:6379
|
||||||
REDIS_PREFIX_KEY=evolution
|
REDIS_PREFIX_KEY=evdocker
|
||||||
|
|
||||||
# Global Webhook Settings
|
# Global Webhook Settings
|
||||||
# Each instance's Webhook URL and events will be requested at the time it is created
|
# Each instance's Webhook URL and events will be requested at the time it is created
|
||||||
## Define a global webhook that will listen for enabled events from all instances
|
## Define a global webhook that will listen for enabled events from all instances
|
||||||
WEBHOOK_GLOBAL_URL='<url>'
|
WEBHOOK_GLOBAL_URL=''
|
||||||
WEBHOOK_GLOBAL_ENABLED=false
|
WEBHOOK_GLOBAL_ENABLED=false
|
||||||
# With this option activated, you work with a url per webhook event, respecting the global url and the name of each event
|
# With this option activated, you work with a url per webhook event, respecting the global url and the name of each event
|
||||||
WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=false
|
WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=false
|
||||||
@@ -69,12 +73,14 @@ WEBHOOK_EVENTS_GROUPS_UPSERT=true
|
|||||||
WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
||||||
WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
||||||
WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
||||||
|
WEBHOOK_EVENTS_CALL=true
|
||||||
# This event fires every time a new token is requested via the refresh route
|
# This event fires every time a new token is requested via the refresh route
|
||||||
WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
||||||
|
|
||||||
# Name that will be displayed on smartphone connection
|
# Name that will be displayed on smartphone connection
|
||||||
CONFIG_SESSION_PHONE_CLIENT='Evolution API'
|
CONFIG_SESSION_PHONE_CLIENT=EvolutionAPI
|
||||||
CONFIG_SESSION_PHONE_NAME=chrome # chrome | firefox | edge | opera | safari
|
# Browser Name = chrome | firefox | edge | opera | safari
|
||||||
|
CONFIG_SESSION_PHONE_NAME=chrome
|
||||||
|
|
||||||
# Set qrcode display limit
|
# Set qrcode display limit
|
||||||
QRCODE_LIMIT=30
|
QRCODE_LIMIT=30
|
||||||
@@ -82,20 +88,13 @@ QRCODE_LIMIT=30
|
|||||||
# Defines an authentication type for the api
|
# Defines an authentication type for the api
|
||||||
# We recommend using the apikey because it will allow you to use a custom token,
|
# We recommend using the apikey because it will allow you to use a custom token,
|
||||||
# if you use jwt, a random token will be generated and may be expired and you will have to generate a new token
|
# if you use jwt, a random token will be generated and may be expired and you will have to generate a new token
|
||||||
AUTHENTICATION_TYPE='apikey' # jwt or 'apikey'
|
# jwt or 'apikey'
|
||||||
|
AUTHENTICATION_TYPE=apikey
|
||||||
## Define a global apikey to access all instances.
|
## Define a global apikey to access all instances.
|
||||||
### OBS: This key must be inserted in the request header to create an instance.
|
### OBS: This key must be inserted in the request header to create an instance.
|
||||||
AUTHENTICATION_API_KEY='B6D711FCDE4D4FD5936544120E713976'
|
AUTHENTICATION_API_KEY=B6D711FCDE4D4FD5936544120E713976
|
||||||
AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
|
AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
|
||||||
## Set the secret key to encrypt and decrypt your token and its expiration time
|
## Set the secret key to encrypt and decrypt your token and its expiration time
|
||||||
AUTHENTICATION_JWT_EXPIRIN_IN=0 # seconds - 3600s ===1h | zero (0) - never expires
|
# seconds - 3600s ===1h | zero (0) - never expires
|
||||||
AUTHENTICATION_JWT_SECRET='L0YWtjb2w554WFqPG'
|
AUTHENTICATION_JWT_EXPIRIN_IN=0
|
||||||
# Set the instance name and webhook url to create an instance in init the application
|
AUTHENTICATION_JWT_SECRET='L=0YWt]b2w[WF>#>:&E`'
|
||||||
# With this option activated, you work with a url per webhook event, respecting the local url and the name of each event
|
|
||||||
AUTHENTICATION_INSTANCE_MODE=server # container or server
|
|
||||||
# if you are using container mode, set the container name and the webhook url to default instance
|
|
||||||
AUTHENTICATION_INSTANCE_NAME=evolution
|
|
||||||
AUTHENTICATION_INSTANCE_WEBHOOK_URL='<url>'
|
|
||||||
AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID=1
|
|
||||||
AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN=123456
|
|
||||||
AUTHENTICATION_INSTANCE_CHATWOOT_URL='<url>'
|
|
||||||
|
|||||||
@@ -13,13 +13,6 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- 6379:6379
|
- 6379:6379
|
||||||
|
|
||||||
rebrow:
|
|
||||||
image: marian/rebrow
|
|
||||||
ports:
|
|
||||||
- 5001:5001
|
|
||||||
links:
|
|
||||||
- redis
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
evolution_redis:
|
evolution_redis:
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ COPY ./package.json .
|
|||||||
|
|
||||||
ENV DOCKER_ENV=true
|
ENV DOCKER_ENV=true
|
||||||
|
|
||||||
ENV SERVER_URL='http://localhost:8080'
|
ENV SERVER_URL=http://localhost:8080
|
||||||
|
|
||||||
ENV CORS_ORIGIN=*
|
ENV CORS_ORIGIN=*
|
||||||
ENV CORS_METHODS=POST,GET,PUT,DELETE
|
ENV CORS_METHODS=POST,GET,PUT,DELETE
|
||||||
@@ -74,10 +74,11 @@ ENV WEBHOOK_EVENTS_GROUPS_UPSERT=true
|
|||||||
ENV WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
ENV WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
||||||
ENV WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
ENV WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
||||||
ENV WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
ENV WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
||||||
|
ENV WEBHOOK_EVENTS_CALL=true
|
||||||
|
|
||||||
ENV WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
ENV WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
||||||
|
|
||||||
ENV CONFIG_SESSION_PHONE_CLIENT='Evolution API'
|
ENV CONFIG_SESSION_PHONE_CLIENT=EvolutionAPI
|
||||||
ENV CONFIG_SESSION_PHONE_NAME=chrome
|
ENV CONFIG_SESSION_PHONE_NAME=chrome
|
||||||
|
|
||||||
ENV QRCODE_LIMIT=30
|
ENV QRCODE_LIMIT=30
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- evolution_instances:/evolution/instances
|
- evolution_instances:/evolution/instances
|
||||||
- evolution_store:/evolution/store
|
- evolution_store:/evolution/store
|
||||||
|
networks:
|
||||||
|
- evolution-net
|
||||||
env_file:
|
env_file:
|
||||||
- ./Docker/.env
|
- ./Docker/.env
|
||||||
command: ['node', './dist/src/main.js']
|
command: ['node', './dist/src/main.js']
|
||||||
79
docker-compose.yaml.example.complete
Normal file
79
docker-compose.yaml.example.complete
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
version: '3.3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
api:
|
||||||
|
container_name: evolution_api
|
||||||
|
image: evolution/api:local
|
||||||
|
restart: always
|
||||||
|
ports:
|
||||||
|
- 8080:8080
|
||||||
|
volumes:
|
||||||
|
- evolution_instances:/evolution/instances
|
||||||
|
- evolution_store:/evolution/store
|
||||||
|
networks:
|
||||||
|
- evolution-net
|
||||||
|
env_file:
|
||||||
|
- ./Docker/.env
|
||||||
|
command: ['node', './dist/src/main.js']
|
||||||
|
expose:
|
||||||
|
- 8080
|
||||||
|
|
||||||
|
mongodb:
|
||||||
|
container_name: mongodb
|
||||||
|
image: mongo
|
||||||
|
restart: always
|
||||||
|
ports:
|
||||||
|
- 27017:27017
|
||||||
|
environment:
|
||||||
|
- MONGO_INITDB_ROOT_USERNAME=root
|
||||||
|
- MONGO_INITDB_ROOT_PASSWORD=root
|
||||||
|
- PUID=1000
|
||||||
|
- PGID=1000
|
||||||
|
volumes:
|
||||||
|
- evolution_mongodb_data:/data/db
|
||||||
|
- evolution_mongodb_configdb:/data/configdb
|
||||||
|
networks:
|
||||||
|
- evolution-net
|
||||||
|
expose:
|
||||||
|
- 27017
|
||||||
|
|
||||||
|
mongo-express:
|
||||||
|
image: mongo-express
|
||||||
|
networks:
|
||||||
|
- evolution-net
|
||||||
|
environment:
|
||||||
|
ME_CONFIG_BASICAUTH_USERNAME: root
|
||||||
|
ME_CONFIG_BASICAUTH_PASSWORD: root
|
||||||
|
ME_CONFIG_MONGODB_SERVER: mongodb
|
||||||
|
ME_CONFIG_MONGODB_ADMINUSERNAME: root
|
||||||
|
ME_CONFIG_MONGODB_ADMINPASSWORD: root
|
||||||
|
ports:
|
||||||
|
- 8081:8081
|
||||||
|
links:
|
||||||
|
- mongodb
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:latest
|
||||||
|
container_name: redis
|
||||||
|
command: >
|
||||||
|
redis-server
|
||||||
|
--port 6379
|
||||||
|
--appendonly yes
|
||||||
|
volumes:
|
||||||
|
- evolution_redis:/data
|
||||||
|
networks:
|
||||||
|
- evolution-net
|
||||||
|
ports:
|
||||||
|
- 6379:6379
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
evolution_instances:
|
||||||
|
evolution_store:
|
||||||
|
evolution_mongodb_data:
|
||||||
|
evolution_mongodb_configdb:
|
||||||
|
evolution_redis:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
evolution-net:
|
||||||
|
external: true
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "evolution-api",
|
"name": "evolution-api",
|
||||||
"version": "1.3.0",
|
"version": "1.4.5",
|
||||||
"description": "Rest api for communication with WhatsApp",
|
"description": "Rest api for communication with WhatsApp",
|
||||||
"main": "./dist/src/main.js",
|
"main": "./dist/src/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -89,25 +89,18 @@ export type EventsWebhook = {
|
|||||||
GROUPS_UPSERT: boolean;
|
GROUPS_UPSERT: boolean;
|
||||||
GROUP_UPDATE: boolean;
|
GROUP_UPDATE: boolean;
|
||||||
GROUP_PARTICIPANTS_UPDATE: boolean;
|
GROUP_PARTICIPANTS_UPDATE: boolean;
|
||||||
|
CALL: boolean;
|
||||||
NEW_JWT_TOKEN: boolean;
|
NEW_JWT_TOKEN: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
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 Instance = {
|
|
||||||
NAME: string;
|
|
||||||
WEBHOOK_URL: string;
|
|
||||||
MODE: string;
|
|
||||||
CHATWOOT_ACCOUNT_ID: string;
|
|
||||||
CHATWOOT_TOKEN: string;
|
|
||||||
CHATWOOT_URL: 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';
|
||||||
INSTANCE: Instance;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type DelInstance = number | boolean;
|
export type DelInstance = number | boolean;
|
||||||
@@ -253,6 +246,7 @@ export class ConfigService {
|
|||||||
GROUP_UPDATE: process.env?.WEBHOOK_EVENTS_GROUPS_UPDATE === 'true',
|
GROUP_UPDATE: process.env?.WEBHOOK_EVENTS_GROUPS_UPDATE === 'true',
|
||||||
GROUP_PARTICIPANTS_UPDATE:
|
GROUP_PARTICIPANTS_UPDATE:
|
||||||
process.env?.WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE === 'true',
|
process.env?.WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE === 'true',
|
||||||
|
CALL: process.env?.WEBHOOK_EVENTS_CALL === 'true',
|
||||||
NEW_JWT_TOKEN: process.env?.WEBHOOK_EVENTS_NEW_JWT_TOKEN === 'true',
|
NEW_JWT_TOKEN: process.env?.WEBHOOK_EVENTS_NEW_JWT_TOKEN === 'true',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -276,15 +270,6 @@ export class ConfigService {
|
|||||||
: 3600,
|
: 3600,
|
||||||
SECRET: process.env.AUTHENTICATION_JWT_SECRET,
|
SECRET: process.env.AUTHENTICATION_JWT_SECRET,
|
||||||
},
|
},
|
||||||
INSTANCE: {
|
|
||||||
NAME: process.env.AUTHENTICATION_INSTANCE_NAME,
|
|
||||||
WEBHOOK_URL: process.env.AUTHENTICATION_INSTANCE_WEBHOOK_URL,
|
|
||||||
MODE: process.env.AUTHENTICATION_INSTANCE_MODE,
|
|
||||||
CHATWOOT_ACCOUNT_ID:
|
|
||||||
process.env.AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID || '',
|
|
||||||
CHATWOOT_TOKEN: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN || '',
|
|
||||||
CHATWOOT_URL: process.env.AUTHENTICATION_INSTANCE_CHATWOOT_URL || '',
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,6 +110,7 @@ WEBHOOK:
|
|||||||
GROUP_UPDATE: true
|
GROUP_UPDATE: true
|
||||||
GROUP_PARTICIPANTS_UPDATE: true
|
GROUP_PARTICIPANTS_UPDATE: true
|
||||||
CONNECTION_UPDATE: true
|
CONNECTION_UPDATE: true
|
||||||
|
CALL: true
|
||||||
# This event fires every time a new token is requested via the refresh route
|
# This event fires every time a new token is requested via the refresh route
|
||||||
NEW_JWT_TOKEN: false
|
NEW_JWT_TOKEN: false
|
||||||
|
|
||||||
@@ -137,13 +138,3 @@ AUTHENTICATION:
|
|||||||
JWT:
|
JWT:
|
||||||
EXPIRIN_IN: 0 # seconds - 3600s === 1h | zero (0) - never expires
|
EXPIRIN_IN: 0 # seconds - 3600s === 1h | zero (0) - never expires
|
||||||
SECRET: L=0YWt]b2w[WF>#>:&E`
|
SECRET: L=0YWt]b2w[WF>#>:&E`
|
||||||
# Set the instance name and webhook url to create an instance in init the application
|
|
||||||
INSTANCE:
|
|
||||||
# With this option activated, you work with a url per webhook event, respecting the local url and the name of each event
|
|
||||||
MODE: server # container or server
|
|
||||||
# if you are using container mode, set the container name and the webhook url to default instance
|
|
||||||
NAME: evolution
|
|
||||||
WEBHOOK_URL: <url>
|
|
||||||
CHATWOOT_ACCOUNT_ID: 1
|
|
||||||
CHATWOOT_TOKEN: 123456
|
|
||||||
CHATWOOT_URL: <url>
|
|
||||||
|
|||||||
@@ -53,11 +53,13 @@ export const instanceNameSchema: JSONSchema7 = {
|
|||||||
'GROUP_UPDATE',
|
'GROUP_UPDATE',
|
||||||
'GROUP_PARTICIPANTS_UPDATE',
|
'GROUP_PARTICIPANTS_UPDATE',
|
||||||
'CONNECTION_UPDATE',
|
'CONNECTION_UPDATE',
|
||||||
|
'CALL',
|
||||||
'NEW_JWT_TOKEN',
|
'NEW_JWT_TOKEN',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
qrcode: { type: 'boolean', enum: [true, false] },
|
qrcode: { type: 'boolean', enum: [true, false] },
|
||||||
|
number: { type: 'string', pattern: '^\\d+[\\.@\\w-]+' },
|
||||||
token: { type: 'string' },
|
token: { type: 'string' },
|
||||||
},
|
},
|
||||||
...isNotEmpty('instanceName'),
|
...isNotEmpty('instanceName'),
|
||||||
@@ -123,7 +125,6 @@ const optionsSchema: JSONSchema7 = {
|
|||||||
|
|
||||||
const numberDefinition: JSONSchema7Definition = {
|
const numberDefinition: JSONSchema7Definition = {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
pattern: '^\\d+[\\.@\\w-]+',
|
|
||||||
description: 'Invalid format',
|
description: 'Invalid format',
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -398,7 +399,7 @@ export const contactMessageSchema: JSONSchema7 = {
|
|||||||
email: { type: 'string' },
|
email: { type: 'string' },
|
||||||
url: { type: 'string' },
|
url: { type: 'string' },
|
||||||
},
|
},
|
||||||
required: ['fullName', 'wuid', 'phoneNumber'],
|
required: ['fullName', 'phoneNumber'],
|
||||||
...isNotEmpty('fullName'),
|
...isNotEmpty('fullName'),
|
||||||
},
|
},
|
||||||
minItems: 1,
|
minItems: 1,
|
||||||
@@ -445,7 +446,6 @@ export const whatsappNumberSchema: JSONSchema7 = {
|
|||||||
uniqueItems: true,
|
uniqueItems: true,
|
||||||
items: {
|
items: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
pattern: '^\\d+',
|
|
||||||
description: '"numbers" must be an array of numeric strings',
|
description: '"numbers" must be an array of numeric strings',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -456,7 +456,7 @@ export const readMessageSchema: JSONSchema7 = {
|
|||||||
$id: v4(),
|
$id: v4(),
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
readMessages: {
|
read_messages: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
minItems: 1,
|
minItems: 1,
|
||||||
uniqueItems: true,
|
uniqueItems: true,
|
||||||
@@ -471,7 +471,7 @@ export const readMessageSchema: JSONSchema7 = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
required: ['readMessages'],
|
required: ['read_messages'],
|
||||||
};
|
};
|
||||||
|
|
||||||
export const privacySettingsSchema: JSONSchema7 = {
|
export const privacySettingsSchema: JSONSchema7 = {
|
||||||
@@ -587,6 +587,17 @@ export const profilePictureSchema: JSONSchema7 = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const profileSchema: JSONSchema7 = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
wuid: { type: 'string' },
|
||||||
|
name: { type: 'string' },
|
||||||
|
picture: { type: 'string' },
|
||||||
|
status: { type: 'string' },
|
||||||
|
isBusiness: { type: 'boolean' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
export const messageValidateSchema: JSONSchema7 = {
|
export const messageValidateSchema: JSONSchema7 = {
|
||||||
$id: v4(),
|
$id: v4(),
|
||||||
type: 'object',
|
type: 'object',
|
||||||
@@ -657,6 +668,7 @@ export const createGroupSchema: JSONSchema7 = {
|
|||||||
subject: { type: 'string' },
|
subject: { type: 'string' },
|
||||||
description: { type: 'string' },
|
description: { type: 'string' },
|
||||||
profilePicture: { type: 'string' },
|
profilePicture: { type: 'string' },
|
||||||
|
promoteParticipants: { type: 'boolean', enum: [true, false] },
|
||||||
participants: {
|
participants: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
minItems: 1,
|
minItems: 1,
|
||||||
@@ -843,6 +855,7 @@ export const webhookSchema: JSONSchema7 = {
|
|||||||
'GROUP_UPDATE',
|
'GROUP_UPDATE',
|
||||||
'GROUP_PARTICIPANTS_UPDATE',
|
'GROUP_PARTICIPANTS_UPDATE',
|
||||||
'CONNECTION_UPDATE',
|
'CONNECTION_UPDATE',
|
||||||
|
'CALL',
|
||||||
'NEW_JWT_TOKEN',
|
'NEW_JWT_TOKEN',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -861,7 +874,51 @@ export const chatwootSchema: JSONSchema7 = {
|
|||||||
token: { type: 'string' },
|
token: { type: 'string' },
|
||||||
url: { type: 'string' },
|
url: { type: 'string' },
|
||||||
sign_msg: { type: 'boolean', enum: [true, false] },
|
sign_msg: { type: 'boolean', enum: [true, false] },
|
||||||
|
reopen_conversation: { type: 'boolean', enum: [true, false] },
|
||||||
|
conversation_pending: { type: 'boolean', enum: [true, false] },
|
||||||
},
|
},
|
||||||
required: ['enabled', 'account_id', 'token', 'url', 'sign_msg'],
|
required: [
|
||||||
...isNotEmpty('account_id', 'token', 'url', 'sign_msg'),
|
'enabled',
|
||||||
|
'account_id',
|
||||||
|
'token',
|
||||||
|
'url',
|
||||||
|
'sign_msg',
|
||||||
|
'reopen_conversation',
|
||||||
|
'conversation_pending',
|
||||||
|
],
|
||||||
|
...isNotEmpty(
|
||||||
|
'account_id',
|
||||||
|
'token',
|
||||||
|
'url',
|
||||||
|
'sign_msg',
|
||||||
|
'reopen_conversation',
|
||||||
|
'conversation_pending',
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const settingsSchema: JSONSchema7 = {
|
||||||
|
$id: v4(),
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
reject_call: { type: 'boolean', enum: [true, false] },
|
||||||
|
msg_call: { type: 'string' },
|
||||||
|
groups_ignore: { type: 'boolean', enum: [true, false] },
|
||||||
|
always_online: { type: 'boolean', enum: [true, false] },
|
||||||
|
read_messages: { type: 'boolean', enum: [true, false] },
|
||||||
|
read_status: { type: 'boolean', enum: [true, false] },
|
||||||
|
},
|
||||||
|
required: [
|
||||||
|
'reject_call',
|
||||||
|
'groups_ignore',
|
||||||
|
'always_online',
|
||||||
|
'read_messages',
|
||||||
|
'read_status',
|
||||||
|
],
|
||||||
|
...isNotEmpty(
|
||||||
|
'reject_call',
|
||||||
|
'groups_ignore',
|
||||||
|
'always_online',
|
||||||
|
'read_messages',
|
||||||
|
'read_status',
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -48,6 +48,14 @@ export class ChatController {
|
|||||||
return await this.waMonitor.waInstances[instanceName].profilePicture(data.number);
|
return await this.waMonitor.waInstances[instanceName].profilePicture(data.number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async fetchProfile({ instanceName }: InstanceDto, data: NumberDto) {
|
||||||
|
logger.verbose('requested fetchProfile from ' + instanceName + ' instance');
|
||||||
|
return await this.waMonitor.waInstances[instanceName].fetchProfile(
|
||||||
|
instanceName,
|
||||||
|
data.number,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) {
|
public async fetchContacts({ instanceName }: InstanceDto, query: ContactQuery) {
|
||||||
logger.verbose('requested fetchContacts from ' + instanceName + ' instance');
|
logger.verbose('requested fetchContacts from ' + instanceName + ' instance');
|
||||||
return await this.waMonitor.waInstances[instanceName].fetchContacts(query);
|
return await this.waMonitor.waInstances[instanceName].fetchContacts(query);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ export class ChatwootController {
|
|||||||
throw new BadRequestException('token is required');
|
throw new BadRequestException('token is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.sign_msg) {
|
if (data.sign_msg !== true && data.sign_msg !== false) {
|
||||||
throw new BadRequestException('sign_msg is required');
|
throw new BadRequestException('sign_msg is required');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,6 +44,8 @@ export class ChatwootController {
|
|||||||
data.token = '';
|
data.token = '';
|
||||||
data.url = '';
|
data.url = '';
|
||||||
data.sign_msg = false;
|
data.sign_msg = false;
|
||||||
|
data.reopen_conversation = false;
|
||||||
|
data.conversation_pending = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
data.name_inbox = instance.instanceName;
|
data.name_inbox = instance.instanceName;
|
||||||
@@ -94,4 +96,10 @@ export class ChatwootController {
|
|||||||
|
|
||||||
return chatwootService.receiveWebhook(instance, data);
|
return chatwootService.receiveWebhook(instance, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async newInstance(data: any) {
|
||||||
|
const chatwootService = new ChatwootService(waMonitor, this.configService);
|
||||||
|
|
||||||
|
return chatwootService.newInstance(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import { Logger } from '../../config/logger.config';
|
|||||||
import { wa } from '../types/wa.types';
|
import { wa } from '../types/wa.types';
|
||||||
import { RedisCache } from '../../db/redis.client';
|
import { RedisCache } from '../../db/redis.client';
|
||||||
import { isURL } from 'class-validator';
|
import { isURL } from 'class-validator';
|
||||||
|
import { SettingsService } from '../services/settings.service';
|
||||||
|
|
||||||
export class InstanceController {
|
export class InstanceController {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -23,6 +24,7 @@ export class InstanceController {
|
|||||||
private readonly authService: AuthService,
|
private readonly authService: AuthService,
|
||||||
private readonly webhookService: WebhookService,
|
private readonly webhookService: WebhookService,
|
||||||
private readonly chatwootService: ChatwootService,
|
private readonly chatwootService: ChatwootService,
|
||||||
|
private readonly settingsService: SettingsService,
|
||||||
private readonly cache: RedisCache,
|
private readonly cache: RedisCache,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -34,155 +36,29 @@ export class InstanceController {
|
|||||||
webhook_by_events,
|
webhook_by_events,
|
||||||
events,
|
events,
|
||||||
qrcode,
|
qrcode,
|
||||||
|
number,
|
||||||
token,
|
token,
|
||||||
chatwoot_account_id,
|
chatwoot_account_id,
|
||||||
chatwoot_token,
|
chatwoot_token,
|
||||||
chatwoot_url,
|
chatwoot_url,
|
||||||
chatwoot_sign_msg,
|
chatwoot_sign_msg,
|
||||||
|
chatwoot_reopen_conversation,
|
||||||
|
chatwoot_conversation_pending,
|
||||||
|
reject_call,
|
||||||
|
msg_call,
|
||||||
|
groups_ignore,
|
||||||
|
always_online,
|
||||||
|
read_messages,
|
||||||
|
read_status,
|
||||||
}: InstanceDto) {
|
}: InstanceDto) {
|
||||||
|
try {
|
||||||
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
|
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
|
||||||
|
|
||||||
const mode = this.configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
|
if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) {
|
||||||
|
throw new BadRequestException(
|
||||||
if (mode === 'container') {
|
'The instance name must be lowercase and without special characters',
|
||||||
this.logger.verbose('container mode');
|
|
||||||
|
|
||||||
if (Object.keys(this.waMonitor.waInstances).length > 0) {
|
|
||||||
throw new BadRequestException([
|
|
||||||
'Instance already created',
|
|
||||||
'Only one instance can be created',
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
|
|
||||||
this.logger.verbose('instance created');
|
|
||||||
this.logger.verbose({
|
|
||||||
instance: {
|
|
||||||
instanceName: instance.instanceName,
|
|
||||||
status: 'created',
|
|
||||||
},
|
|
||||||
hash,
|
|
||||||
webhook,
|
|
||||||
events: getEvents,
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
instance: {
|
|
||||||
instanceName: instance.instanceName,
|
|
||||||
status: 'created',
|
|
||||||
},
|
|
||||||
hash,
|
|
||||||
webhook,
|
|
||||||
events: getEvents,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!chatwoot_account_id) {
|
|
||||||
throw new BadRequestException('account_id is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!chatwoot_token) {
|
|
||||||
throw new BadRequestException('token is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!chatwoot_url) {
|
|
||||||
throw new BadRequestException('url is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isURL(chatwoot_url, { require_tld: false })) {
|
|
||||||
throw new BadRequestException('Invalid "url" property in chatwoot');
|
|
||||||
}
|
|
||||||
|
|
||||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.chatwootService.create(instance, {
|
|
||||||
enabled: true,
|
|
||||||
account_id: chatwoot_account_id,
|
|
||||||
token: chatwoot_token,
|
|
||||||
url: chatwoot_url,
|
|
||||||
sign_msg: chatwoot_sign_msg || false,
|
|
||||||
name_inbox: instance.instanceName,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.chatwootService.initInstanceChatwoot(
|
|
||||||
instance,
|
|
||||||
instance.instanceName,
|
|
||||||
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
|
||||||
qrcode,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.log(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
instance: {
|
|
||||||
instanceName: instance.instanceName,
|
|
||||||
status: 'created',
|
|
||||||
},
|
|
||||||
hash,
|
|
||||||
chatwoot: {
|
|
||||||
enabled: true,
|
|
||||||
account_id: chatwoot_account_id,
|
|
||||||
token: chatwoot_token,
|
|
||||||
url: chatwoot_url,
|
|
||||||
sign_msg: chatwoot_sign_msg || false,
|
|
||||||
name_inbox: instance.instanceName,
|
|
||||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
this.logger.verbose('server mode');
|
|
||||||
|
|
||||||
this.logger.verbose('checking duplicate token');
|
this.logger.verbose('checking duplicate token');
|
||||||
await this.authService.checkDuplicateToken(token);
|
await this.authService.checkDuplicateToken(token);
|
||||||
@@ -236,30 +112,31 @@ export class InstanceController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('creating settings');
|
||||||
|
const settings: wa.LocalSettings = {
|
||||||
|
reject_call: reject_call || false,
|
||||||
|
msg_call: msg_call || '',
|
||||||
|
groups_ignore: groups_ignore || false,
|
||||||
|
always_online: always_online || false,
|
||||||
|
read_messages: read_messages || false,
|
||||||
|
read_status: read_status || false,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.logger.verbose('settings: ' + JSON.stringify(settings));
|
||||||
|
|
||||||
|
this.settingsService.create(instance, settings);
|
||||||
|
|
||||||
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
|
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
|
||||||
let getQrcode: wa.QrCode;
|
let getQrcode: wa.QrCode;
|
||||||
|
|
||||||
if (qrcode) {
|
if (qrcode) {
|
||||||
this.logger.verbose('creating qrcode');
|
this.logger.verbose('creating qrcode');
|
||||||
await instance.connectToWhatsapp();
|
await instance.connectToWhatsapp(number);
|
||||||
await delay(2000);
|
await delay(5000);
|
||||||
getQrcode = instance.qrCode;
|
getQrcode = instance.qrCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('instance created');
|
const result = {
|
||||||
this.logger.verbose({
|
|
||||||
instance: {
|
|
||||||
instanceName: instance.instanceName,
|
|
||||||
status: 'created',
|
|
||||||
},
|
|
||||||
hash,
|
|
||||||
webhook,
|
|
||||||
webhook_by_events,
|
|
||||||
events: getEvents,
|
|
||||||
qrcode: getQrcode,
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
instance: {
|
instance: {
|
||||||
instanceName: instance.instanceName,
|
instanceName: instance.instanceName,
|
||||||
status: 'created',
|
status: 'created',
|
||||||
@@ -268,8 +145,14 @@ export class InstanceController {
|
|||||||
webhook,
|
webhook,
|
||||||
webhook_by_events,
|
webhook_by_events,
|
||||||
events: getEvents,
|
events: getEvents,
|
||||||
|
settings,
|
||||||
qrcode: getQrcode,
|
qrcode: getQrcode,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.logger.verbose('instance created');
|
||||||
|
this.logger.verbose(result);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!chatwoot_account_id) {
|
if (!chatwoot_account_id) {
|
||||||
@@ -288,6 +171,24 @@ export class InstanceController {
|
|||||||
throw new BadRequestException('Invalid "url" property in chatwoot');
|
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;
|
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -298,6 +199,9 @@ export class InstanceController {
|
|||||||
url: chatwoot_url,
|
url: chatwoot_url,
|
||||||
sign_msg: chatwoot_sign_msg || false,
|
sign_msg: chatwoot_sign_msg || false,
|
||||||
name_inbox: instance.instanceName,
|
name_inbox: instance.instanceName,
|
||||||
|
number,
|
||||||
|
reopen_conversation: chatwoot_reopen_conversation || false,
|
||||||
|
conversation_pending: chatwoot_conversation_pending || false,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.chatwootService.initInstanceChatwoot(
|
this.chatwootService.initInstanceChatwoot(
|
||||||
@@ -305,6 +209,7 @@ export class InstanceController {
|
|||||||
instance.instanceName,
|
instance.instanceName,
|
||||||
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||||
qrcode,
|
qrcode,
|
||||||
|
number,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.log(error);
|
this.logger.log(error);
|
||||||
@@ -319,20 +224,27 @@ export class InstanceController {
|
|||||||
webhook,
|
webhook,
|
||||||
webhook_by_events,
|
webhook_by_events,
|
||||||
events: getEvents,
|
events: getEvents,
|
||||||
|
settings,
|
||||||
chatwoot: {
|
chatwoot: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
account_id: chatwoot_account_id,
|
account_id: chatwoot_account_id,
|
||||||
token: chatwoot_token,
|
token: chatwoot_token,
|
||||||
url: chatwoot_url,
|
url: chatwoot_url,
|
||||||
sign_msg: chatwoot_sign_msg || false,
|
sign_msg: chatwoot_sign_msg || false,
|
||||||
|
reopen_conversation: chatwoot_reopen_conversation || false,
|
||||||
|
conversation_pending: chatwoot_conversation_pending || false,
|
||||||
|
number,
|
||||||
name_inbox: instance.instanceName,
|
name_inbox: instance.instanceName,
|
||||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return { error: true, message: error.toString() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connectToWhatsapp({ instanceName }: InstanceDto) {
|
public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) {
|
||||||
try {
|
try {
|
||||||
this.logger.verbose(
|
this.logger.verbose(
|
||||||
'requested connectToWhatsapp from ' + instanceName + ' instance',
|
'requested connectToWhatsapp from ' + instanceName + ' instance',
|
||||||
@@ -343,17 +255,29 @@ export class InstanceController {
|
|||||||
|
|
||||||
this.logger.verbose('state: ' + state);
|
this.logger.verbose('state: ' + state);
|
||||||
|
|
||||||
switch (state) {
|
if (state == 'open') {
|
||||||
case 'close':
|
|
||||||
this.logger.verbose('connecting');
|
|
||||||
await instance.connectToWhatsapp();
|
|
||||||
await delay(2000);
|
|
||||||
return instance.qrCode;
|
|
||||||
case 'connecting':
|
|
||||||
return instance.qrCode;
|
|
||||||
default:
|
|
||||||
return await this.connectionState({ instanceName });
|
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) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
}
|
}
|
||||||
@@ -374,7 +298,12 @@ export class InstanceController {
|
|||||||
|
|
||||||
public async connectionState({ instanceName }: InstanceDto) {
|
public async connectionState({ instanceName }: InstanceDto) {
|
||||||
this.logger.verbose('requested connectionState from ' + instanceName + ' instance');
|
this.logger.verbose('requested connectionState from ' + instanceName + ' instance');
|
||||||
return this.waMonitor.waInstances[instanceName]?.connectionStatus;
|
return {
|
||||||
|
instance: {
|
||||||
|
instanceName: instanceName,
|
||||||
|
state: this.waMonitor.waInstances[instanceName]?.connectionStatus?.state,
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchInstances({ instanceName }: InstanceDto) {
|
public async fetchInstances({ instanceName }: InstanceDto) {
|
||||||
@@ -389,9 +318,9 @@ export class InstanceController {
|
|||||||
|
|
||||||
public async logout({ instanceName }: InstanceDto) {
|
public async logout({ instanceName }: InstanceDto) {
|
||||||
this.logger.verbose('requested logout from ' + instanceName + ' instance');
|
this.logger.verbose('requested logout from ' + instanceName + ' instance');
|
||||||
const stateConn = await this.connectionState({ instanceName });
|
const { instance } = await this.connectionState({ instanceName });
|
||||||
|
|
||||||
if (stateConn.state === 'close') {
|
if (instance.state === 'close') {
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(
|
||||||
'The "' + instanceName + '" instance is not connected',
|
'The "' + instanceName + '" instance is not connected',
|
||||||
);
|
);
|
||||||
@@ -414,15 +343,15 @@ export class InstanceController {
|
|||||||
|
|
||||||
public async deleteInstance({ instanceName }: InstanceDto) {
|
public async deleteInstance({ instanceName }: InstanceDto) {
|
||||||
this.logger.verbose('requested deleteInstance from ' + instanceName + ' instance');
|
this.logger.verbose('requested deleteInstance from ' + instanceName + ' instance');
|
||||||
const stateConn = await this.connectionState({ instanceName });
|
const { instance } = await this.connectionState({ instanceName });
|
||||||
|
|
||||||
if (stateConn.state === 'open') {
|
if (instance.state === 'open') {
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(
|
||||||
'The "' + instanceName + '" instance needs to be disconnected',
|
'The "' + instanceName + '" instance needs to be disconnected',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (stateConn.state === 'connecting') {
|
if (instance.state === 'connecting') {
|
||||||
this.logger.verbose('logging out instance: ' + instanceName);
|
this.logger.verbose('logging out instance: ' + instanceName);
|
||||||
|
|
||||||
await this.logout({ instanceName });
|
await this.logout({ instanceName });
|
||||||
|
|||||||
25
src/whatsapp/controllers/settings.controller.ts
Normal file
25
src/whatsapp/controllers/settings.controller.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { isURL } from 'class-validator';
|
||||||
|
import { BadRequestException } from '../../exceptions';
|
||||||
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
|
import { SettingsDto } from '../dto/settings.dto';
|
||||||
|
import { SettingsService } from '../services/settings.service';
|
||||||
|
import { Logger } from '../../config/logger.config';
|
||||||
|
|
||||||
|
const logger = new Logger('SettingsController');
|
||||||
|
|
||||||
|
export class SettingsController {
|
||||||
|
constructor(private readonly settingsService: SettingsService) {}
|
||||||
|
|
||||||
|
public async createSettings(instance: InstanceDto, data: SettingsDto) {
|
||||||
|
logger.verbose(
|
||||||
|
'requested createSettings from ' + instance.instanceName + ' instance',
|
||||||
|
);
|
||||||
|
|
||||||
|
return this.settingsService.create(instance, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async findSettings(instance: InstanceDto) {
|
||||||
|
logger.verbose('requested findSettings from ' + instance.instanceName + ' instance');
|
||||||
|
return this.settingsService.find(instance);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,19 @@ export class NumberDto {
|
|||||||
number: string;
|
number: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class NumberBusiness {
|
||||||
|
wid?: string;
|
||||||
|
jid?: string;
|
||||||
|
exists?: boolean;
|
||||||
|
isBusiness: boolean;
|
||||||
|
name?: string;
|
||||||
|
message?: string;
|
||||||
|
description?: string;
|
||||||
|
email?: string;
|
||||||
|
website?: string[];
|
||||||
|
address?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export class ProfileNameDto {
|
export class ProfileNameDto {
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
@@ -46,7 +59,7 @@ class Key {
|
|||||||
remoteJid: string;
|
remoteJid: string;
|
||||||
}
|
}
|
||||||
export class ReadMessageDto {
|
export class ReadMessageDto {
|
||||||
readMessages: Key[];
|
read_messages: Key[];
|
||||||
}
|
}
|
||||||
|
|
||||||
class LastMessage {
|
class LastMessage {
|
||||||
|
|||||||
@@ -5,4 +5,7 @@ export class ChatwootDto {
|
|||||||
url?: string;
|
url?: string;
|
||||||
name_inbox?: string;
|
name_inbox?: string;
|
||||||
sign_msg?: boolean;
|
sign_msg?: boolean;
|
||||||
|
number?: string;
|
||||||
|
reopen_conversation?: boolean;
|
||||||
|
conversation_pending?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
export class CreateGroupDto {
|
export class CreateGroupDto {
|
||||||
subject: string;
|
subject: string;
|
||||||
description?: string;
|
|
||||||
participants: string[];
|
participants: string[];
|
||||||
|
description?: string;
|
||||||
|
promoteParticipants?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GroupPictureDto {
|
export class GroupPictureDto {
|
||||||
|
|||||||
@@ -1,12 +1,21 @@
|
|||||||
export class InstanceDto {
|
export class InstanceDto {
|
||||||
instanceName: string;
|
instanceName: string;
|
||||||
|
qrcode?: boolean;
|
||||||
|
number?: string;
|
||||||
|
token?: string;
|
||||||
webhook?: string;
|
webhook?: string;
|
||||||
webhook_by_events?: boolean;
|
webhook_by_events?: boolean;
|
||||||
events?: string[];
|
events?: string[];
|
||||||
qrcode?: boolean;
|
reject_call?: boolean;
|
||||||
token?: string;
|
msg_call?: string;
|
||||||
|
groups_ignore?: boolean;
|
||||||
|
always_online?: boolean;
|
||||||
|
read_messages?: boolean;
|
||||||
|
read_status?: boolean;
|
||||||
chatwoot_account_id?: string;
|
chatwoot_account_id?: string;
|
||||||
chatwoot_token?: string;
|
chatwoot_token?: string;
|
||||||
chatwoot_url?: string;
|
chatwoot_url?: string;
|
||||||
chatwoot_sign_msg?: boolean;
|
chatwoot_sign_msg?: boolean;
|
||||||
|
chatwoot_reopen_conversation?: boolean;
|
||||||
|
chatwoot_conversation_pending?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ export class Options {
|
|||||||
presence?: WAPresence;
|
presence?: WAPresence;
|
||||||
quoted?: Quoted;
|
quoted?: Quoted;
|
||||||
mentions?: Mentions;
|
mentions?: Mentions;
|
||||||
|
linkPreview?: boolean;
|
||||||
|
encoding?: boolean;
|
||||||
}
|
}
|
||||||
class OptionsMessage {
|
class OptionsMessage {
|
||||||
options: Options;
|
options: Options;
|
||||||
|
|||||||
8
src/whatsapp/dto/settings.dto.ts
Normal file
8
src/whatsapp/dto/settings.dto.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export class SettingsDto {
|
||||||
|
reject_call?: boolean;
|
||||||
|
msg_call?: string;
|
||||||
|
groups_ignore?: boolean;
|
||||||
|
always_online?: boolean;
|
||||||
|
read_messages?: boolean;
|
||||||
|
read_status?: boolean;
|
||||||
|
}
|
||||||
@@ -9,6 +9,9 @@ export class ChatwootRaw {
|
|||||||
url?: string;
|
url?: string;
|
||||||
name_inbox?: string;
|
name_inbox?: string;
|
||||||
sign_msg?: boolean;
|
sign_msg?: boolean;
|
||||||
|
number?: string;
|
||||||
|
reopen_conversation?: boolean;
|
||||||
|
conversation_pending?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chatwootSchema = new Schema<ChatwootRaw>({
|
const chatwootSchema = new Schema<ChatwootRaw>({
|
||||||
@@ -19,6 +22,7 @@ const chatwootSchema = new Schema<ChatwootRaw>({
|
|||||||
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 },
|
||||||
});
|
});
|
||||||
|
|
||||||
export const ChatwootModel = dbserver?.model(
|
export const ChatwootModel = dbserver?.model(
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ export * from './message.model';
|
|||||||
export * from './auth.model';
|
export * from './auth.model';
|
||||||
export * from './webhook.model';
|
export * from './webhook.model';
|
||||||
export * from './chatwoot.model';
|
export * from './chatwoot.model';
|
||||||
|
export * from './settings.model';
|
||||||
|
|||||||
29
src/whatsapp/models/settings.model.ts
Normal file
29
src/whatsapp/models/settings.model.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { Schema } from 'mongoose';
|
||||||
|
import { dbserver } from '../../db/db.connect';
|
||||||
|
|
||||||
|
export class SettingsRaw {
|
||||||
|
_id?: string;
|
||||||
|
reject_call?: boolean;
|
||||||
|
msg_call?: string;
|
||||||
|
groups_ignore?: boolean;
|
||||||
|
always_online?: boolean;
|
||||||
|
read_messages?: boolean;
|
||||||
|
read_status?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const settingsSchema = new Schema<SettingsRaw>({
|
||||||
|
_id: { type: String, _id: true },
|
||||||
|
reject_call: { type: Boolean, required: true },
|
||||||
|
msg_call: { type: String, required: true },
|
||||||
|
groups_ignore: { type: Boolean, required: true },
|
||||||
|
always_online: { type: Boolean, required: true },
|
||||||
|
read_messages: { type: Boolean, required: true },
|
||||||
|
read_status: { type: Boolean, required: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
export const SettingsModel = dbserver?.model(
|
||||||
|
SettingsRaw.name,
|
||||||
|
settingsSchema,
|
||||||
|
'settings',
|
||||||
|
);
|
||||||
|
export type ISettingsModel = typeof SettingsModel;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { Auth, ConfigService } from '../../config/env.config';
|
import { Auth, ConfigService, Database } from '../../config/env.config';
|
||||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||||
import { IAuthModel, AuthRaw } from '../models';
|
import { IAuthModel, AuthRaw } from '../models';
|
||||||
import { readFileSync } from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ import { MessageUpRepository } from './messageUp.repository';
|
|||||||
import { MongoClient } from 'mongodb';
|
import { MongoClient } from 'mongodb';
|
||||||
import { WebhookRepository } from './webhook.repository';
|
import { WebhookRepository } from './webhook.repository';
|
||||||
import { ChatwootRepository } from './chatwoot.repository';
|
import { ChatwootRepository } from './chatwoot.repository';
|
||||||
|
import { SettingsRepository } from './settings.repository';
|
||||||
|
|
||||||
import { AuthRepository } from './auth.repository';
|
import { AuthRepository } from './auth.repository';
|
||||||
import { Auth, ConfigService, Database } from '../../config/env.config';
|
import { Auth, ConfigService, Database } from '../../config/env.config';
|
||||||
import { execSync } from 'child_process';
|
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { Logger } from '../../config/logger.config';
|
import { Logger } from '../../config/logger.config';
|
||||||
@@ -20,6 +20,7 @@ export class RepositoryBroker {
|
|||||||
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 auth: AuthRepository,
|
public readonly auth: AuthRepository,
|
||||||
private configService: ConfigService,
|
private configService: ConfigService,
|
||||||
dbServer?: MongoClient,
|
dbServer?: MongoClient,
|
||||||
@@ -53,8 +54,9 @@ export class RepositoryBroker {
|
|||||||
const messageUpDir = join(storePath, 'message-up');
|
const messageUpDir = join(storePath, 'message-up');
|
||||||
const webhookDir = join(storePath, 'webhook');
|
const webhookDir = join(storePath, 'webhook');
|
||||||
const chatwootDir = join(storePath, 'chatwoot');
|
const chatwootDir = join(storePath, 'chatwoot');
|
||||||
|
const settingsDir = join(storePath, 'settings');
|
||||||
|
const tempDir = join(storePath, 'temp');
|
||||||
|
|
||||||
// Check if directories exist, create them if not
|
|
||||||
if (!fs.existsSync(authDir)) {
|
if (!fs.existsSync(authDir)) {
|
||||||
this.logger.verbose('creating auth dir: ' + authDir);
|
this.logger.verbose('creating auth dir: ' + authDir);
|
||||||
fs.mkdirSync(authDir, { recursive: true });
|
fs.mkdirSync(authDir, { recursive: true });
|
||||||
@@ -83,6 +85,34 @@ export class RepositoryBroker {
|
|||||||
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
|
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
|
||||||
fs.mkdirSync(chatwootDir, { recursive: true });
|
fs.mkdirSync(chatwootDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
if (!fs.existsSync(settingsDir)) {
|
||||||
|
this.logger.verbose('creating settings dir: ' + settingsDir);
|
||||||
|
fs.mkdirSync(settingsDir, { recursive: true });
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(tempDir)) {
|
||||||
|
this.logger.verbose('creating temp dir: ' + tempDir);
|
||||||
|
fs.mkdirSync(tempDir, { recursive: true });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const storePath = join(process.cwd(), 'store');
|
||||||
|
|
||||||
|
this.logger.verbose('creating store path: ' + storePath);
|
||||||
|
|
||||||
|
const tempDir = join(storePath, 'temp');
|
||||||
|
const chatwootDir = join(storePath, 'chatwoot');
|
||||||
|
|
||||||
|
if (!fs.existsSync(chatwootDir)) {
|
||||||
|
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
|
||||||
|
fs.mkdirSync(chatwootDir, { recursive: true });
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(tempDir)) {
|
||||||
|
this.logger.verbose('creating temp dir: ' + tempDir);
|
||||||
|
fs.mkdirSync(tempDir, { recursive: true });
|
||||||
|
}
|
||||||
|
try {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
}
|
}
|
||||||
|
|||||||
75
src/whatsapp/repository/settings.repository.ts
Normal file
75
src/whatsapp/repository/settings.repository.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||||
|
import { ConfigService } from '../../config/env.config';
|
||||||
|
import { join } from 'path';
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { ISettingsModel, SettingsRaw } from '../models';
|
||||||
|
import { Logger } from '../../config/logger.config';
|
||||||
|
|
||||||
|
export class SettingsRepository extends Repository {
|
||||||
|
constructor(
|
||||||
|
private readonly settingsModel: ISettingsModel,
|
||||||
|
private readonly configService: ConfigService,
|
||||||
|
) {
|
||||||
|
super(configService);
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly logger = new Logger('SettingsRepository');
|
||||||
|
|
||||||
|
public async create(data: SettingsRaw, instance: string): Promise<IInsert> {
|
||||||
|
try {
|
||||||
|
this.logger.verbose('creating settings');
|
||||||
|
if (this.dbSettings.ENABLED) {
|
||||||
|
this.logger.verbose('saving settings to db');
|
||||||
|
const insert = await this.settingsModel.replaceOne(
|
||||||
|
{ _id: instance },
|
||||||
|
{ ...data },
|
||||||
|
{ upsert: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
this.logger.verbose(
|
||||||
|
'settings saved to db: ' + insert.modifiedCount + ' settings',
|
||||||
|
);
|
||||||
|
return { insertCount: insert.modifiedCount };
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('saving settings to store');
|
||||||
|
|
||||||
|
this.writeStore<SettingsRaw>({
|
||||||
|
path: join(this.storePath, 'settings'),
|
||||||
|
fileName: instance,
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.logger.verbose(
|
||||||
|
'settings saved to store in path: ' +
|
||||||
|
join(this.storePath, 'settings') +
|
||||||
|
'/' +
|
||||||
|
instance,
|
||||||
|
);
|
||||||
|
|
||||||
|
this.logger.verbose('settings created');
|
||||||
|
return { insertCount: 1 };
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async find(instance: string): Promise<SettingsRaw> {
|
||||||
|
try {
|
||||||
|
this.logger.verbose('finding settings');
|
||||||
|
if (this.dbSettings.ENABLED) {
|
||||||
|
this.logger.verbose('finding settings in db');
|
||||||
|
return await this.settingsModel.findOne({ _id: instance });
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('finding settings in store');
|
||||||
|
return JSON.parse(
|
||||||
|
readFileSync(join(this.storePath, 'settings', instance + '.json'), {
|
||||||
|
encoding: 'utf-8',
|
||||||
|
}),
|
||||||
|
) as SettingsRaw;
|
||||||
|
} catch (error) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
privacySettingsSchema,
|
privacySettingsSchema,
|
||||||
profileNameSchema,
|
profileNameSchema,
|
||||||
profilePictureSchema,
|
profilePictureSchema,
|
||||||
|
profileSchema,
|
||||||
profileStatusSchema,
|
profileStatusSchema,
|
||||||
readMessageSchema,
|
readMessageSchema,
|
||||||
whatsappNumberSchema,
|
whatsappNumberSchema,
|
||||||
@@ -129,6 +130,23 @@ export class ChatRouter extends RouterBroker {
|
|||||||
|
|
||||||
return res.status(HttpStatus.OK).json(response);
|
return res.status(HttpStatus.OK).json(response);
|
||||||
})
|
})
|
||||||
|
.post(this.routerPath('fetchProfile'), ...guards, async (req, res) => {
|
||||||
|
logger.verbose('request received in fetchProfile');
|
||||||
|
logger.verbose('request body: ');
|
||||||
|
logger.verbose(req.body);
|
||||||
|
|
||||||
|
logger.verbose('request query: ');
|
||||||
|
logger.verbose(req.query);
|
||||||
|
|
||||||
|
const response = await this.dataValidate<NumberDto>({
|
||||||
|
request: req,
|
||||||
|
schema: profileSchema,
|
||||||
|
ClassRef: NumberDto,
|
||||||
|
execute: (instance, data) => chatController.fetchProfile(instance, data),
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(HttpStatus.OK).json(response);
|
||||||
|
})
|
||||||
.post(this.routerPath('findContacts'), ...guards, async (req, res) => {
|
.post(this.routerPath('findContacts'), ...guards, async (req, res) => {
|
||||||
logger.verbose('request received in findContacts');
|
logger.verbose('request received in findContacts');
|
||||||
logger.verbose('request body: ');
|
logger.verbose('request body: ');
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { ViewsRouter } from './view.router';
|
|||||||
import { WebhookRouter } from './webhook.router';
|
import { WebhookRouter } from './webhook.router';
|
||||||
import { ChatwootRouter } from './chatwoot.router';
|
import { ChatwootRouter } from './chatwoot.router';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import { SettingsRouter } from './settings.router';
|
||||||
|
|
||||||
enum HttpStatus {
|
enum HttpStatus {
|
||||||
OK = 200,
|
OK = 200,
|
||||||
@@ -44,6 +45,7 @@ router
|
|||||||
.use('/chat', new ChatRouter(...guards).router)
|
.use('/chat', new ChatRouter(...guards).router)
|
||||||
.use('/group', new GroupRouter(...guards).router)
|
.use('/group', new GroupRouter(...guards).router)
|
||||||
.use('/webhook', new WebhookRouter(...guards).router)
|
.use('/webhook', new WebhookRouter(...guards).router)
|
||||||
.use('/chatwoot', new ChatwootRouter(...guards).router);
|
.use('/chatwoot', new ChatwootRouter(...guards).router)
|
||||||
|
.use('/settings', new SettingsRouter(...guards).router);
|
||||||
|
|
||||||
export { router, HttpStatus };
|
export { router, HttpStatus };
|
||||||
|
|||||||
52
src/whatsapp/routers/settings.router.ts
Normal file
52
src/whatsapp/routers/settings.router.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import { RequestHandler, Router } from 'express';
|
||||||
|
import { instanceNameSchema, settingsSchema } from '../../validate/validate.schema';
|
||||||
|
import { RouterBroker } from '../abstract/abstract.router';
|
||||||
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
|
import { SettingsDto } from '../dto/settings.dto';
|
||||||
|
import { settingsController } from '../whatsapp.module';
|
||||||
|
import { SettingsService } from '../services/settings.service';
|
||||||
|
import { HttpStatus } from './index.router';
|
||||||
|
import { Logger } from '../../config/logger.config';
|
||||||
|
|
||||||
|
const logger = new Logger('SettingsRouter');
|
||||||
|
|
||||||
|
export class SettingsRouter extends RouterBroker {
|
||||||
|
constructor(...guards: RequestHandler[]) {
|
||||||
|
super();
|
||||||
|
this.router
|
||||||
|
.post(this.routerPath('set'), ...guards, async (req, res) => {
|
||||||
|
logger.verbose('request received in setSettings');
|
||||||
|
logger.verbose('request body: ');
|
||||||
|
logger.verbose(req.body);
|
||||||
|
|
||||||
|
logger.verbose('request query: ');
|
||||||
|
logger.verbose(req.query);
|
||||||
|
const response = await this.dataValidate<SettingsDto>({
|
||||||
|
request: req,
|
||||||
|
schema: settingsSchema,
|
||||||
|
ClassRef: SettingsDto,
|
||||||
|
execute: (instance, data) => settingsController.createSettings(instance, data),
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(HttpStatus.CREATED).json(response);
|
||||||
|
})
|
||||||
|
.get(this.routerPath('find'), ...guards, async (req, res) => {
|
||||||
|
logger.verbose('request received in findSettings');
|
||||||
|
logger.verbose('request body: ');
|
||||||
|
logger.verbose(req.body);
|
||||||
|
|
||||||
|
logger.verbose('request query: ');
|
||||||
|
logger.verbose(req.query);
|
||||||
|
const response = await this.dataValidate<InstanceDto>({
|
||||||
|
request: req,
|
||||||
|
schema: instanceNameSchema,
|
||||||
|
ClassRef: InstanceDto,
|
||||||
|
execute: (instance) => settingsController.findSettings(instance),
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(HttpStatus.OK).json(response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly router = Router();
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ import { SendAudioDto } from '../dto/sendMessage.dto';
|
|||||||
import { SendMediaDto } from '../dto/sendMessage.dto';
|
import { SendMediaDto } from '../dto/sendMessage.dto';
|
||||||
import { ROOT_DIR } from '../../config/path.config';
|
import { ROOT_DIR } from '../../config/path.config';
|
||||||
import { ConfigService, HttpServer } from '../../config/env.config';
|
import { ConfigService, HttpServer } from '../../config/env.config';
|
||||||
|
import { type } from 'os';
|
||||||
|
|
||||||
export class ChatwootService {
|
export class ChatwootService {
|
||||||
private messageCacheFile: string;
|
private messageCacheFile: string;
|
||||||
@@ -154,6 +155,7 @@ export class ChatwootService {
|
|||||||
inboxName: string,
|
inboxName: string,
|
||||||
webhookUrl: string,
|
webhookUrl: string,
|
||||||
qrcode: boolean,
|
qrcode: boolean,
|
||||||
|
number: string,
|
||||||
) {
|
) {
|
||||||
this.logger.verbose('init instance chatwoot: ' + instance.instanceName);
|
this.logger.verbose('init instance chatwoot: ' + instance.instanceName);
|
||||||
|
|
||||||
@@ -229,12 +231,18 @@ export class ChatwootService {
|
|||||||
|
|
||||||
if (qrcode) {
|
if (qrcode) {
|
||||||
this.logger.verbose('create conversation in chatwoot');
|
this.logger.verbose('create conversation in chatwoot');
|
||||||
const conversation = await client.conversations.create({
|
const data = {
|
||||||
accountId: this.provider.account_id,
|
|
||||||
data: {
|
|
||||||
contact_id: contactId.toString(),
|
contact_id: contactId.toString(),
|
||||||
inbox_id: inboxId.toString(),
|
inbox_id: inboxId.toString(),
|
||||||
},
|
};
|
||||||
|
|
||||||
|
if (this.provider.conversation_pending) {
|
||||||
|
data['status'] = 'pending';
|
||||||
|
}
|
||||||
|
|
||||||
|
const conversation = await client.conversations.create({
|
||||||
|
accountId: this.provider.account_id,
|
||||||
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!conversation) {
|
if (!conversation) {
|
||||||
@@ -243,11 +251,18 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('create message for init instance in chatwoot');
|
this.logger.verbose('create message for init instance in chatwoot');
|
||||||
|
|
||||||
|
let contentMsg = '/init';
|
||||||
|
|
||||||
|
if (number) {
|
||||||
|
contentMsg = `/init:${number}`;
|
||||||
|
}
|
||||||
|
|
||||||
const message = await client.messages.create({
|
const message = await client.messages.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.account_id,
|
||||||
conversationId: conversation.id,
|
conversationId: conversation.id,
|
||||||
data: {
|
data: {
|
||||||
content: '/init',
|
content: contentMsg,
|
||||||
message_type: 'outgoing',
|
message_type: 'outgoing',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -428,10 +443,12 @@ export class ChatwootService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (findParticipant) {
|
if (findParticipant) {
|
||||||
|
if (!findParticipant.name || findParticipant.name === chatId) {
|
||||||
await this.updateContact(instance, findParticipant.id, {
|
await this.updateContact(instance, findParticipant.id, {
|
||||||
name: body.pushName,
|
name: body.pushName,
|
||||||
avatar_url: picture_url.profilePictureUrl || null,
|
avatar_url: picture_url.profilePictureUrl || null,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
await this.createContact(
|
await this.createContact(
|
||||||
instance,
|
instance,
|
||||||
@@ -454,13 +471,28 @@ export class ChatwootService {
|
|||||||
|
|
||||||
let contact: any;
|
let contact: any;
|
||||||
if (body.key.fromMe) {
|
if (body.key.fromMe) {
|
||||||
|
if (findContact) {
|
||||||
contact = findContact;
|
contact = findContact;
|
||||||
|
} else {
|
||||||
|
contact = await this.createContact(
|
||||||
|
instance,
|
||||||
|
chatId,
|
||||||
|
filterInbox.id,
|
||||||
|
isGroup,
|
||||||
|
nameContact,
|
||||||
|
picture_url.profilePictureUrl || null,
|
||||||
|
);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (findContact) {
|
if (findContact) {
|
||||||
|
if (!findContact.name || findContact.name === chatId) {
|
||||||
contact = await this.updateContact(instance, findContact.id, {
|
contact = await this.updateContact(instance, findContact.id, {
|
||||||
name: nameContact,
|
name: nameContact,
|
||||||
avatar_url: picture_url.profilePictureUrl || null,
|
avatar_url: picture_url.profilePictureUrl || null,
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
contact = findContact;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
contact = await this.createContact(
|
contact = await this.createContact(
|
||||||
instance,
|
instance,
|
||||||
@@ -495,11 +527,20 @@ export class ChatwootService {
|
|||||||
})) as any;
|
})) as any;
|
||||||
|
|
||||||
if (contactConversations) {
|
if (contactConversations) {
|
||||||
this.logger.verbose('return conversation if exists');
|
let conversation: any;
|
||||||
const conversation = contactConversations.payload.find(
|
if (this.provider.reopen_conversation) {
|
||||||
(conversation) =>
|
conversation = contactConversations.payload.find(
|
||||||
conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id,
|
(conversation) => conversation.inbox_id == filterInbox.id,
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
conversation = contactConversations.payload.find(
|
||||||
|
(conversation) =>
|
||||||
|
conversation.status !== 'resolved' &&
|
||||||
|
conversation.inbox_id == filterInbox.id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this.logger.verbose('return conversation if exists');
|
||||||
|
|
||||||
if (conversation) {
|
if (conversation) {
|
||||||
this.logger.verbose('conversation found');
|
this.logger.verbose('conversation found');
|
||||||
return conversation.id;
|
return conversation.id;
|
||||||
@@ -507,12 +548,18 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('create conversation in chatwoot');
|
this.logger.verbose('create conversation in chatwoot');
|
||||||
|
const data = {
|
||||||
|
contact_id: contactId.toString(),
|
||||||
|
inbox_id: filterInbox.id.toString(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.provider.conversation_pending) {
|
||||||
|
data['status'] = 'pending';
|
||||||
|
}
|
||||||
|
|
||||||
const conversation = await client.conversations.create({
|
const conversation = await client.conversations.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.account_id,
|
||||||
data: {
|
data,
|
||||||
contact_id: `${contactId}`,
|
|
||||||
inbox_id: `${filterInbox.id}`,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!conversation) {
|
if (!conversation) {
|
||||||
@@ -936,13 +983,14 @@ export class ChatwootService {
|
|||||||
|
|
||||||
const command = messageReceived.replace('/', '');
|
const command = messageReceived.replace('/', '');
|
||||||
|
|
||||||
if (command === 'init' || command === 'iniciar') {
|
if (command.includes('init') || command.includes('iniciar')) {
|
||||||
this.logger.verbose('command init found');
|
this.logger.verbose('command init found');
|
||||||
const state = waInstance?.connectionStatus?.state;
|
const state = waInstance?.connectionStatus?.state;
|
||||||
|
|
||||||
if (state !== 'open') {
|
if (state !== 'open') {
|
||||||
this.logger.verbose('connect to whatsapp');
|
this.logger.verbose('connect to whatsapp');
|
||||||
await waInstance.connectToWhatsapp();
|
const number = command.split(':')[1];
|
||||||
|
await waInstance.connectToWhatsapp(number);
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('whatsapp already connected');
|
this.logger.verbose('whatsapp already connected');
|
||||||
await this.createBotMessage(
|
await this.createBotMessage(
|
||||||
@@ -990,7 +1038,7 @@ export class ChatwootService {
|
|||||||
await waInstance?.client?.ws?.close();
|
await waInstance?.client?.ws?.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command.includes('#inbox_whatsapp')) {
|
if (command.includes('new_instance')) {
|
||||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
|
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
|
||||||
|
|
||||||
@@ -1003,6 +1051,10 @@ export class ChatwootService {
|
|||||||
chatwoot_sign_msg: this.provider.sign_msg,
|
chatwoot_sign_msg: this.provider.sign_msg,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (command.split(':')[2]) {
|
||||||
|
data['number'] = command.split(':')[2];
|
||||||
|
}
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
maxBodyLength: Infinity,
|
maxBodyLength: Infinity,
|
||||||
@@ -1094,17 +1146,13 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (body.message_type === 'template' && body.event === 'message_created') {
|
||||||
body.message_type === 'template' &&
|
this.logger.verbose('check if is template');
|
||||||
body.content_type === 'input_csat' &&
|
|
||||||
body.event === 'message_created'
|
|
||||||
) {
|
|
||||||
this.logger.verbose('check if is csat');
|
|
||||||
|
|
||||||
const data: SendTextDto = {
|
const data: SendTextDto = {
|
||||||
number: chatId,
|
number: chatId,
|
||||||
textMessage: {
|
textMessage: {
|
||||||
text: body.content,
|
text: body.content.replace(/\\\r\n|\\\n|\n/g, '\n'),
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
delay: 1200,
|
delay: 1200,
|
||||||
@@ -1153,13 +1201,15 @@ export class ChatwootService {
|
|||||||
videoMessage: msg.videoMessage?.caption,
|
videoMessage: msg.videoMessage?.caption,
|
||||||
extendedTextMessage: msg.extendedTextMessage?.text,
|
extendedTextMessage: msg.extendedTextMessage?.text,
|
||||||
messageContextInfo: msg.messageContextInfo?.stanzaId,
|
messageContextInfo: msg.messageContextInfo?.stanzaId,
|
||||||
stickerMessage: msg.stickerMessage?.fileSha256.toString('base64'),
|
stickerMessage: undefined,
|
||||||
documentMessage: msg.documentMessage?.caption,
|
documentMessage: msg.documentMessage?.caption,
|
||||||
documentWithCaptionMessage:
|
documentWithCaptionMessage:
|
||||||
msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
|
msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
|
||||||
audioMessage: msg.audioMessage?.caption,
|
audioMessage: msg.audioMessage?.caption,
|
||||||
contactMessage: msg.contactMessage?.vcard,
|
contactMessage: msg.contactMessage?.vcard,
|
||||||
contactsArrayMessage: msg.contactsArrayMessage,
|
contactsArrayMessage: msg.contactsArrayMessage,
|
||||||
|
locationMessage: msg.locationMessage,
|
||||||
|
liveLocationMessage: msg.liveLocationMessage,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.logger.verbose('type message: ' + types);
|
this.logger.verbose('type message: ' + types);
|
||||||
@@ -1173,6 +1223,21 @@ export class ChatwootService {
|
|||||||
|
|
||||||
const result = typeKey ? types[typeKey] : undefined;
|
const result = typeKey ? types[typeKey] : undefined;
|
||||||
|
|
||||||
|
if (typeKey === 'locationMessage' || typeKey === 'liveLocationMessage') {
|
||||||
|
const latitude = result.degreesLatitude;
|
||||||
|
const longitude = result.degreesLongitude;
|
||||||
|
|
||||||
|
const formattedLocation = `**Location:**
|
||||||
|
**latitude:** ${latitude}
|
||||||
|
**longitude:** ${longitude}
|
||||||
|
https://www.google.com/maps/search/?api=1&query=${latitude},${longitude}
|
||||||
|
`;
|
||||||
|
|
||||||
|
this.logger.verbose('message content: ' + formattedLocation);
|
||||||
|
|
||||||
|
return formattedLocation;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeKey === 'contactMessage') {
|
if (typeKey === 'contactMessage') {
|
||||||
const vCardData = result.split('\n');
|
const vCardData = result.split('\n');
|
||||||
const contactInfo = {};
|
const contactInfo = {};
|
||||||
@@ -1276,6 +1341,16 @@ export class ChatwootService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('get conversation message');
|
||||||
|
const bodyMessage = await this.getConversationMessage(body.message);
|
||||||
|
|
||||||
|
const isMedia = this.isMediaMessage(body.message);
|
||||||
|
|
||||||
|
if (!bodyMessage && !isMedia) {
|
||||||
|
this.logger.warn('no body message found');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.logger.verbose('get conversation in chatwoot');
|
this.logger.verbose('get conversation in chatwoot');
|
||||||
const getConversion = await this.createConversation(instance, body);
|
const getConversion = await this.createConversation(instance, body);
|
||||||
|
|
||||||
@@ -1288,18 +1363,8 @@ export class ChatwootService {
|
|||||||
|
|
||||||
this.logger.verbose('message type: ' + messageType);
|
this.logger.verbose('message type: ' + messageType);
|
||||||
|
|
||||||
const isMedia = this.isMediaMessage(body.message);
|
|
||||||
|
|
||||||
this.logger.verbose('is media: ' + isMedia);
|
this.logger.verbose('is media: ' + isMedia);
|
||||||
|
|
||||||
this.logger.verbose('get conversation message');
|
|
||||||
const bodyMessage = await this.getConversationMessage(body.message);
|
|
||||||
|
|
||||||
if (!bodyMessage && !isMedia) {
|
|
||||||
this.logger.warn('no body message found');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.verbose('check if is media');
|
this.logger.verbose('check if is media');
|
||||||
if (isMedia) {
|
if (isMedia) {
|
||||||
this.logger.verbose('message is media');
|
this.logger.verbose('message is media');
|
||||||
@@ -1539,7 +1604,16 @@ export class ChatwootService {
|
|||||||
fileName,
|
fileName,
|
||||||
);
|
);
|
||||||
|
|
||||||
const msgQrCode = `⚡️ QRCode successfully generated!\n\nScan this QR code within the next 40 seconds:`;
|
let msgQrCode = `⚡️ QRCode successfully generated!\n\nScan this QR code within the next 40 seconds.`;
|
||||||
|
|
||||||
|
if (body?.qrcode?.pairingCode) {
|
||||||
|
msgQrCode =
|
||||||
|
msgQrCode +
|
||||||
|
`\n\n*Pairing Code:* ${body.qrcode.pairingCode.substring(
|
||||||
|
0,
|
||||||
|
4,
|
||||||
|
)}-${body.qrcode.pairingCode.substring(4, 8)}`;
|
||||||
|
}
|
||||||
|
|
||||||
this.logger.verbose('send message to chatwoot');
|
this.logger.verbose('send message to chatwoot');
|
||||||
await this.createBotMessage(instance, msgQrCode, 'incoming');
|
await this.createBotMessage(instance, msgQrCode, 'incoming');
|
||||||
@@ -1549,4 +1623,49 @@ export class ChatwootService {
|
|||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async newInstance(data: any) {
|
||||||
|
try {
|
||||||
|
const instanceName = data.instanceName;
|
||||||
|
const qrcode = true;
|
||||||
|
const number = data.number;
|
||||||
|
const accountId = data.accountId;
|
||||||
|
const chatwootToken = data.token;
|
||||||
|
const chatwootUrl = data.url;
|
||||||
|
const signMsg = true;
|
||||||
|
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
|
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
|
||||||
|
|
||||||
|
const requestData = {
|
||||||
|
instanceName,
|
||||||
|
qrcode,
|
||||||
|
chatwoot_account_id: accountId,
|
||||||
|
chatwoot_token: chatwootToken,
|
||||||
|
chatwoot_url: chatwootUrl,
|
||||||
|
chatwoot_sign_msg: signMsg,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (number) {
|
||||||
|
requestData['number'] = number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
method: 'post',
|
||||||
|
maxBodyLength: Infinity,
|
||||||
|
url: `${urlServer}/instance/create`,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
apikey: apiKey,
|
||||||
|
},
|
||||||
|
data: requestData,
|
||||||
|
};
|
||||||
|
|
||||||
|
// await axios.request(config);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,19 @@ 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 { initInstance } from '../whatsapp.module';
|
|
||||||
import { RedisCache } from '../../db/redis.client';
|
import { RedisCache } from '../../db/redis.client';
|
||||||
import { execSync } from 'child_process';
|
import { execSync } from 'child_process';
|
||||||
|
import { dbserver } from '../../db/db.connect';
|
||||||
|
import mongoose from 'mongoose';
|
||||||
|
import {
|
||||||
|
AuthModel,
|
||||||
|
ChatwootModel,
|
||||||
|
ContactModel,
|
||||||
|
MessageModel,
|
||||||
|
MessageUpModel,
|
||||||
|
SettingsModel,
|
||||||
|
WebhookModel,
|
||||||
|
} from '../models';
|
||||||
|
|
||||||
export class WAMonitoringService {
|
export class WAMonitoringService {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -45,6 +55,8 @@ export class WAMonitoringService {
|
|||||||
|
|
||||||
private dbInstance: Db;
|
private dbInstance: Db;
|
||||||
|
|
||||||
|
private dbStore = dbserver;
|
||||||
|
|
||||||
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> = {};
|
||||||
|
|
||||||
@@ -90,7 +102,7 @@ export class WAMonitoringService {
|
|||||||
|
|
||||||
const findChatwoot = await this.waInstances[key].findChatwoot();
|
const findChatwoot = await this.waInstances[key].findChatwoot();
|
||||||
|
|
||||||
if (findChatwoot.enabled) {
|
if (findChatwoot && findChatwoot.enabled) {
|
||||||
chatwoot = {
|
chatwoot = {
|
||||||
...findChatwoot,
|
...findChatwoot,
|
||||||
webhook_url: `${urlServer}/chatwoot/webhook/${key}`,
|
webhook_url: `${urlServer}/chatwoot/webhook/${key}`,
|
||||||
@@ -218,11 +230,8 @@ export class WAMonitoringService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async cleaningStoreFiles(instanceName: string) {
|
public async cleaningStoreFiles(instanceName: string) {
|
||||||
this.logger.verbose('cleaning store files instance: ' + instanceName);
|
|
||||||
|
|
||||||
if (!this.db.ENABLED) {
|
if (!this.db.ENABLED) {
|
||||||
const instance = this.waInstances[instanceName];
|
this.logger.verbose('cleaning store files instance: ' + instanceName);
|
||||||
|
|
||||||
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
|
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
|
||||||
|
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'chats', instanceName)}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'chats', instanceName)}`);
|
||||||
@@ -233,7 +242,23 @@ export class WAMonitoringService {
|
|||||||
execSync(`rm -rf ${join(STORE_DIR, 'auth', 'apikey', instanceName + '.json')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'auth', 'apikey', instanceName + '.json')}`);
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'webhook', instanceName + '.json')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'webhook', instanceName + '.json')}`);
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'chatwoot', instanceName + '*')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'chatwoot', instanceName + '*')}`);
|
||||||
|
execSync(`rm -rf ${join(STORE_DIR, 'settings', instanceName + '*')}`);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('cleaning store database instance: ' + instanceName);
|
||||||
|
|
||||||
|
await AuthModel.deleteMany({ owner: instanceName });
|
||||||
|
await ContactModel.deleteMany({ owner: instanceName });
|
||||||
|
await MessageModel.deleteMany({ owner: instanceName });
|
||||||
|
await MessageUpModel.deleteMany({ owner: instanceName });
|
||||||
|
await AuthModel.deleteMany({ _id: instanceName });
|
||||||
|
await WebhookModel.deleteMany({ _id: instanceName });
|
||||||
|
await ChatwootModel.deleteMany({ _id: instanceName });
|
||||||
|
await SettingsModel.deleteMany({ _id: instanceName });
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async loadInstance() {
|
public async loadInstance() {
|
||||||
@@ -264,7 +289,6 @@ export class WAMonitoringService {
|
|||||||
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');
|
this.logger.verbose('no instance keys found');
|
||||||
initInstance();
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -280,7 +304,6 @@ export class WAMonitoringService {
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('no collections found');
|
this.logger.verbose('no collections found');
|
||||||
initInstance();
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -301,7 +324,6 @@ export class WAMonitoringService {
|
|||||||
await set(dirent.name);
|
await set(dirent.name);
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('no instance files found');
|
this.logger.verbose('no instance files found');
|
||||||
initInstance();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
34
src/whatsapp/services/settings.service.ts
Normal file
34
src/whatsapp/services/settings.service.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
|
import { SettingsDto } from '../dto/settings.dto';
|
||||||
|
import { WAMonitoringService } from './monitor.service';
|
||||||
|
import { Logger } from '../../config/logger.config';
|
||||||
|
|
||||||
|
export class SettingsService {
|
||||||
|
constructor(private readonly waMonitor: WAMonitoringService) {}
|
||||||
|
|
||||||
|
private readonly logger = new Logger(SettingsService.name);
|
||||||
|
|
||||||
|
public create(instance: InstanceDto, data: SettingsDto) {
|
||||||
|
this.logger.verbose('create settings: ' + instance.instanceName);
|
||||||
|
this.waMonitor.waInstances[instance.instanceName].setSettings(data);
|
||||||
|
|
||||||
|
return { settings: { ...instance, settings: data } };
|
||||||
|
}
|
||||||
|
|
||||||
|
public async find(instance: InstanceDto): Promise<SettingsDto> {
|
||||||
|
try {
|
||||||
|
this.logger.verbose('find settings: ' + instance.instanceName);
|
||||||
|
const result = await this.waMonitor.waInstances[
|
||||||
|
instance.instanceName
|
||||||
|
].findSettings();
|
||||||
|
|
||||||
|
if (Object.keys(result).length === 0) {
|
||||||
|
throw new Error('Settings not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
return { reject_call: false, msg_call: '', groups_ignore: false };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -84,6 +84,7 @@ import { arrayUnique, isBase64, isURL } from 'class-validator';
|
|||||||
import {
|
import {
|
||||||
ArchiveChatDto,
|
ArchiveChatDto,
|
||||||
DeleteMessage,
|
DeleteMessage,
|
||||||
|
NumberBusiness,
|
||||||
OnWhatsAppDto,
|
OnWhatsAppDto,
|
||||||
PrivacySettingDto,
|
PrivacySettingDto,
|
||||||
ReadMessageDto,
|
ReadMessageDto,
|
||||||
@@ -115,13 +116,13 @@ import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-d
|
|||||||
import Long from 'long';
|
import Long from 'long';
|
||||||
import { WebhookRaw } from '../models/webhook.model';
|
import { WebhookRaw } from '../models/webhook.model';
|
||||||
import { ChatwootRaw } from '../models/chatwoot.model';
|
import { ChatwootRaw } from '../models/chatwoot.model';
|
||||||
|
import { SettingsRaw } from '../models';
|
||||||
import { dbserver } from '../../db/db.connect';
|
import { dbserver } from '../../db/db.connect';
|
||||||
import NodeCache from 'node-cache';
|
import NodeCache from 'node-cache';
|
||||||
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
|
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
|
||||||
import sharp from 'sharp';
|
import sharp from 'sharp';
|
||||||
import { RedisCache } from '../../db/redis.client';
|
import { RedisCache } from '../../db/redis.client';
|
||||||
import { Log } from '../../config/env.config';
|
import { Log } from '../../config/env.config';
|
||||||
import ProxyAgent from 'proxy-agent';
|
|
||||||
import { ChatwootService } from './chatwoot.service';
|
import { ChatwootService } from './chatwoot.service';
|
||||||
import { waMonitor } from '../whatsapp.module';
|
import { waMonitor } from '../whatsapp.module';
|
||||||
|
|
||||||
@@ -142,6 +143,7 @@ export class WAStartupService {
|
|||||||
public client: WASocket;
|
public client: WASocket;
|
||||||
private readonly localWebhook: wa.LocalWebHook = {};
|
private readonly localWebhook: wa.LocalWebHook = {};
|
||||||
private readonly localChatwoot: wa.LocalChatwoot = {};
|
private readonly localChatwoot: wa.LocalChatwoot = {};
|
||||||
|
private readonly localSettings: wa.LocalSettings = {};
|
||||||
private stateConnection: wa.StateConnection = { state: 'close' };
|
private stateConnection: wa.StateConnection = { state: 'close' };
|
||||||
public readonly storePath = join(ROOT_DIR, 'store');
|
public readonly storePath = join(ROOT_DIR, 'store');
|
||||||
private readonly msgRetryCounterCache: CacheStore = new NodeCache();
|
private readonly msgRetryCounterCache: CacheStore = new NodeCache();
|
||||||
@@ -149,6 +151,8 @@ export class WAStartupService {
|
|||||||
private endSession = false;
|
private endSession = false;
|
||||||
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
|
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
|
||||||
|
|
||||||
|
private phoneNumber: string;
|
||||||
|
|
||||||
private chatwootService = new ChatwootService(waMonitor, this.configService);
|
private chatwootService = new ChatwootService(waMonitor, this.configService);
|
||||||
|
|
||||||
public set instanceName(name: string) {
|
public set instanceName(name: string) {
|
||||||
@@ -238,7 +242,9 @@ export class WAStartupService {
|
|||||||
|
|
||||||
public get qrCode(): wa.QrCode {
|
public get qrCode(): wa.QrCode {
|
||||||
this.logger.verbose('Getting qrcode');
|
this.logger.verbose('Getting qrcode');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
pairingCode: this.instance.qrcode?.pairingCode,
|
||||||
code: this.instance.qrcode?.code,
|
code: this.instance.qrcode?.code,
|
||||||
base64: this.instance.qrcode?.base64,
|
base64: this.instance.qrcode?.base64,
|
||||||
};
|
};
|
||||||
@@ -306,6 +312,19 @@ export class WAStartupService {
|
|||||||
this.localChatwoot.sign_msg = data?.sign_msg;
|
this.localChatwoot.sign_msg = data?.sign_msg;
|
||||||
this.logger.verbose(`Chatwoot sign msg: ${this.localChatwoot.sign_msg}`);
|
this.logger.verbose(`Chatwoot sign msg: ${this.localChatwoot.sign_msg}`);
|
||||||
|
|
||||||
|
this.localChatwoot.number = data?.number;
|
||||||
|
this.logger.verbose(`Chatwoot number: ${this.localChatwoot.number}`);
|
||||||
|
|
||||||
|
this.localChatwoot.reopen_conversation = data?.reopen_conversation;
|
||||||
|
this.logger.verbose(
|
||||||
|
`Chatwoot reopen conversation: ${this.localChatwoot.reopen_conversation}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
this.localChatwoot.conversation_pending = data?.conversation_pending;
|
||||||
|
this.logger.verbose(
|
||||||
|
`Chatwoot conversation pending: ${this.localChatwoot.conversation_pending}`,
|
||||||
|
);
|
||||||
|
|
||||||
this.logger.verbose('Chatwoot loaded');
|
this.logger.verbose('Chatwoot loaded');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,6 +336,8 @@ export class WAStartupService {
|
|||||||
this.logger.verbose(`Chatwoot url: ${data.url}`);
|
this.logger.verbose(`Chatwoot url: ${data.url}`);
|
||||||
this.logger.verbose(`Chatwoot inbox name: ${data.name_inbox}`);
|
this.logger.verbose(`Chatwoot inbox name: ${data.name_inbox}`);
|
||||||
this.logger.verbose(`Chatwoot sign msg: ${data.sign_msg}`);
|
this.logger.verbose(`Chatwoot sign msg: ${data.sign_msg}`);
|
||||||
|
this.logger.verbose(`Chatwoot reopen conversation: ${data.reopen_conversation}`);
|
||||||
|
this.logger.verbose(`Chatwoot conversation pending: ${data.conversation_pending}`);
|
||||||
|
|
||||||
Object.assign(this.localChatwoot, data);
|
Object.assign(this.localChatwoot, data);
|
||||||
this.logger.verbose('Chatwoot set');
|
this.logger.verbose('Chatwoot set');
|
||||||
@@ -328,7 +349,7 @@ export class WAStartupService {
|
|||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
this.logger.verbose('Chatwoot not found');
|
this.logger.verbose('Chatwoot not found');
|
||||||
throw new NotFoundException('Chatwoot not found');
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose(`Chatwoot account id: ${data.account_id}`);
|
this.logger.verbose(`Chatwoot account id: ${data.account_id}`);
|
||||||
@@ -336,26 +357,84 @@ export class WAStartupService {
|
|||||||
this.logger.verbose(`Chatwoot url: ${data.url}`);
|
this.logger.verbose(`Chatwoot url: ${data.url}`);
|
||||||
this.logger.verbose(`Chatwoot inbox name: ${data.name_inbox}`);
|
this.logger.verbose(`Chatwoot inbox name: ${data.name_inbox}`);
|
||||||
this.logger.verbose(`Chatwoot sign msg: ${data.sign_msg}`);
|
this.logger.verbose(`Chatwoot sign msg: ${data.sign_msg}`);
|
||||||
|
this.logger.verbose(`Chatwoot reopen conversation: ${data.reopen_conversation}`);
|
||||||
|
this.logger.verbose(`Chatwoot conversation pending: ${data.conversation_pending}`);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async loadSettings() {
|
||||||
|
this.logger.verbose('Loading settings');
|
||||||
|
const data = await this.repository.settings.find(this.instanceName);
|
||||||
|
this.localSettings.reject_call = data?.reject_call;
|
||||||
|
this.logger.verbose(`Settings reject_call: ${this.localSettings.reject_call}`);
|
||||||
|
|
||||||
|
this.localSettings.msg_call = data?.msg_call;
|
||||||
|
this.logger.verbose(`Settings msg_call: ${this.localSettings.msg_call}`);
|
||||||
|
|
||||||
|
this.localSettings.groups_ignore = data?.groups_ignore;
|
||||||
|
this.logger.verbose(`Settings groups_ignore: ${this.localSettings.groups_ignore}`);
|
||||||
|
|
||||||
|
this.localSettings.always_online = data?.always_online;
|
||||||
|
this.logger.verbose(`Settings always_online: ${this.localSettings.always_online}`);
|
||||||
|
|
||||||
|
this.localSettings.read_messages = data?.read_messages;
|
||||||
|
this.logger.verbose(`Settings read_messages: ${this.localSettings.read_messages}`);
|
||||||
|
|
||||||
|
this.localSettings.read_status = data?.read_status;
|
||||||
|
this.logger.verbose(`Settings read_status: ${this.localSettings.read_status}`);
|
||||||
|
|
||||||
|
this.logger.verbose('Settings loaded');
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setSettings(data: SettingsRaw) {
|
||||||
|
this.logger.verbose('Setting settings');
|
||||||
|
await this.repository.settings.create(data, this.instanceName);
|
||||||
|
this.logger.verbose(`Settings reject_call: ${data.reject_call}`);
|
||||||
|
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
|
||||||
|
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
|
||||||
|
this.logger.verbose(`Settings always_online: ${data.always_online}`);
|
||||||
|
this.logger.verbose(`Settings read_messages: ${data.read_messages}`);
|
||||||
|
this.logger.verbose(`Settings read_status: ${data.read_status}`);
|
||||||
|
Object.assign(this.localSettings, data);
|
||||||
|
this.logger.verbose('Settings set');
|
||||||
|
|
||||||
|
this.client?.ws?.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async findSettings() {
|
||||||
|
this.logger.verbose('Finding settings');
|
||||||
|
const data = await this.repository.settings.find(this.instanceName);
|
||||||
|
|
||||||
|
if (!data) {
|
||||||
|
this.logger.verbose('Settings not found');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.verbose(`Settings url: ${data.reject_call}`);
|
||||||
|
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
|
||||||
|
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
|
||||||
|
this.logger.verbose(`Settings always_online: ${data.always_online}`);
|
||||||
|
this.logger.verbose(`Settings read_messages: ${data.read_messages}`);
|
||||||
|
this.logger.verbose(`Settings read_status: ${data.read_status}`);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
|
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
|
||||||
const webhookGlobal = this.configService.get<Webhook>('WEBHOOK');
|
const webhookGlobal = this.configService.get<Webhook>('WEBHOOK');
|
||||||
const webhookLocal = this.localWebhook.events;
|
const webhookLocal = this.localWebhook.events;
|
||||||
const serverUrl = this.configService.get<HttpServer>('SERVER').URL;
|
const serverUrl = this.configService.get<HttpServer>('SERVER').URL;
|
||||||
const we = event.replace(/[\.-]/gm, '_').toUpperCase();
|
const we = event.replace(/[\.-]/gm, '_').toUpperCase();
|
||||||
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
|
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
|
||||||
const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE;
|
|
||||||
|
|
||||||
const expose =
|
const expose =
|
||||||
this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES;
|
this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES;
|
||||||
const tokenStore = await this.repository.auth.find(this.instanceName);
|
const tokenStore = await this.repository.auth.find(this.instanceName);
|
||||||
const instanceApikey = tokenStore.apikey || 'Apikey not found';
|
const instanceApikey = tokenStore?.apikey || 'Apikey not found';
|
||||||
|
|
||||||
const globalApiKey = this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
|
const globalApiKey = this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
|
||||||
|
|
||||||
if (local && instance.MODE !== 'container') {
|
if (local) {
|
||||||
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
|
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
|
||||||
this.logger.verbose('Sending data to webhook local');
|
this.logger.verbose('Sending data to webhook local');
|
||||||
let baseURL;
|
let baseURL;
|
||||||
@@ -432,13 +511,7 @@ export class WAStartupService {
|
|||||||
globalURL = globalWebhook.URL;
|
globalURL = globalWebhook.URL;
|
||||||
}
|
}
|
||||||
|
|
||||||
let localUrl;
|
const localUrl = this.localWebhook.url;
|
||||||
|
|
||||||
if (instance.MODE === 'container') {
|
|
||||||
localUrl = instance.WEBHOOK_URL;
|
|
||||||
} else {
|
|
||||||
localUrl = this.localWebhook.url;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
|
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
|
||||||
const logData = {
|
const logData = {
|
||||||
@@ -562,6 +635,15 @@ export class WAStartupService {
|
|||||||
color: { light: '#ffffff', dark: '#198754' },
|
color: { light: '#ffffff', dark: '#198754' },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (this.phoneNumber) {
|
||||||
|
await delay(2000);
|
||||||
|
this.instance.qrcode.pairingCode = await this.client.requestPairingCode(
|
||||||
|
this.phoneNumber,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
this.instance.qrcode.pairingCode = null;
|
||||||
|
}
|
||||||
|
|
||||||
this.logger.verbose('Generating QR code');
|
this.logger.verbose('Generating QR code');
|
||||||
qrcode.toDataURL(qr, optsQrcode, (error, base64) => {
|
qrcode.toDataURL(qr, optsQrcode, (error, base64) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -573,7 +655,12 @@ export class WAStartupService {
|
|||||||
this.instance.qrcode.code = qr;
|
this.instance.qrcode.code = qr;
|
||||||
|
|
||||||
this.sendDataWebhook(Events.QRCODE_UPDATED, {
|
this.sendDataWebhook(Events.QRCODE_UPDATED, {
|
||||||
qrcode: { instance: this.instance.name, code: qr, base64 },
|
qrcode: {
|
||||||
|
instance: this.instance.name,
|
||||||
|
pairingCode: this.instance.qrcode.pairingCode,
|
||||||
|
code: qr,
|
||||||
|
base64,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.localChatwoot.enabled) {
|
if (this.localChatwoot.enabled) {
|
||||||
@@ -581,7 +668,12 @@ export class WAStartupService {
|
|||||||
Events.QRCODE_UPDATED,
|
Events.QRCODE_UPDATED,
|
||||||
{ instanceName: this.instance.name },
|
{ instanceName: this.instance.name },
|
||||||
{
|
{
|
||||||
qrcode: { instance: this.instance.name, code: qr, base64 },
|
qrcode: {
|
||||||
|
instance: this.instance.name,
|
||||||
|
pairingCode: this.instance.qrcode.pairingCode,
|
||||||
|
code: qr,
|
||||||
|
base64,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -590,7 +682,7 @@ export class WAStartupService {
|
|||||||
this.logger.verbose('Generating QR code in terminal');
|
this.logger.verbose('Generating QR code in terminal');
|
||||||
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
|
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`\n{ instance: ${this.instance.name}, qrcodeCount: ${this.instance.qrcode.count} }\n` +
|
`\n{ instance: ${this.instance.name} pairingCode: ${this.instance.qrcode.pairingCode}, qrcodeCount: ${this.instance.qrcode.count} }\n` +
|
||||||
qrcode,
|
qrcode,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -757,11 +849,12 @@ export class WAStartupService {
|
|||||||
return await useMultiFileAuthState(join(INSTANCE_DIR, this.instance.name));
|
return await useMultiFileAuthState(join(INSTANCE_DIR, this.instance.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connectToWhatsapp(): Promise<WASocket> {
|
public async connectToWhatsapp(number?: string): Promise<WASocket> {
|
||||||
this.logger.verbose('Connecting to whatsapp');
|
this.logger.verbose('Connecting to whatsapp');
|
||||||
try {
|
try {
|
||||||
this.loadWebhook();
|
this.loadWebhook();
|
||||||
this.loadChatwoot();
|
this.loadChatwoot();
|
||||||
|
this.loadSettings();
|
||||||
|
|
||||||
this.instance.authState = await this.defineAuthState();
|
this.instance.authState = await this.defineAuthState();
|
||||||
|
|
||||||
@@ -783,6 +876,7 @@ export class WAStartupService {
|
|||||||
printQRInTerminal: false,
|
printQRInTerminal: false,
|
||||||
browser,
|
browser,
|
||||||
version,
|
version,
|
||||||
|
markOnlineOnConnect: this.localSettings.always_online,
|
||||||
connectTimeoutMs: 60_000,
|
connectTimeoutMs: 60_000,
|
||||||
qrTimeout: 40_000,
|
qrTimeout: 40_000,
|
||||||
defaultQueryTimeoutMs: undefined,
|
defaultQueryTimeoutMs: undefined,
|
||||||
@@ -830,6 +924,8 @@ export class WAStartupService {
|
|||||||
|
|
||||||
this.logger.verbose('Socket event handler initialized');
|
this.logger.verbose('Socket event handler initialized');
|
||||||
|
|
||||||
|
this.phoneNumber = number;
|
||||||
|
|
||||||
return this.client;
|
return this.client;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
@@ -1044,6 +1140,7 @@ export class WAStartupService {
|
|||||||
type: MessageUpsertType;
|
type: MessageUpsertType;
|
||||||
},
|
},
|
||||||
database: Database,
|
database: Database,
|
||||||
|
settings: SettingsRaw,
|
||||||
) => {
|
) => {
|
||||||
this.logger.verbose('Event received: messages.upsert');
|
this.logger.verbose('Event received: messages.upsert');
|
||||||
const received = messages[0];
|
const received = messages[0];
|
||||||
@@ -1061,6 +1158,11 @@ export class WAStartupService {
|
|||||||
received.messageTimestamp = received.messageTimestamp?.toNumber();
|
received.messageTimestamp = received.messageTimestamp?.toNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (settings?.groups_ignore && received.key.remoteJid.includes('@g.us')) {
|
||||||
|
this.logger.verbose('group ignored');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const messageRaw: MessageRaw = {
|
const messageRaw: MessageRaw = {
|
||||||
key: received.key,
|
key: received.key,
|
||||||
pushName: received.pushName,
|
pushName: received.pushName,
|
||||||
@@ -1071,6 +1173,14 @@ export class WAStartupService {
|
|||||||
source: getDevice(received.key.id),
|
source: getDevice(received.key.id),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (this.localSettings.read_messages && received.key.id !== 'status@broadcast') {
|
||||||
|
await this.client.readMessages([received.key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.localSettings.read_status && received.key.id === 'status@broadcast') {
|
||||||
|
await this.client.readMessages([received.key]);
|
||||||
|
}
|
||||||
|
|
||||||
this.logger.log(messageRaw);
|
this.logger.log(messageRaw);
|
||||||
|
|
||||||
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
|
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
|
||||||
@@ -1152,7 +1262,11 @@ export class WAStartupService {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
'messages.update': async (args: WAMessageUpdate[], database: Database) => {
|
'messages.update': async (
|
||||||
|
args: WAMessageUpdate[],
|
||||||
|
database: Database,
|
||||||
|
settings: SettingsRaw,
|
||||||
|
) => {
|
||||||
this.logger.verbose('Event received: messages.update');
|
this.logger.verbose('Event received: messages.update');
|
||||||
const status: Record<number, wa.StatusMessage> = {
|
const status: Record<number, wa.StatusMessage> = {
|
||||||
0: 'ERROR',
|
0: 'ERROR',
|
||||||
@@ -1163,6 +1277,10 @@ export class WAStartupService {
|
|||||||
5: 'PLAYED',
|
5: 'PLAYED',
|
||||||
};
|
};
|
||||||
for await (const { key, update } of args) {
|
for await (const { key, update } of args) {
|
||||||
|
if (settings?.groups_ignore && key.remoteJid.includes('@g.us')) {
|
||||||
|
this.logger.verbose('group ignored');
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (key.remoteJid !== 'status@broadcast' && !key?.remoteJid?.match(/(:\d+)/)) {
|
if (key.remoteJid !== 'status@broadcast' && !key?.remoteJid?.match(/(:\d+)/)) {
|
||||||
this.logger.verbose('Message update is valid');
|
this.logger.verbose('Message update is valid');
|
||||||
|
|
||||||
@@ -1190,6 +1308,22 @@ export class WAStartupService {
|
|||||||
|
|
||||||
this.logger.verbose('Sending data to webhook in event MESSAGE_DELETE');
|
this.logger.verbose('Sending data to webhook in event MESSAGE_DELETE');
|
||||||
await this.sendDataWebhook(Events.MESSAGES_DELETE, key);
|
await this.sendDataWebhook(Events.MESSAGES_DELETE, key);
|
||||||
|
|
||||||
|
const message: MessageUpdateRaw = {
|
||||||
|
...key,
|
||||||
|
status: 'DELETED',
|
||||||
|
datetime: Date.now(),
|
||||||
|
owner: this.instance.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.logger.verbose(message);
|
||||||
|
|
||||||
|
this.logger.verbose('Inserting message in database');
|
||||||
|
await this.repository.messageUpdate.insert(
|
||||||
|
[message],
|
||||||
|
this.instance.name,
|
||||||
|
database.SAVE_DATA.MESSAGE_UPDATE,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1246,9 +1380,36 @@ export class WAStartupService {
|
|||||||
|
|
||||||
private eventHandler() {
|
private eventHandler() {
|
||||||
this.logger.verbose('Initializing event handler');
|
this.logger.verbose('Initializing event handler');
|
||||||
this.client.ev.process((events) => {
|
this.client.ev.process(async (events) => {
|
||||||
if (!this.endSession) {
|
if (!this.endSession) {
|
||||||
const database = this.configService.get<Database>('DATABASE');
|
const database = this.configService.get<Database>('DATABASE');
|
||||||
|
const settings = await this.findSettings();
|
||||||
|
|
||||||
|
if (events.call) {
|
||||||
|
this.logger.verbose('Listening event: call');
|
||||||
|
const call = events.call[0];
|
||||||
|
|
||||||
|
if (settings?.reject_call && call.status == 'offer') {
|
||||||
|
this.logger.verbose('Rejecting call');
|
||||||
|
this.client.rejectCall(call.id, call.from);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (settings?.msg_call.trim().length > 0 && call.status == 'offer') {
|
||||||
|
this.logger.verbose('Sending message in call');
|
||||||
|
const msg = await this.client.sendMessage(call.from, {
|
||||||
|
text: settings.msg_call,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.logger.verbose('Sending data to event messages.upsert');
|
||||||
|
this.client.ev.emit('messages.upsert', {
|
||||||
|
messages: [msg],
|
||||||
|
type: 'notify',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.verbose('Sending data to webhook in event CALL');
|
||||||
|
this.sendDataWebhook(Events.CALL, call);
|
||||||
|
}
|
||||||
|
|
||||||
if (events['connection.update']) {
|
if (events['connection.update']) {
|
||||||
this.logger.verbose('Listening event: connection.update');
|
this.logger.verbose('Listening event: connection.update');
|
||||||
@@ -1269,21 +1430,27 @@ export class WAStartupService {
|
|||||||
if (events['messages.upsert']) {
|
if (events['messages.upsert']) {
|
||||||
this.logger.verbose('Listening event: messages.upsert');
|
this.logger.verbose('Listening event: messages.upsert');
|
||||||
const payload = events['messages.upsert'];
|
const payload = events['messages.upsert'];
|
||||||
this.messageHandle['messages.upsert'](payload, database);
|
this.messageHandle['messages.upsert'](payload, database, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (events['messages.update']) {
|
if (events['messages.update']) {
|
||||||
this.logger.verbose('Listening event: messages.update');
|
this.logger.verbose('Listening event: messages.update');
|
||||||
const payload = events['messages.update'];
|
const payload = events['messages.update'];
|
||||||
this.messageHandle['messages.update'](payload, database);
|
this.messageHandle['messages.update'](payload, database, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (events['presence.update']) {
|
if (events['presence.update']) {
|
||||||
this.logger.verbose('Listening event: presence.update');
|
this.logger.verbose('Listening event: presence.update');
|
||||||
const payload = events['presence.update'];
|
const payload = events['presence.update'];
|
||||||
|
|
||||||
|
if (settings.groups_ignore && payload.id.includes('@g.us')) {
|
||||||
|
this.logger.verbose('group ignored');
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.sendDataWebhook(Events.PRESENCE_UPDATE, payload);
|
this.sendDataWebhook(Events.PRESENCE_UPDATE, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!settings?.groups_ignore) {
|
||||||
if (events['groups.upsert']) {
|
if (events['groups.upsert']) {
|
||||||
this.logger.verbose('Listening event: groups.upsert');
|
this.logger.verbose('Listening event: groups.upsert');
|
||||||
const payload = events['groups.upsert'];
|
const payload = events['groups.upsert'];
|
||||||
@@ -1301,6 +1468,7 @@ export class WAStartupService {
|
|||||||
const payload = events['group-participants.update'];
|
const payload = events['group-participants.update'];
|
||||||
this.groupHandler['group-participants.update'](payload);
|
this.groupHandler['group-participants.update'](payload);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (events['chats.upsert']) {
|
if (events['chats.upsert']) {
|
||||||
this.logger.verbose('Listening event: chats.upsert');
|
this.logger.verbose('Listening event: chats.upsert');
|
||||||
@@ -1382,35 +1550,25 @@ export class WAStartupService {
|
|||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const countryCode = number.substring(0, 2);
|
number = number
|
||||||
|
?.replace(/\s/g, '')
|
||||||
|
.replace(/\+/g, '')
|
||||||
|
.replace(/\(/g, '')
|
||||||
|
.replace(/\)/g, '')
|
||||||
|
.split(/\:/)[0]
|
||||||
|
.split('@')[0];
|
||||||
|
|
||||||
if (Number(countryCode) === 55) {
|
if (number.includes('-') && number.length >= 24) {
|
||||||
const formattedBRNumber = this.formatBRNumber(number);
|
|
||||||
if (formattedBRNumber !== number) {
|
|
||||||
this.logger.verbose(
|
|
||||||
'Jid created is whatsapp in format BR: ' +
|
|
||||||
`${formattedBRNumber}@s.whatsapp.net`,
|
|
||||||
);
|
|
||||||
return `${formattedBRNumber}@s.whatsapp.net`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Number(countryCode) === 52 || Number(countryCode) === 54) {
|
|
||||||
console.log('numero mexicano');
|
|
||||||
|
|
||||||
const formattedMXARNumber = this.formatMXOrARNumber(number);
|
|
||||||
|
|
||||||
if (formattedMXARNumber !== number) {
|
|
||||||
this.logger.verbose(
|
|
||||||
'Jid created is whatsapp in format MXAR: ' +
|
|
||||||
`${formattedMXARNumber}@s.whatsapp.net`,
|
|
||||||
);
|
|
||||||
return `${formattedMXARNumber}@s.whatsapp.net`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (number.includes('-')) {
|
|
||||||
this.logger.verbose('Jid created is group: ' + `${number}@g.us`);
|
this.logger.verbose('Jid created is group: ' + `${number}@g.us`);
|
||||||
|
number = number.replace(/[^\d-]/g, '');
|
||||||
|
return `${number}@g.us`;
|
||||||
|
}
|
||||||
|
|
||||||
|
number = number.replace(/\D/g, '');
|
||||||
|
|
||||||
|
if (number.length >= 18) {
|
||||||
|
this.logger.verbose('Jid created is group: ' + `${number}@g.us`);
|
||||||
|
number = number.replace(/[^\d-]/g, '');
|
||||||
return `${number}@g.us`;
|
return `${number}@g.us`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1437,6 +1595,78 @@ export class WAStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getStatus(number: string) {
|
||||||
|
const jid = this.createJid(number);
|
||||||
|
|
||||||
|
this.logger.verbose('Getting profile status with jid:' + jid);
|
||||||
|
try {
|
||||||
|
this.logger.verbose('Getting status');
|
||||||
|
return {
|
||||||
|
wuid: jid,
|
||||||
|
status: (await this.client.fetchStatus(jid))?.status,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.verbose('Status not found');
|
||||||
|
return {
|
||||||
|
wuid: jid,
|
||||||
|
status: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async fetchProfile(instanceName: string, number?: string) {
|
||||||
|
const jid = number ? this.createJid(number) : this.client?.user?.id;
|
||||||
|
|
||||||
|
this.logger.verbose('Getting profile with jid: ' + jid);
|
||||||
|
try {
|
||||||
|
this.logger.verbose('Getting profile info');
|
||||||
|
const info = await waMonitor.instanceInfo(instanceName);
|
||||||
|
const business = await this.fetchBusinessProfile(jid);
|
||||||
|
|
||||||
|
if (number) {
|
||||||
|
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
|
||||||
|
const picture = await this.profilePicture(jid);
|
||||||
|
const status = await this.getStatus(jid);
|
||||||
|
|
||||||
|
return {
|
||||||
|
wuid: jid,
|
||||||
|
name: info?.name,
|
||||||
|
numberExists: info?.exists,
|
||||||
|
picture: picture?.profilePictureUrl,
|
||||||
|
status: status?.status,
|
||||||
|
isBusiness: business.isBusiness,
|
||||||
|
email: business?.email,
|
||||||
|
description: business?.description,
|
||||||
|
website: business?.website?.shift(),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const info = await waMonitor.instanceInfo(instanceName);
|
||||||
|
|
||||||
|
return {
|
||||||
|
wuid: jid,
|
||||||
|
name: info?.instance?.profileName,
|
||||||
|
numberExists: true,
|
||||||
|
picture: info?.instance?.profilePictureUrl,
|
||||||
|
status: info?.instance?.profileStatus,
|
||||||
|
isBusiness: business.isBusiness,
|
||||||
|
email: business?.email,
|
||||||
|
description: business?.description,
|
||||||
|
website: business?.website?.shift(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.verbose('Profile not found');
|
||||||
|
return {
|
||||||
|
wuid: jid,
|
||||||
|
name: null,
|
||||||
|
picture: null,
|
||||||
|
status: null,
|
||||||
|
os: null,
|
||||||
|
isBusiness: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async sendMessageWithTyping<T = proto.IMessage>(
|
private async sendMessageWithTyping<T = proto.IMessage>(
|
||||||
number: string,
|
number: string,
|
||||||
message: T,
|
message: T,
|
||||||
@@ -1444,15 +1674,14 @@ export class WAStartupService {
|
|||||||
) {
|
) {
|
||||||
this.logger.verbose('Sending message with typing');
|
this.logger.verbose('Sending message with typing');
|
||||||
|
|
||||||
const jid = this.createJid(number);
|
const numberWA = await this.whatsappNumber({ numbers: [number] });
|
||||||
const numberWA = await this.whatsappNumber({ numbers: [jid] });
|
|
||||||
const isWA = numberWA[0];
|
const isWA = numberWA[0];
|
||||||
|
|
||||||
if (!isWA.exists && !isJidGroup(isWA.jid) && !isWA.jid.includes('@broadcast')) {
|
if (!isWA.exists && !isJidGroup(isWA.jid) && !isWA.jid.includes('@broadcast')) {
|
||||||
throw new BadRequestException(isWA);
|
throw new BadRequestException(isWA);
|
||||||
}
|
}
|
||||||
|
|
||||||
const sender = isJidGroup(jid) ? jid : isWA.jid;
|
const sender = isWA.jid;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (options?.delay) {
|
if (options?.delay) {
|
||||||
@@ -1461,7 +1690,7 @@ export class WAStartupService {
|
|||||||
await this.client.presenceSubscribe(sender);
|
await this.client.presenceSubscribe(sender);
|
||||||
this.logger.verbose('Subscribing to presence');
|
this.logger.verbose('Subscribing to presence');
|
||||||
|
|
||||||
await this.client.sendPresenceUpdate(options?.presence ?? 'composing', jid);
|
await this.client.sendPresenceUpdate(options?.presence ?? 'composing', sender);
|
||||||
this.logger.verbose(
|
this.logger.verbose(
|
||||||
'Sending presence update: ' + options?.presence ?? 'composing',
|
'Sending presence update: ' + options?.presence ?? 'composing',
|
||||||
);
|
);
|
||||||
@@ -1473,6 +1702,8 @@ export class WAStartupService {
|
|||||||
this.logger.verbose('Sending presence update: paused');
|
this.logger.verbose('Sending presence update: paused');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const linkPreview = options?.linkPreview != false ? undefined : false;
|
||||||
|
|
||||||
let quoted: WAMessage;
|
let quoted: WAMessage;
|
||||||
|
|
||||||
if (options?.quoted) {
|
if (options?.quoted) {
|
||||||
@@ -1502,25 +1733,19 @@ export class WAStartupService {
|
|||||||
if (options?.mentions) {
|
if (options?.mentions) {
|
||||||
this.logger.verbose('Mentions defined');
|
this.logger.verbose('Mentions defined');
|
||||||
|
|
||||||
if (
|
if (options.mentions?.everyOne) {
|
||||||
!Array.isArray(options.mentions.mentioned) &&
|
|
||||||
!options.mentions.everyOne
|
|
||||||
) {
|
|
||||||
throw new BadRequestException('Mentions must be an array');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.mentions.everyOne) {
|
|
||||||
this.logger.verbose('Mentions everyone');
|
this.logger.verbose('Mentions everyone');
|
||||||
|
|
||||||
this.logger.verbose('Getting group metadata');
|
this.logger.verbose('Getting group metadata');
|
||||||
mentions = groupMetadata.participants.map((participant) => participant.id);
|
mentions = groupMetadata.participants.map((participant) => participant.id);
|
||||||
this.logger.verbose('Getting group metadata for mentions');
|
this.logger.verbose('Getting group metadata for mentions');
|
||||||
} else {
|
} else if (options.mentions?.mentioned?.length) {
|
||||||
this.logger.verbose('Mentions manually defined');
|
this.logger.verbose('Mentions manually defined');
|
||||||
mentions = options.mentions.mentioned.map((mention) => {
|
mentions = options.mentions.mentioned.map((mention) => {
|
||||||
const jid = this.createJid(mention);
|
const jid = this.createJid(mention);
|
||||||
if (isJidGroup(jid)) {
|
if (isJidGroup(jid)) {
|
||||||
throw new BadRequestException('Mentions must be a number');
|
return null;
|
||||||
|
// throw new BadRequestException('Mentions must be a number');
|
||||||
}
|
}
|
||||||
return jid;
|
return jid;
|
||||||
});
|
});
|
||||||
@@ -1566,6 +1791,7 @@ export class WAStartupService {
|
|||||||
{
|
{
|
||||||
text: message['conversation'],
|
text: message['conversation'],
|
||||||
mentions,
|
mentions,
|
||||||
|
linkPreview: linkPreview,
|
||||||
} as unknown as AnyMessageContent,
|
} as unknown as AnyMessageContent,
|
||||||
option as unknown as MiscMessageGenerationOptions,
|
option as unknown as MiscMessageGenerationOptions,
|
||||||
);
|
);
|
||||||
@@ -1986,6 +2212,12 @@ export class WAStartupService {
|
|||||||
|
|
||||||
public async audioWhatsapp(data: SendAudioDto) {
|
public async audioWhatsapp(data: SendAudioDto) {
|
||||||
this.logger.verbose('Sending audio whatsapp');
|
this.logger.verbose('Sending audio whatsapp');
|
||||||
|
|
||||||
|
if (!data.options?.encoding && data.options?.encoding !== false) {
|
||||||
|
data.options.encoding = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.options?.encoding) {
|
||||||
const convert = await this.processAudio(data.audioMessage.audio, data.number);
|
const convert = await this.processAudio(data.audioMessage.audio, data.number);
|
||||||
if (typeof convert === 'string') {
|
if (typeof convert === 'string') {
|
||||||
const audio = fs.readFileSync(convert).toString('base64');
|
const audio = fs.readFileSync(convert).toString('base64');
|
||||||
@@ -2008,6 +2240,19 @@ export class WAStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return await this.sendMessageWithTyping<AnyMessageContent>(
|
||||||
|
data.number,
|
||||||
|
{
|
||||||
|
audio: isURL(data.audioMessage.audio)
|
||||||
|
? { url: data.audioMessage.audio }
|
||||||
|
: Buffer.from(data.audioMessage.audio, 'base64'),
|
||||||
|
ptt: true,
|
||||||
|
mimetype: 'audio/ogg; codecs=opus',
|
||||||
|
},
|
||||||
|
{ presence: 'recording', delay: data?.options?.delay },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public async buttonMessage(data: SendButtonDto) {
|
public async buttonMessage(data: SendButtonDto) {
|
||||||
this.logger.verbose('Sending button message');
|
this.logger.verbose('Sending button message');
|
||||||
const embeddedMedia: any = {};
|
const embeddedMedia: any = {};
|
||||||
@@ -2118,6 +2363,11 @@ export class WAStartupService {
|
|||||||
result += `URL:${contact.url}\n`;
|
result += `URL:${contact.url}\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!contact.wuid) {
|
||||||
|
this.logger.verbose('Wuid defined');
|
||||||
|
contact.wuid = this.createJid(contact.phoneNumber);
|
||||||
|
}
|
||||||
|
|
||||||
result +=
|
result +=
|
||||||
`item1.TEL;waid=${contact.wuid}:${contact.phoneNumber}\n` +
|
`item1.TEL;waid=${contact.wuid}:${contact.phoneNumber}\n` +
|
||||||
'item1.X-ABLabel:Celular\n' +
|
'item1.X-ABLabel:Celular\n' +
|
||||||
@@ -2163,8 +2413,8 @@ export class WAStartupService {
|
|||||||
|
|
||||||
const onWhatsapp: OnWhatsAppDto[] = [];
|
const onWhatsapp: OnWhatsAppDto[] = [];
|
||||||
for await (const number of data.numbers) {
|
for await (const number of data.numbers) {
|
||||||
const jid = this.createJid(number);
|
let jid = this.createJid(number);
|
||||||
// const jid = `${number}@s.whatsapp.net`;
|
|
||||||
if (isJidGroup(jid)) {
|
if (isJidGroup(jid)) {
|
||||||
const group = await this.findGroup({ groupJid: jid }, 'inner');
|
const group = await this.findGroup({ groupJid: jid }, 'inner');
|
||||||
|
|
||||||
@@ -2172,6 +2422,7 @@ export class WAStartupService {
|
|||||||
|
|
||||||
onWhatsapp.push(new OnWhatsAppDto(group.id, !!group?.id, group?.subject));
|
onWhatsapp.push(new OnWhatsAppDto(group.id, !!group?.id, group?.subject));
|
||||||
} else {
|
} else {
|
||||||
|
jid = !jid.startsWith('+') ? `+${jid}` : jid;
|
||||||
const verify = await this.client.onWhatsApp(jid);
|
const verify = await this.client.onWhatsApp(jid);
|
||||||
|
|
||||||
const result = verify[0];
|
const result = verify[0];
|
||||||
@@ -2191,7 +2442,7 @@ export class WAStartupService {
|
|||||||
this.logger.verbose('Marking message as read');
|
this.logger.verbose('Marking message as read');
|
||||||
try {
|
try {
|
||||||
const keys: proto.IMessageKey[] = [];
|
const keys: proto.IMessageKey[] = [];
|
||||||
data.readMessages.forEach((read) => {
|
data.read_messages.forEach((read) => {
|
||||||
if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) {
|
if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) {
|
||||||
keys.push({
|
keys.push({
|
||||||
remoteJid: read.remoteJid,
|
remoteJid: read.remoteJid,
|
||||||
@@ -2351,6 +2602,9 @@ export class WAStartupService {
|
|||||||
this.logger.verbose('Fetching contacts');
|
this.logger.verbose('Fetching contacts');
|
||||||
if (query?.where) {
|
if (query?.where) {
|
||||||
query.where.owner = this.instance.name;
|
query.where.owner = this.instance.name;
|
||||||
|
if (query.where?.id) {
|
||||||
|
query.where.id = this.createJid(query.where.id);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
query = {
|
query = {
|
||||||
where: {
|
where: {
|
||||||
@@ -2364,6 +2618,9 @@ export class WAStartupService {
|
|||||||
public async fetchMessages(query: MessageQuery) {
|
public async fetchMessages(query: MessageQuery) {
|
||||||
this.logger.verbose('Fetching messages');
|
this.logger.verbose('Fetching messages');
|
||||||
if (query?.where) {
|
if (query?.where) {
|
||||||
|
if (query.where?.key?.remoteJid) {
|
||||||
|
query.where.key.remoteJid = this.createJid(query.where.key.remoteJid);
|
||||||
|
}
|
||||||
query.where.owner = this.instance.name;
|
query.where.owner = this.instance.name;
|
||||||
} else {
|
} else {
|
||||||
query = {
|
query = {
|
||||||
@@ -2379,6 +2636,9 @@ export class WAStartupService {
|
|||||||
public async fetchStatusMessage(query: MessageUpQuery) {
|
public async fetchStatusMessage(query: MessageUpQuery) {
|
||||||
this.logger.verbose('Fetching status messages');
|
this.logger.verbose('Fetching status messages');
|
||||||
if (query?.where) {
|
if (query?.where) {
|
||||||
|
if (query.where?.remoteJid) {
|
||||||
|
query.where.remoteJid = this.createJid(query.where.remoteJid);
|
||||||
|
}
|
||||||
query.where.owner = this.instance.name;
|
query.where.owner = this.instance.name;
|
||||||
} else {
|
} else {
|
||||||
query = {
|
query = {
|
||||||
@@ -2422,9 +2682,19 @@ export class WAStartupService {
|
|||||||
await this.client.updateGroupsAddPrivacy(settings.privacySettings.groupadd);
|
await this.client.updateGroupsAddPrivacy(settings.privacySettings.groupadd);
|
||||||
this.logger.verbose('Groups add privacy updated');
|
this.logger.verbose('Groups add privacy updated');
|
||||||
|
|
||||||
// reinicia a instancia
|
this.client?.ws?.close();
|
||||||
|
|
||||||
return { update: 'success', data: await this.client.fetchPrivacySettings() };
|
return {
|
||||||
|
update: 'success',
|
||||||
|
data: {
|
||||||
|
readreceipts: settings.privacySettings.readreceipts,
|
||||||
|
profile: settings.privacySettings.profile,
|
||||||
|
status: settings.privacySettings.status,
|
||||||
|
online: settings.privacySettings.online,
|
||||||
|
last: settings.privacySettings.last,
|
||||||
|
groupadd: settings.privacySettings.groupadd,
|
||||||
|
},
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new InternalServerErrorException(
|
throw new InternalServerErrorException(
|
||||||
'Error updating privacy settings',
|
'Error updating privacy settings',
|
||||||
@@ -2433,29 +2703,29 @@ export class WAStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchBusinessProfile(number: string) {
|
public async fetchBusinessProfile(number: string): Promise<NumberBusiness> {
|
||||||
this.logger.verbose('Fetching business profile');
|
this.logger.verbose('Fetching business profile');
|
||||||
try {
|
try {
|
||||||
let jid;
|
const jid = number ? this.createJid(number) : this.instance.wuid;
|
||||||
|
|
||||||
if (!number) {
|
|
||||||
jid = this.instance.wuid;
|
|
||||||
} else {
|
|
||||||
jid = this.createJid(number);
|
|
||||||
}
|
|
||||||
|
|
||||||
const profile = await this.client.getBusinessProfile(jid);
|
const profile = await this.client.getBusinessProfile(jid);
|
||||||
this.logger.verbose('Trying to get business profile');
|
this.logger.verbose('Trying to get business profile');
|
||||||
|
|
||||||
if (!profile) {
|
if (!profile) {
|
||||||
|
const info = await this.whatsappNumber({ numbers: [jid] });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
exists: false,
|
isBusiness: false,
|
||||||
message: 'Business profile not found',
|
message: 'Not is business profile',
|
||||||
|
...info?.shift(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('Business profile fetched');
|
this.logger.verbose('Business profile fetched');
|
||||||
return profile;
|
return {
|
||||||
|
isBusiness: true,
|
||||||
|
...profile,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new InternalServerErrorException(
|
throw new InternalServerErrorException(
|
||||||
'Error updating profile name',
|
'Error updating profile name',
|
||||||
@@ -2551,10 +2821,19 @@ export class WAStartupService {
|
|||||||
await this.client.groupUpdateDescription(id, create.description);
|
await this.client.groupUpdateDescription(id, create.description);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (create?.promoteParticipants) {
|
||||||
|
this.logger.verbose('Prometing group participants: ' + create.description);
|
||||||
|
await this.updateGParticipant({
|
||||||
|
groupJid: id,
|
||||||
|
action: 'promote',
|
||||||
|
participants: participants,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const group = await this.client.groupMetadata(id);
|
const group = await this.client.groupMetadata(id);
|
||||||
this.logger.verbose('Getting group metadata');
|
this.logger.verbose('Getting group metadata');
|
||||||
|
|
||||||
return { groupMetadata: group };
|
return group;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
throw new InternalServerErrorException('Error creating group', error.toString());
|
throw new InternalServerErrorException('Error creating group', error.toString());
|
||||||
|
|||||||
@@ -22,12 +22,19 @@ export enum Events {
|
|||||||
GROUPS_UPSERT = 'groups.upsert',
|
GROUPS_UPSERT = 'groups.upsert',
|
||||||
GROUPS_UPDATE = 'groups.update',
|
GROUPS_UPDATE = 'groups.update',
|
||||||
GROUP_PARTICIPANTS_UPDATE = 'group-participants.update',
|
GROUP_PARTICIPANTS_UPDATE = 'group-participants.update',
|
||||||
|
CALL = 'call',
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare namespace wa {
|
export declare namespace wa {
|
||||||
export type QrCode = { count?: number; base64?: string; code?: string };
|
export type QrCode = {
|
||||||
|
count?: number;
|
||||||
|
pairingCode?: string;
|
||||||
|
base64?: string;
|
||||||
|
code?: string;
|
||||||
|
};
|
||||||
export type Instance = {
|
export type Instance = {
|
||||||
qrcode?: QrCode;
|
qrcode?: QrCode;
|
||||||
|
pairingCode?: string;
|
||||||
authState?: { state: AuthenticationState; saveCreds: () => void };
|
authState?: { state: AuthenticationState; saveCreds: () => void };
|
||||||
name?: string;
|
name?: string;
|
||||||
wuid?: string;
|
wuid?: string;
|
||||||
@@ -49,6 +56,18 @@ export declare namespace wa {
|
|||||||
url?: string;
|
url?: string;
|
||||||
name_inbox?: string;
|
name_inbox?: string;
|
||||||
sign_msg?: boolean;
|
sign_msg?: boolean;
|
||||||
|
number?: string;
|
||||||
|
reopen_conversation?: boolean;
|
||||||
|
conversation_pending?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type LocalSettings = {
|
||||||
|
reject_call?: boolean;
|
||||||
|
msg_call?: string;
|
||||||
|
groups_ignore?: boolean;
|
||||||
|
always_online?: boolean;
|
||||||
|
read_messages?: boolean;
|
||||||
|
read_status?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type StateConnection = {
|
export type StateConnection = {
|
||||||
@@ -63,6 +82,7 @@ export declare namespace wa {
|
|||||||
| 'SERVER_ACK'
|
| 'SERVER_ACK'
|
||||||
| 'DELIVERY_ACK'
|
| 'DELIVERY_ACK'
|
||||||
| 'READ'
|
| 'READ'
|
||||||
|
| 'DELETED'
|
||||||
| 'PLAYED';
|
| 'PLAYED';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import {
|
|||||||
MessageUpModel,
|
MessageUpModel,
|
||||||
ChatwootModel,
|
ChatwootModel,
|
||||||
WebhookModel,
|
WebhookModel,
|
||||||
|
SettingsModel,
|
||||||
} from './models';
|
} from './models';
|
||||||
import { dbserver } from '../db/db.connect';
|
import { dbserver } from '../db/db.connect';
|
||||||
import { WebhookRepository } from './repository/webhook.repository';
|
import { WebhookRepository } from './repository/webhook.repository';
|
||||||
@@ -34,6 +35,9 @@ 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';
|
import { RedisCache } from '../db/redis.client';
|
||||||
|
import { SettingsRepository } from './repository/settings.repository';
|
||||||
|
import { SettingsService } from './services/settings.service';
|
||||||
|
import { SettingsController } from './controllers/settings.controller';
|
||||||
|
|
||||||
const logger = new Logger('WA MODULE');
|
const logger = new Logger('WA MODULE');
|
||||||
|
|
||||||
@@ -43,6 +47,7 @@ const contactRepository = new ContactRepository(ContactModel, configService);
|
|||||||
const messageUpdateRepository = new MessageUpRepository(MessageUpModel, configService);
|
const messageUpdateRepository = new MessageUpRepository(MessageUpModel, configService);
|
||||||
const webhookRepository = new WebhookRepository(WebhookModel, configService);
|
const webhookRepository = new WebhookRepository(WebhookModel, configService);
|
||||||
const chatwootRepository = new ChatwootRepository(ChatwootModel, configService);
|
const chatwootRepository = new ChatwootRepository(ChatwootModel, configService);
|
||||||
|
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(
|
||||||
@@ -52,6 +57,7 @@ export const repository = new RepositoryBroker(
|
|||||||
messageUpdateRepository,
|
messageUpdateRepository,
|
||||||
webhookRepository,
|
webhookRepository,
|
||||||
chatwootRepository,
|
chatwootRepository,
|
||||||
|
settingsRepository,
|
||||||
authRepository,
|
authRepository,
|
||||||
configService,
|
configService,
|
||||||
dbserver?.getClient(),
|
dbserver?.getClient(),
|
||||||
@@ -76,6 +82,10 @@ const chatwootService = new ChatwootService(waMonitor, configService);
|
|||||||
|
|
||||||
export const chatwootController = new ChatwootController(chatwootService, configService);
|
export const chatwootController = new ChatwootController(chatwootService, configService);
|
||||||
|
|
||||||
|
const settingsService = new SettingsService(waMonitor);
|
||||||
|
|
||||||
|
export const settingsController = new SettingsController(settingsService);
|
||||||
|
|
||||||
export const instanceController = new InstanceController(
|
export const instanceController = new InstanceController(
|
||||||
waMonitor,
|
waMonitor,
|
||||||
configService,
|
configService,
|
||||||
@@ -84,6 +94,7 @@ export const instanceController = new InstanceController(
|
|||||||
authService,
|
authService,
|
||||||
webhookService,
|
webhookService,
|
||||||
chatwootService,
|
chatwootService,
|
||||||
|
settingsService,
|
||||||
cache,
|
cache,
|
||||||
);
|
);
|
||||||
export const viewsController = new ViewsController(waMonitor, configService);
|
export const viewsController = new ViewsController(waMonitor, configService);
|
||||||
@@ -91,111 +102,4 @@ export const sendMessageController = new SendMessageController(waMonitor);
|
|||||||
export const chatController = new ChatController(waMonitor);
|
export const chatController = new ChatController(waMonitor);
|
||||||
export const groupController = new GroupController(waMonitor);
|
export const groupController = new GroupController(waMonitor);
|
||||||
|
|
||||||
export async function initInstance() {
|
|
||||||
const instance = new WAStartupService(configService, eventEmitter, repository, cache);
|
|
||||||
|
|
||||||
const mode = configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
|
|
||||||
|
|
||||||
logger.verbose('Sending data webhook for event: ' + Events.APPLICATION_STARTUP);
|
|
||||||
instance.sendDataWebhook(
|
|
||||||
Events.APPLICATION_STARTUP,
|
|
||||||
{
|
|
||||||
message: 'Application startup',
|
|
||||||
mode,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (mode === 'container') {
|
|
||||||
logger.verbose('Application startup in container mode');
|
|
||||||
|
|
||||||
const instanceName = configService.get<Auth>('AUTHENTICATION').INSTANCE.NAME;
|
|
||||||
logger.verbose('Instance name: ' + instanceName);
|
|
||||||
|
|
||||||
const instanceWebhook =
|
|
||||||
configService.get<Auth>('AUTHENTICATION').INSTANCE.WEBHOOK_URL;
|
|
||||||
logger.verbose('Instance webhook: ' + instanceWebhook);
|
|
||||||
|
|
||||||
const chatwootAccountId =
|
|
||||||
configService.get<Auth>('AUTHENTICATION').INSTANCE.CHATWOOT_ACCOUNT_ID;
|
|
||||||
logger.verbose('Chatwoot account id: ' + chatwootAccountId);
|
|
||||||
|
|
||||||
const chatwootToken =
|
|
||||||
configService.get<Auth>('AUTHENTICATION').INSTANCE.CHATWOOT_TOKEN;
|
|
||||||
logger.verbose('Chatwoot token: ' + chatwootToken);
|
|
||||||
|
|
||||||
const chatwootUrl = configService.get<Auth>('AUTHENTICATION').INSTANCE.CHATWOOT_URL;
|
|
||||||
logger.verbose('Chatwoot url: ' + chatwootUrl);
|
|
||||||
|
|
||||||
instance.instanceName = instanceName;
|
|
||||||
|
|
||||||
waMonitor.waInstances[instance.instanceName] = instance;
|
|
||||||
waMonitor.delInstanceTime(instance.instanceName);
|
|
||||||
|
|
||||||
const hash = await authService.generateHash({
|
|
||||||
instanceName: instance.instanceName,
|
|
||||||
token: configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
|
|
||||||
});
|
|
||||||
logger.verbose('Hash generated: ' + hash);
|
|
||||||
|
|
||||||
if (instanceWebhook) {
|
|
||||||
logger.verbose('Creating webhook for instance: ' + instanceName);
|
|
||||||
try {
|
|
||||||
webhookService.create(instance, { enabled: true, url: instanceWebhook });
|
|
||||||
logger.verbose('Webhook created');
|
|
||||||
} catch (error) {
|
|
||||||
logger.log(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (chatwootUrl && chatwootToken && chatwootAccountId) {
|
|
||||||
logger.verbose('Creating chatwoot for instance: ' + instanceName);
|
|
||||||
try {
|
|
||||||
chatwootService.create(instance, {
|
|
||||||
enabled: true,
|
|
||||||
url: chatwootUrl,
|
|
||||||
token: chatwootToken,
|
|
||||||
account_id: chatwootAccountId,
|
|
||||||
sign_msg: false,
|
|
||||||
});
|
|
||||||
logger.verbose('Chatwoot created');
|
|
||||||
} catch (error) {
|
|
||||||
logger.log(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const state = instance.connectionStatus?.state;
|
|
||||||
|
|
||||||
switch (state) {
|
|
||||||
case 'close':
|
|
||||||
await instance.connectToWhatsapp();
|
|
||||||
await delay(2000);
|
|
||||||
return instance.qrCode;
|
|
||||||
case 'connecting':
|
|
||||||
return instance.qrCode;
|
|
||||||
default:
|
|
||||||
return await this.connectionState({ instanceName });
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
logger.log(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = {
|
|
||||||
instance: {
|
|
||||||
instanceName: instance.instanceName,
|
|
||||||
status: 'created',
|
|
||||||
},
|
|
||||||
hash,
|
|
||||||
webhook: instanceWebhook,
|
|
||||||
};
|
|
||||||
|
|
||||||
logger.info(result);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info('Module - ON');
|
logger.info('Module - ON');
|
||||||
|
|||||||
Reference in New Issue
Block a user