mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-19 03:42:23 -06:00
Compare commits
145 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
acac09375e | ||
|
|
83cf859da3 | ||
|
|
956c391f13 | ||
|
|
7767a2607e | ||
|
|
429d1ac183 | ||
|
|
f42928f88f | ||
|
|
2d816ab92d | ||
|
|
aa75380662 | ||
|
|
3cf0ced62e | ||
|
|
41fa700fb3 | ||
|
|
a4ef9fb9b7 | ||
|
|
1db23b5277 | ||
|
|
584f0cbac0 | ||
|
|
dc432a19a3 | ||
|
|
6727b1cfca | ||
|
|
8c7b0698f3 | ||
|
|
69e1622e82 | ||
|
|
af7a5d3248 | ||
|
|
7b1a4554ad | ||
|
|
b3e213c133 | ||
|
|
a9fafec79d | ||
|
|
7cacf7bbaf | ||
|
|
44bf39f7b7 | ||
|
|
0db8f7295b | ||
|
|
c1226062b1 | ||
|
|
819ed168da | ||
|
|
19940953e2 | ||
|
|
f4af3eaf5d | ||
|
|
718563fe6a | ||
|
|
7e996ad6fa | ||
|
|
0b07fb6a49 | ||
|
|
0cb87e6ed9 | ||
|
|
71a3e75ae2 | ||
|
|
a2448ea4c1 | ||
|
|
8a14141021 | ||
|
|
69353892d9 | ||
|
|
7a2fcc3469 | ||
|
|
dfa6dd5011 | ||
|
|
654400c0cf | ||
|
|
3c15fc1b0d | ||
|
|
4b32485e3c | ||
|
|
b90736ed25 | ||
|
|
f28d5c7781 | ||
|
|
0654627478 | ||
|
|
2e91a2ecdb | ||
|
|
66a8ceb27a | ||
|
|
e6b0addb6c | ||
|
|
c00f132145 | ||
|
|
b9eb8d45b2 | ||
|
|
ec7ad70458 | ||
|
|
64e19699fb | ||
|
|
6d3e1b0cbe | ||
|
|
d3a53e1d3c | ||
|
|
23bbb3ee32 | ||
|
|
56bfcd52b7 | ||
|
|
1c19b6de1e | ||
|
|
85bed0bdf1 | ||
|
|
0102796769 | ||
|
|
50b2a72f1b | ||
|
|
04cc239756 | ||
|
|
0c20da2481 | ||
|
|
72d2a563f3 | ||
|
|
6f3f8c944d | ||
|
|
ed60fd2e82 | ||
|
|
636fef4693 | ||
|
|
77775e2f85 | ||
|
|
b260959a6e | ||
|
|
2a04f7b378 | ||
|
|
d4766f9c81 | ||
|
|
7f4c9b5b11 | ||
|
|
70f7c8ce89 | ||
|
|
f33a6aba39 | ||
|
|
236d717f10 | ||
|
|
0fc160f57c | ||
|
|
666023d89a | ||
|
|
8c4f2991c7 | ||
|
|
4453dff36d | ||
|
|
90b043561f |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -16,6 +16,8 @@ lerna-debug.log*
|
||||
/docker-compose-data
|
||||
/docker-data
|
||||
|
||||
docker-compose.yaml
|
||||
|
||||
# Package
|
||||
/yarn.lock
|
||||
/package-lock.json
|
||||
|
||||
116
CHANGELOG.md
116
CHANGELOG.md
@@ -1,4 +1,118 @@
|
||||
# 1.2.0 (homolog)
|
||||
# 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)
|
||||
|
||||
### Features
|
||||
|
||||
* Added messages.delete event
|
||||
* Added restart instance endpoint
|
||||
* Created automation for creating instances in the chatwoot bot with the command '#inbox_whatsapp:{INSTANCE_NAME}
|
||||
* Change Baileys version to: 6.4.0
|
||||
* Send contact in chatwoot
|
||||
* Send contact array in chatwoot
|
||||
* Added apiKey in webhook and serverUrl in fetchInstance if EXPOSE_IN_FETCH_INSTANCES: true
|
||||
* Translation set to default (english) in chatwoot
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed error to send message in large groups
|
||||
* Docker files adjusted
|
||||
* Fixed in the postman collection the webhookByEvent parameter by webhook_by_events
|
||||
* Added validations in create instance
|
||||
* Removed link preview endpoint, now it's done automatically from sending conventional text
|
||||
* Added group membership validation before sending message to groups
|
||||
* Adjusts in docker files
|
||||
* Adjusts in returns in endpoints chatwoot and webhook
|
||||
* Fixed ghost mentions in send text message
|
||||
* Fixed bug that saved contacts from groups came without number in chatwoot
|
||||
* Fixed problem to receive csat in chatwoot
|
||||
* Fixed require fileName for document only in base64 for send media message
|
||||
* Bug fix when sending mobile message change contact name to number in chatwoot
|
||||
* Bug fix when connecting whatsapp does not send confirmation message
|
||||
* Fixed quoted message with id or message directly
|
||||
* Adjust in validation for mexican and argentine numbers
|
||||
* Adjust in create store files
|
||||
|
||||
### Integrations
|
||||
|
||||
- Chatwoot: v2.18.0
|
||||
|
||||
# 1.2.2 (2023-07-15 09:36)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Tweak in route "/" with version info
|
||||
* Adjusts chatwoot version
|
||||
|
||||
### Integrations
|
||||
|
||||
- Chatwoot: v2.18.0
|
||||
|
||||
# 1.2.1 (2023-07-14 19:04)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Adjusts in docker files
|
||||
* Save picture url groups in chatwoot
|
||||
|
||||
# 1.2.0 (2023-07-14 15:28)
|
||||
|
||||
### Features
|
||||
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
SERVER_URL='<url>' # ex.: http://localhost:3333
|
||||
# Server URL - Set your application url
|
||||
SERVER_URL=http://localhost:8080
|
||||
|
||||
CORS_ORIGIN='*' # Or separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'
|
||||
CORS_METHODS='POST,GET,PUT,DELETE'
|
||||
# Cors - * for all or set separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'
|
||||
CORS_ORIGIN=*
|
||||
CORS_METHODS=POST,GET,PUT,DELETE
|
||||
CORS_CREDENTIALS=true
|
||||
|
||||
# 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_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.
|
||||
# Default time: 5 minutes
|
||||
@@ -20,16 +23,17 @@ STORE_MESSAGE_UP=true
|
||||
STORE_CONTACTS=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_MESSAGE_UP=true
|
||||
CLEAN_STORE_CONTACTS=true
|
||||
CLEAN_STORE_CHATS=true
|
||||
|
||||
# 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_DB_PREFIX_NAME=evolution
|
||||
DATABASE_CONNECTION_DB_PREFIX_NAME=evdocker
|
||||
|
||||
# Choose the data you want to save in the application's database or store
|
||||
DATABASE_SAVE_DATA_INSTANCE=false
|
||||
@@ -38,14 +42,14 @@ DATABASE_SAVE_MESSAGE_UPDATE=false
|
||||
DATABASE_SAVE_DATA_CONTACTS=false
|
||||
DATABASE_SAVE_DATA_CHATS=false
|
||||
|
||||
REDIS_ENABLED=false
|
||||
REDIS_ENABLED=true
|
||||
REDIS_URI=redis://redis:6379
|
||||
REDIS_PREFIX_KEY=evolution
|
||||
REDIS_PREFIX_KEY=evdocker
|
||||
|
||||
# Global Webhook Settings
|
||||
# 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
|
||||
WEBHOOK_GLOBAL_URL='<url>'
|
||||
WEBHOOK_GLOBAL_URL=''
|
||||
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
|
||||
WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=false
|
||||
@@ -55,6 +59,7 @@ WEBHOOK_EVENTS_QRCODE_UPDATED=true
|
||||
WEBHOOK_EVENTS_MESSAGES_SET=true
|
||||
WEBHOOK_EVENTS_MESSAGES_UPSERT=true
|
||||
WEBHOOK_EVENTS_MESSAGES_UPDATE=true
|
||||
WEBHOOK_EVENTS_MESSAGES_DELETE=true
|
||||
WEBHOOK_EVENTS_SEND_MESSAGE=true
|
||||
WEBHOOK_EVENTS_CONTACTS_SET=true
|
||||
WEBHOOK_EVENTS_CONTACTS_UPSERT=true
|
||||
@@ -72,8 +77,9 @@ WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
||||
WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
||||
|
||||
# Name that will be displayed on smartphone connection
|
||||
CONFIG_SESSION_PHONE_CLIENT='Evolution API'
|
||||
CONFIG_SESSION_PHONE_NAME=chrome # chrome | firefox | edge | opera | safari
|
||||
CONFIG_SESSION_PHONE_CLIENT=EvolutionAPI
|
||||
# Browser Name = chrome | firefox | edge | opera | safari
|
||||
CONFIG_SESSION_PHONE_NAME=chrome
|
||||
|
||||
# Set qrcode display limit
|
||||
QRCODE_LIMIT=30
|
||||
@@ -81,20 +87,13 @@ QRCODE_LIMIT=30
|
||||
# Defines an authentication type for the api
|
||||
# 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
|
||||
AUTHENTICATION_TYPE='apikey' # jwt or 'apikey'
|
||||
# jwt or 'apikey'
|
||||
AUTHENTICATION_TYPE=apikey
|
||||
## Define a global apikey to access all instances.
|
||||
### 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
|
||||
## 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
|
||||
AUTHENTICATION_JWT_SECRET='L0YWtjb2w554WFqPG'
|
||||
# Set the instance name and webhook url to create an instance in init the application
|
||||
# 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>'
|
||||
# seconds - 3600s ===1h | zero (0) - never expires
|
||||
AUTHENTICATION_JWT_EXPIRIN_IN=0
|
||||
AUTHENTICATION_JWT_SECRET='L=0YWt]b2w[WF>#>:&E`'
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
version: '3.3'
|
||||
|
||||
networks:
|
||||
evolution-net:
|
||||
driver: bridge
|
||||
|
||||
services:
|
||||
mongodb:
|
||||
container_name: mongodb
|
||||
@@ -19,11 +15,27 @@ services:
|
||||
volumes:
|
||||
- evolution_mongodb_data:/data/db
|
||||
- evolution_mongodb_configdb:/data/configdb
|
||||
networks:
|
||||
- evolution-net
|
||||
expose:
|
||||
- 27017
|
||||
|
||||
mongo-express:
|
||||
image: mongo-express
|
||||
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
|
||||
|
||||
volumes:
|
||||
evolution_mongodb_data:
|
||||
evolution_mongodb_configdb:
|
||||
evolution_mongodb_configdb:
|
||||
|
||||
networks:
|
||||
evolution-net:
|
||||
external: true
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
version: '3.3'
|
||||
|
||||
networks:
|
||||
evolution-net:
|
||||
driver: bridge
|
||||
|
||||
services:
|
||||
redis:
|
||||
image: redis:latest
|
||||
@@ -16,8 +12,10 @@ services:
|
||||
- evolution_redis:/data
|
||||
ports:
|
||||
- 6379:6379
|
||||
networks:
|
||||
- evolution-net
|
||||
|
||||
|
||||
volumes:
|
||||
evolution_redis:
|
||||
|
||||
networks:
|
||||
evolution-net:
|
||||
external: true
|
||||
|
||||
130
Dockerfile
130
Dockerfile
@@ -13,86 +13,90 @@ COPY ./package.json .
|
||||
|
||||
ENV DOCKER_ENV=true
|
||||
|
||||
ENV SERVER_TYPE="http"
|
||||
ENV SERVER_PORT=8080
|
||||
ENV SERVER_URL=$SERVER_URL
|
||||
ENV SERVER_URL=http://localhost:8080
|
||||
|
||||
ENV CORS_ORIGIN="*"
|
||||
ENV CORS_METHODS="POST,GET,PUT,DELETE"
|
||||
ENV CORS_ORIGIN=*
|
||||
ENV CORS_METHODS=POST,GET,PUT,DELETE
|
||||
ENV CORS_CREDENTIALS=true
|
||||
|
||||
ENV LOG_LEVEL=$LOG_LEVEL
|
||||
ENV LOG_COLOR=$LOG_COLOR
|
||||
ENV LOG_LEVEL=ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK,WEBHOOKS
|
||||
ENV LOG_COLOR=true
|
||||
ENV LOG_BAILEYS=error
|
||||
|
||||
ENV DEL_INSTANCE=$DEL_INSTANCE
|
||||
ENV DEL_INSTANCE=false
|
||||
|
||||
ENV STORE_MESSAGES=$STORE_MESSAGE
|
||||
ENV STORE_MESSAGE_UP=$STORE_MESSAGE_UP
|
||||
ENV STORE_CONTACTS=$STORE_CONTACTS
|
||||
ENV STORE_CHATS=$STORE_CHATS
|
||||
ENV STORE_MESSAGES=true
|
||||
ENV STORE_MESSAGE_UP=true
|
||||
ENV STORE_CONTACTS=true
|
||||
ENV STORE_CHATS=true
|
||||
|
||||
ENV CLEAN_STORE_CLEANING_INTERVAL=$CLEAN_STORE_CLEANING_INTERVAL
|
||||
ENV CLEAN_STORE_MESSAGES=$CLEAN_STORE_MESSAGE
|
||||
ENV CLEAN_STORE_MESSAGE_UP=$CLEAN_STORE_MESSAGE_UP
|
||||
ENV CLEAN_STORE_CONTACTS=$CLEAN_STORE_CONTACTS
|
||||
ENV CLEAN_STORE_CHATS=$CLEAN_STORE_CHATS
|
||||
ENV CLEAN_STORE_CLEANING_INTERVAL=7200
|
||||
ENV CLEAN_STORE_MESSAGES=true
|
||||
ENV CLEAN_STORE_MESSAGE_UP=true
|
||||
ENV CLEAN_STORE_CONTACTS=true
|
||||
ENV CLEAN_STORE_CHATS=true
|
||||
|
||||
ENV DATABASE_ENABLED=$DATABASE_ENABLED
|
||||
ENV DATABASE_CONNECTION_URI=$DATABASE_CONNECTION_URI
|
||||
ENV DATABASE_CONNECTION_DB_PREFIX_NAME=$DATABASE_CONNECTION_DB_PREFIX_NAME
|
||||
ENV DATABASE_SAVE_DATA_INSTANCE=$DATABASE_SAVE_DATA_INSTANCE
|
||||
ENV DATABASE_SAVE_DATA_NEW_MESSAGE=$DATABASE_SAVE_DATA_NEW_MESSAGE
|
||||
ENV DATABASE_SAVE_MESSAGE_UPDATE=$DATABASE_SAVE_MESSAGE_UPDATE
|
||||
ENV DATABASE_SAVE_DATA_CONTACTS=$DATABASE_SAVE_DATA_CONTACTS
|
||||
ENV DATABASE_SAVE_DATA_CHATS=$DATABASE_SAVE_DATA_CHATS
|
||||
ENV DATABASE_ENABLED=false
|
||||
ENV DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true
|
||||
ENV DATABASE_CONNECTION_DB_PREFIX_NAME=evolution
|
||||
|
||||
ENV REDIS_ENABLED=$REDIS_ENABLED
|
||||
ENV REDIS_URI=$REDIS_URI
|
||||
ENV REDIS_PREFIX_KEY=$REDIS_PREFIX_KEY
|
||||
ENV DATABASE_SAVE_DATA_INSTANCE=false
|
||||
ENV DATABASE_SAVE_DATA_NEW_MESSAGE=false
|
||||
ENV DATABASE_SAVE_MESSAGE_UPDATE=false
|
||||
ENV DATABASE_SAVE_DATA_CONTACTS=false
|
||||
ENV DATABASE_SAVE_DATA_CHATS=false
|
||||
|
||||
ENV WEBHOOK_GLOBAL_URL=$WEBHOOK_GLOBAL_URL
|
||||
ENV WEBHOOK_GLOBAL_ENABLED=$WEBHOOK_GLOBAL_ENABLED
|
||||
ENV WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=$WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS
|
||||
ENV REDIS_ENABLED=false
|
||||
ENV REDIS_URI=redis://redis:6379
|
||||
ENV REDIS_PREFIX_KEY=evolution
|
||||
|
||||
ENV WEBHOOK_EVENTS_APPLICATION_STARTUP=$WEBHOOK_EVENTS_APPLICATION_STARTUP
|
||||
ENV WEBHOOK_EVENTS_QRCODE_UPDATED=$WEBHOOK_EVENTS_QRCODE_UPDATED
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_SET=$WEBHOOK_EVENTS_MESSAGES_SET
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_UPSERT=$WEBHOOK_EVENTS_MESSAGES_UPSERT
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_UPDATE=$WEBHOOK_EVENTS_MESSAGES_UPDATE
|
||||
ENV WEBHOOK_EVENTS_SEND_MESSAGE=$WEBHOOK_EVENTS_SEND_MESSAGE
|
||||
ENV WEBHOOK_EVENTS_CONTACTS_SET=$WEBHOOK_EVENTS_CONTACTS_SET
|
||||
ENV WEBHOOK_EVENTS_CONTACTS_UPSERT=$WEBHOOK_EVENTS_CONTACTS_UPSERT
|
||||
ENV WEBHOOK_EVENTS_CONTACTS_UPDATE=$WEBHOOK_EVENTS_CONTACTS_UPDATE
|
||||
ENV WEBHOOK_EVENTS_PRESENCE_UPDATE=$WEBHOOK_EVENTS_PRESENCE_UPDATE
|
||||
ENV WEBHOOK_EVENTS_CHATS_SET=$WEBHOOK_EVENTS_CHATS_SET
|
||||
ENV WEBHOOK_EVENTS_CHATS_UPSERT=$WEBHOOK_EVENTS_CHATS_UPSERT
|
||||
ENV WEBHOOK_EVENTS_CHATS_UPDATE=$WEBHOOK_EVENTS_CHATS_UPDATE
|
||||
ENV WEBHOOK_EVENTS_CONNECTION_UPDATE=$WEBHOOK_EVENTS_CONNECTION_UPDATE
|
||||
ENV WEBHOOK_EVENTS_GROUPS_UPSERT=$WEBHOOK_EVENTS_GROUPS_UPSERT
|
||||
ENV WEBHOOK_EVENTS_GROUPS_UPDATE=$WEBHOOK_EVENTS_GROUPS_UPDATE
|
||||
ENV WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=$WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE
|
||||
ENV WEBHOOK_GLOBAL_URL=<url>
|
||||
ENV WEBHOOK_GLOBAL_ENABLED=false
|
||||
|
||||
ENV WEBHOOK_EVENTS_NEW_JWT_TOKEN=$WEBHOOK_EVENTS_NEW_JWT_TOKEN
|
||||
ENV WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=false
|
||||
|
||||
ENV CONFIG_SESSION_PHONE_CLIENT=$CONFIG_SESSION_PHONE_CLIENT
|
||||
ENV CONFIG_SESSION_PHONE_NAME=$CONFIG_SESSION_PHONE_NAME
|
||||
ENV WEBHOOK_EVENTS_APPLICATION_STARTUP=false
|
||||
ENV WEBHOOK_EVENTS_QRCODE_UPDATED=true
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_SET=true
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_UPSERT=true
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_UPDATE=true
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_DELETE=true
|
||||
ENV WEBHOOK_EVENTS_SEND_MESSAGE=true
|
||||
ENV WEBHOOK_EVENTS_CONTACTS_SET=true
|
||||
ENV WEBHOOK_EVENTS_CONTACTS_UPSERT=true
|
||||
ENV WEBHOOK_EVENTS_CONTACTS_UPDATE=true
|
||||
ENV WEBHOOK_EVENTS_PRESENCE_UPDATE=true
|
||||
ENV WEBHOOK_EVENTS_CHATS_SET=true
|
||||
ENV WEBHOOK_EVENTS_CHATS_UPSERT=true
|
||||
ENV WEBHOOK_EVENTS_CHATS_UPDATE=true
|
||||
ENV WEBHOOK_EVENTS_CHATS_DELETE=true
|
||||
ENV WEBHOOK_EVENTS_GROUPS_UPSERT=true
|
||||
ENV WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
||||
ENV WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
||||
ENV WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
||||
|
||||
ENV QRCODE_LIMIT=$QRCODE_LIMIT
|
||||
ENV WEBHOOK_EVENTS_NEW_JWT_TOKEN=false
|
||||
|
||||
ENV AUTHENTICATION_TYPE=$AUTHENTICATION_TYPE
|
||||
ENV CONFIG_SESSION_PHONE_CLIENT=EvolutionAPI
|
||||
ENV CONFIG_SESSION_PHONE_NAME=chrome
|
||||
|
||||
ENV AUTHENTICATION_API_KEY=$AUTHENTICATION_API_KEY
|
||||
ENV AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=$AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES
|
||||
ENV QRCODE_LIMIT=30
|
||||
|
||||
ENV AUTHENTICATION_JWT_EXPIRIN_IN=$AUTHENTICATION_JWT_EXPIRIN_IN
|
||||
ENV AUTHENTICATION_JWT_SECRET="L=0YWt]b2w[WF>#>:&E`"
|
||||
ENV AUTHENTICATION_TYPE=apikey
|
||||
|
||||
ENV AUTHENTICATION_INSTANCE_NAME=$AUTHENTICATION_INSTANCE_NAME
|
||||
ENV AUTHENTICATION_INSTANCE_WEBHOOK_URL=$AUTHENTICATION_INSTANCE_WEBHOOK_URL
|
||||
ENV AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID=$AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID
|
||||
ENV AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN=$AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN
|
||||
ENV AUTHENTICATION_INSTANCE_CHATWOOT_URL=$AUTHENTICATION_INSTANCE_CHATWOOT_URL
|
||||
ENV AUTHENTICATION_INSTANCE_MODE=$AUTHENTICATION_INSTANCE_MODE
|
||||
ENV AUTHENTICATION_API_KEY=B6D711FCDE4D4FD5936544120E713976
|
||||
ENV AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true
|
||||
|
||||
ENV AUTHENTICATION_JWT_EXPIRIN_IN=0
|
||||
ENV AUTHENTICATION_JWT_SECRET='L=0YWt]b2w[WF>#>:&E`'
|
||||
|
||||
ENV AUTHENTICATION_INSTANCE_MODE=server
|
||||
|
||||
ENV AUTHENTICATION_INSTANCE_NAME=evolution
|
||||
ENV AUTHENTICATION_INSTANCE_WEBHOOK_URL=<url>
|
||||
ENV AUTHENTICATION_INSTANCE_CHATWOOT_ACCOUNT_ID=1
|
||||
ENV AUTHENTICATION_INSTANCE_CHATWOOT_TOKEN=123456
|
||||
ENV AUTHENTICATION_INSTANCE_CHATWOOT_URL=<url>
|
||||
|
||||
RUN npm install
|
||||
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
version: '3.3'
|
||||
|
||||
networks:
|
||||
evolution-net:
|
||||
driver: bridge
|
||||
|
||||
services:
|
||||
api:
|
||||
container_name: evolution_api
|
||||
@@ -14,14 +10,19 @@ services:
|
||||
volumes:
|
||||
- evolution_instances:/evolution/instances
|
||||
- evolution_store:/evolution/store
|
||||
networks:
|
||||
- evolution-net
|
||||
env_file:
|
||||
- ./Docker/.env
|
||||
command: ['node', './dist/src/main.js']
|
||||
networks:
|
||||
- evolution-net
|
||||
expose:
|
||||
- 8080
|
||||
|
||||
volumes:
|
||||
evolution_instances:
|
||||
evolution_store:
|
||||
evolution_store:
|
||||
|
||||
networks:
|
||||
evolution-net:
|
||||
external: true
|
||||
|
||||
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",
|
||||
"version": "1.2.0",
|
||||
"version": "1.4.1",
|
||||
"description": "Rest api for communication with WhatsApp",
|
||||
"main": "./dist/src/main.js",
|
||||
"scripts": {
|
||||
@@ -42,9 +42,10 @@
|
||||
"dependencies": {
|
||||
"@adiwajshing/keyed-db": "^0.2.4",
|
||||
"@ffmpeg-installer/ffmpeg": "^1.1.0",
|
||||
"@hapi/boom": "^10.0.1",
|
||||
"@whiskeysockets/baileys": "github:vphelipe/WhiskeySockets-Baileys#master",
|
||||
"@figuro/chatwoot-sdk": "^1.1.14",
|
||||
"@hapi/boom": "^10.0.1",
|
||||
"@sentry/node": "^7.59.2",
|
||||
"@whiskeysockets/baileys": "^6.4.0",
|
||||
"axios": "^1.3.5",
|
||||
"class-validator": "^0.13.2",
|
||||
"compression": "^1.7.4",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { load } from 'js-yaml';
|
||||
import { join } from 'path';
|
||||
import { SRC_DIR } from './path.config';
|
||||
import { isBooleanString } from 'class-validator';
|
||||
|
||||
export type HttpServer = { TYPE: 'http' | 'https'; PORT: number; URL: string };
|
||||
@@ -76,6 +75,7 @@ export type EventsWebhook = {
|
||||
MESSAGES_SET: boolean;
|
||||
MESSAGES_UPSERT: boolean;
|
||||
MESSAGES_UPDATE: boolean;
|
||||
MESSAGES_DELETE: boolean;
|
||||
SEND_MESSAGE: boolean;
|
||||
CONTACTS_SET: boolean;
|
||||
CONTACTS_UPDATE: boolean;
|
||||
@@ -94,20 +94,12 @@ export type EventsWebhook = {
|
||||
|
||||
export type ApiKey = { KEY: 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 = {
|
||||
API_KEY: ApiKey;
|
||||
EXPOSE_IN_FETCH_INSTANCES: boolean;
|
||||
JWT: Jwt;
|
||||
TYPE: 'jwt' | 'apikey';
|
||||
INSTANCE: Instance;
|
||||
};
|
||||
|
||||
export type DelInstance = number | boolean;
|
||||
@@ -193,7 +185,7 @@ export class ConfigService {
|
||||
CLEAN_STORE: {
|
||||
CLEANING_INTERVAL: Number.isInteger(process.env?.CLEAN_STORE_CLEANING_TERMINAL)
|
||||
? Number.parseInt(process.env.CLEAN_STORE_CLEANING_TERMINAL)
|
||||
: undefined,
|
||||
: 7200,
|
||||
MESSAGES: process.env?.CLEAN_STORE_MESSAGES === 'true',
|
||||
MESSAGE_UP: process.env?.CLEAN_STORE_MESSAGE_UP === 'true',
|
||||
CONTACTS: process.env?.CLEAN_STORE_CONTACTS === 'true',
|
||||
@@ -225,7 +217,7 @@ export class ConfigService {
|
||||
},
|
||||
DEL_INSTANCE: isBooleanString(process.env?.DEL_INSTANCE)
|
||||
? process.env.DEL_INSTANCE === 'true'
|
||||
: Number.parseInt(process.env.DEL_INSTANCE),
|
||||
: Number.parseInt(process.env.DEL_INSTANCE) || false,
|
||||
WEBHOOK: {
|
||||
GLOBAL: {
|
||||
URL: process.env?.WEBHOOK_GLOBAL_URL,
|
||||
@@ -238,6 +230,7 @@ export class ConfigService {
|
||||
MESSAGES_SET: process.env?.WEBHOOK_EVENTS_MESSAGES_SET === 'true',
|
||||
MESSAGES_UPSERT: process.env?.WEBHOOK_EVENTS_MESSAGES_UPSERT === 'true',
|
||||
MESSAGES_UPDATE: process.env?.WEBHOOK_EVENTS_MESSAGES_UPDATE === 'true',
|
||||
MESSAGES_DELETE: process.env?.WEBHOOK_EVENTS_MESSAGES_DELETE === 'true',
|
||||
SEND_MESSAGE: process.env?.WEBHOOK_EVENTS_SEND_MESSAGE === 'true',
|
||||
CONTACTS_SET: process.env?.WEBHOOK_EVENTS_CONTACTS_SET === 'true',
|
||||
CONTACTS_UPDATE: process.env?.WEBHOOK_EVENTS_CONTACTS_UPDATE === 'true',
|
||||
@@ -256,11 +249,11 @@ export class ConfigService {
|
||||
},
|
||||
},
|
||||
CONFIG_SESSION_PHONE: {
|
||||
CLIENT: process.env?.CONFIG_SESSION_PHONE_CLIENT,
|
||||
NAME: process.env?.CONFIG_SESSION_PHONE_NAME,
|
||||
CLIENT: process.env?.CONFIG_SESSION_PHONE_CLIENT || 'Evolution API',
|
||||
NAME: process.env?.CONFIG_SESSION_PHONE_NAME || 'chrome',
|
||||
},
|
||||
QRCODE: {
|
||||
LIMIT: Number.parseInt(process.env.QRCODE_LIMIT),
|
||||
LIMIT: Number.parseInt(process.env.QRCODE_LIMIT) || 30,
|
||||
},
|
||||
AUTHENTICATION: {
|
||||
TYPE: process.env.AUTHENTICATION_TYPE as 'jwt',
|
||||
@@ -275,15 +268,6 @@ export class ConfigService {
|
||||
: 3600,
|
||||
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 || '',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ LOG:
|
||||
- DARK
|
||||
- WEBHOOKS
|
||||
COLOR: true
|
||||
BAILEYS: error # "fatal" | "error" | "warn" | "info" | "debug" | "trace"
|
||||
BAILEYS: error # fatal | error | warn | info | debug | trace
|
||||
|
||||
# Determine how long the instance should be deleted from memory in case of no connection.
|
||||
# Default time: 5 minutes
|
||||
@@ -96,6 +96,7 @@ WEBHOOK:
|
||||
MESSAGES_SET: true
|
||||
MESSAGES_UPSERT: true
|
||||
MESSAGES_UPDATE: true
|
||||
MESSAGES_DELETE: true
|
||||
SEND_MESSAGE: true
|
||||
CONTACTS_SET: true
|
||||
CONTACTS_UPSERT: true
|
||||
@@ -135,14 +136,4 @@ AUTHENTICATION:
|
||||
# Set the secret key to encrypt and decrypt your token and its expiration time.
|
||||
JWT:
|
||||
EXPIRIN_IN: 0 # seconds - 3600s === 1h | zero (0) - never expires
|
||||
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>
|
||||
SECRET: L=0YWt]b2w[WF>#>:&E`
|
||||
29
src/main.ts
29
src/main.ts
@@ -10,6 +10,7 @@ import { waMonitor } from './whatsapp/whatsapp.module';
|
||||
import { HttpStatus, router } from './whatsapp/routers/index.router';
|
||||
import 'express-async-errors';
|
||||
import { ServerUP } from './utils/server-up';
|
||||
import * as Sentry from '@sentry/node';
|
||||
|
||||
function initWA() {
|
||||
waMonitor.loadInstance();
|
||||
@@ -19,6 +20,27 @@ function bootstrap() {
|
||||
const logger = new Logger('SERVER');
|
||||
const app = express();
|
||||
|
||||
// Sentry.init({
|
||||
// dsn: '',
|
||||
// integrations: [
|
||||
// // enable HTTP calls tracing
|
||||
// new Sentry.Integrations.Http({ tracing: true }),
|
||||
// // enable Express.js middleware tracing
|
||||
// new Sentry.Integrations.Express({ app }),
|
||||
// // Automatically instrument Node.js libraries and frameworks
|
||||
// ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
|
||||
// ],
|
||||
|
||||
// // Set tracesSampleRate to 1.0 to capture 100%
|
||||
// // of transactions for performance monitoring.
|
||||
// // We recommend adjusting this value in production
|
||||
// tracesSampleRate: 1.0,
|
||||
// });
|
||||
|
||||
// app.use(Sentry.Handlers.requestHandler());
|
||||
|
||||
// app.use(Sentry.Handlers.tracingHandler());
|
||||
|
||||
app.use(
|
||||
cors({
|
||||
origin(requestOrigin, callback) {
|
||||
@@ -43,6 +65,13 @@ function bootstrap() {
|
||||
|
||||
app.use('/', router);
|
||||
|
||||
// app.use(Sentry.Handlers.errorHandler());
|
||||
|
||||
// app.use(function onError(err, req, res, next) {
|
||||
// res.statusCode = 500;
|
||||
// res.end(res.sentry + '\n');
|
||||
// });
|
||||
|
||||
app.use(
|
||||
(err: Error, req: Request, res: Response, next: NextFunction) => {
|
||||
if (err) {
|
||||
|
||||
@@ -39,6 +39,7 @@ export const instanceNameSchema: JSONSchema7 = {
|
||||
'MESSAGES_SET',
|
||||
'MESSAGES_UPSERT',
|
||||
'MESSAGES_UPDATE',
|
||||
'MESSAGES_DELETE',
|
||||
'SEND_MESSAGE',
|
||||
'CONTACTS_SET',
|
||||
'CONTACTS_UPSERT',
|
||||
@@ -57,6 +58,7 @@ export const instanceNameSchema: JSONSchema7 = {
|
||||
},
|
||||
},
|
||||
qrcode: { type: 'boolean', enum: [true, false] },
|
||||
number: { type: 'string', pattern: '^\\d+[\\.@\\w-]+' },
|
||||
token: { type: 'string' },
|
||||
},
|
||||
...isNotEmpty('instanceName'),
|
||||
@@ -81,8 +83,8 @@ const quotedOptionsSchema: JSONSchema7 = {
|
||||
remoteJid: { type: 'string' },
|
||||
fromMe: { type: 'boolean', enum: [true, false] },
|
||||
},
|
||||
required: ['id', 'remoteJid', 'fromMe'],
|
||||
...isNotEmpty('id', 'remoteJid'),
|
||||
required: ['id'],
|
||||
...isNotEmpty('id'),
|
||||
},
|
||||
message: { type: 'object' },
|
||||
},
|
||||
@@ -122,7 +124,6 @@ const optionsSchema: JSONSchema7 = {
|
||||
|
||||
const numberDefinition: JSONSchema7Definition = {
|
||||
type: 'string',
|
||||
pattern: '^\\d+[\\.@\\w-]+',
|
||||
description: 'Invalid format',
|
||||
};
|
||||
|
||||
@@ -144,24 +145,6 @@ export const textMessageSchema: JSONSchema7 = {
|
||||
required: ['textMessage', 'number'],
|
||||
};
|
||||
|
||||
export const linkPreviewSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
number: { ...numberDefinition },
|
||||
options: { ...optionsSchema },
|
||||
linkPreview: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
text: { type: 'string' },
|
||||
},
|
||||
required: ['text'],
|
||||
...isNotEmpty('text'),
|
||||
},
|
||||
},
|
||||
required: ['linkPreview', 'number'],
|
||||
};
|
||||
|
||||
export const pollMessageSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
@@ -415,7 +398,7 @@ export const contactMessageSchema: JSONSchema7 = {
|
||||
email: { type: 'string' },
|
||||
url: { type: 'string' },
|
||||
},
|
||||
required: ['fullName', 'wuid', 'phoneNumber'],
|
||||
required: ['fullName', 'phoneNumber'],
|
||||
...isNotEmpty('fullName'),
|
||||
},
|
||||
minItems: 1,
|
||||
@@ -462,7 +445,6 @@ export const whatsappNumberSchema: JSONSchema7 = {
|
||||
uniqueItems: true,
|
||||
items: {
|
||||
type: 'string',
|
||||
pattern: '^\\d+',
|
||||
description: '"numbers" must be an array of numeric strings',
|
||||
},
|
||||
},
|
||||
@@ -604,6 +586,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 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
@@ -846,6 +839,7 @@ export const webhookSchema: JSONSchema7 = {
|
||||
'MESSAGES_SET',
|
||||
'MESSAGES_UPSERT',
|
||||
'MESSAGES_UPDATE',
|
||||
'MESSAGES_DELETE',
|
||||
'SEND_MESSAGE',
|
||||
'CONTACTS_SET',
|
||||
'CONTACTS_UPSERT',
|
||||
@@ -881,3 +875,15 @@ export const chatwootSchema: JSONSchema7 = {
|
||||
required: ['enabled', 'account_id', 'token', 'url', 'sign_msg'],
|
||||
...isNotEmpty('account_id', 'token', 'url', 'sign_msg'),
|
||||
};
|
||||
|
||||
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] },
|
||||
},
|
||||
required: ['reject_call'],
|
||||
...isNotEmpty('reject_call'),
|
||||
};
|
||||
|
||||
@@ -160,8 +160,6 @@ export abstract class RouterBroker {
|
||||
|
||||
const v = validate(ref, schema);
|
||||
|
||||
console.log(v, '@checkei aqui');
|
||||
|
||||
if (!v.valid) {
|
||||
const message: any[] = v.errors.map(({ property, stack, schema }) => {
|
||||
let message: string;
|
||||
@@ -203,8 +201,6 @@ export abstract class RouterBroker {
|
||||
|
||||
const v = validate(ref, schema);
|
||||
|
||||
console.log(v, '@checkei aqui');
|
||||
|
||||
if (!v.valid) {
|
||||
const message: any[] = v.errors.map(({ property, stack, schema }) => {
|
||||
let message: string;
|
||||
|
||||
@@ -48,6 +48,14 @@ export class ChatController {
|
||||
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) {
|
||||
logger.verbose('requested fetchContacts from ' + instanceName + ' instance');
|
||||
return await this.waMonitor.waInstances[instanceName].fetchContacts(query);
|
||||
|
||||
@@ -33,7 +33,7 @@ export class ChatwootController {
|
||||
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');
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,18 @@ export class ChatwootController {
|
||||
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
if (Object.keys(result).length === 0) {
|
||||
return {
|
||||
enabled: false,
|
||||
url: '',
|
||||
account_id: '',
|
||||
token: '',
|
||||
sign_msg: false,
|
||||
name_inbox: '',
|
||||
webhook_url: '',
|
||||
};
|
||||
}
|
||||
|
||||
const response = {
|
||||
...result,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
@@ -78,8 +90,14 @@ export class ChatwootController {
|
||||
logger.verbose(
|
||||
'requested receiveWebhook from ' + instance.instanceName + ' instance',
|
||||
);
|
||||
const chatwootService = new ChatwootService(waMonitor);
|
||||
const chatwootService = new ChatwootService(waMonitor, this.configService);
|
||||
|
||||
return chatwootService.receiveWebhook(instance, data);
|
||||
}
|
||||
|
||||
public async newInstance(data: any) {
|
||||
const chatwootService = new ChatwootService(waMonitor, this.configService);
|
||||
|
||||
return chatwootService.newInstance(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import { ChatwootService } from '../services/chatwoot.service';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { wa } from '../types/wa.types';
|
||||
import { RedisCache } from '../../db/redis.client';
|
||||
import { isURL } from 'class-validator';
|
||||
|
||||
export class InstanceController {
|
||||
constructor(
|
||||
@@ -33,6 +34,7 @@ export class InstanceController {
|
||||
webhook_by_events,
|
||||
events,
|
||||
qrcode,
|
||||
number,
|
||||
token,
|
||||
chatwoot_account_id,
|
||||
chatwoot_token,
|
||||
@@ -41,254 +43,75 @@ export class InstanceController {
|
||||
}: InstanceDto) {
|
||||
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
|
||||
|
||||
const mode = this.configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
|
||||
|
||||
if (mode === 'container') {
|
||||
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,
|
||||
if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) {
|
||||
throw new BadRequestException(
|
||||
'The instance name must be lowercase and without special characters',
|
||||
);
|
||||
instance.instanceName = instanceName;
|
||||
this.logger.verbose('instance: ' + instance.instanceName + ' created');
|
||||
}
|
||||
|
||||
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||
this.waMonitor.delInstanceTime(instance.instanceName);
|
||||
this.logger.verbose('checking duplicate token');
|
||||
await this.authService.checkDuplicateToken(token);
|
||||
|
||||
this.logger.verbose('generating hash');
|
||||
const hash = await this.authService.generateHash(
|
||||
{
|
||||
instanceName: instance.instanceName,
|
||||
},
|
||||
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('hash: ' + hash + ' generated');
|
||||
this.logger.verbose('instance: ' + instance.instanceName + ' created');
|
||||
|
||||
let getEvents: string[];
|
||||
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||
this.waMonitor.delInstanceTime(instance.instanceName);
|
||||
|
||||
if (webhook) {
|
||||
this.logger.verbose('creating webhook');
|
||||
try {
|
||||
this.webhookService.create(instance, {
|
||||
enabled: true,
|
||||
url: webhook,
|
||||
events,
|
||||
webhook_by_events,
|
||||
});
|
||||
this.logger.verbose('generating hash');
|
||||
const hash = await this.authService.generateHash(
|
||||
{
|
||||
instanceName: instance.instanceName,
|
||||
},
|
||||
token,
|
||||
);
|
||||
|
||||
getEvents = (await this.webhookService.find(instance)).events;
|
||||
} catch (error) {
|
||||
this.logger.log(error);
|
||||
}
|
||||
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');
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
this.logger.verbose('creating webhook');
|
||||
try {
|
||||
this.chatwootService.create(instance, {
|
||||
this.webhookService.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,
|
||||
url: webhook,
|
||||
events,
|
||||
webhook_by_events,
|
||||
});
|
||||
|
||||
this.chatwootService.initInstanceChatwoot(
|
||||
instance,
|
||||
instance.instanceName,
|
||||
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
qrcode,
|
||||
);
|
||||
getEvents = (await this.webhookService.find(instance)).events;
|
||||
} 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');
|
||||
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
|
||||
let getQrcode: wa.QrCode;
|
||||
|
||||
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;
|
||||
|
||||
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) {
|
||||
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 (qrcode) {
|
||||
this.logger.verbose('creating qrcode');
|
||||
await instance.connectToWhatsapp(number);
|
||||
await delay(5000);
|
||||
getQrcode = instance.qrCode;
|
||||
}
|
||||
|
||||
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
|
||||
let getQrcode: wa.QrCode;
|
||||
|
||||
if (qrcode) {
|
||||
this.logger.verbose('creating qrcode');
|
||||
await instance.connectToWhatsapp();
|
||||
await delay(2000);
|
||||
getQrcode = instance.qrCode;
|
||||
}
|
||||
|
||||
this.logger.verbose('instance created');
|
||||
this.logger.verbose({
|
||||
instance: {
|
||||
instanceName: instance.instanceName,
|
||||
status: 'created',
|
||||
},
|
||||
hash,
|
||||
webhook,
|
||||
webhook_by_events,
|
||||
events: getEvents,
|
||||
qrcode: getQrcode,
|
||||
});
|
||||
|
||||
return {
|
||||
instance: {
|
||||
instanceName: instance.instanceName,
|
||||
status: 'created',
|
||||
},
|
||||
hash,
|
||||
webhook,
|
||||
webhook_by_events,
|
||||
events: getEvents,
|
||||
qrcode: getQrcode,
|
||||
};
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
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 {
|
||||
const result = {
|
||||
instance: {
|
||||
instanceName: instance.instanceName,
|
||||
status: 'created',
|
||||
@@ -297,20 +120,78 @@ export class InstanceController {
|
||||
webhook,
|
||||
webhook_by_events,
|
||||
events: getEvents,
|
||||
chatwoot: {
|
||||
enabled: true,
|
||||
account_id: chatwoot_account_id,
|
||||
token: chatwoot_token,
|
||||
url: chatwoot_url,
|
||||
sign_msg: chatwoot_sign_msg || false,
|
||||
name_inbox: instance.instanceName,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
},
|
||||
qrcode: getQrcode,
|
||||
};
|
||||
|
||||
this.logger.verbose('instance created');
|
||||
this.logger.verbose(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!chatwoot_account_id) {
|
||||
throw new BadRequestException('account_id is required');
|
||||
}
|
||||
|
||||
if (!chatwoot_token) {
|
||||
throw new BadRequestException('token is required');
|
||||
}
|
||||
|
||||
if (!chatwoot_url) {
|
||||
throw new BadRequestException('url is required');
|
||||
}
|
||||
|
||||
if (!isURL(chatwoot_url, { require_tld: false })) {
|
||||
throw new BadRequestException('Invalid "url" property in chatwoot');
|
||||
}
|
||||
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
try {
|
||||
this.chatwootService.create(instance, {
|
||||
enabled: true,
|
||||
account_id: chatwoot_account_id,
|
||||
token: chatwoot_token,
|
||||
url: chatwoot_url,
|
||||
sign_msg: chatwoot_sign_msg || false,
|
||||
name_inbox: instance.instanceName,
|
||||
number,
|
||||
});
|
||||
|
||||
this.chatwootService.initInstanceChatwoot(
|
||||
instance,
|
||||
instance.instanceName,
|
||||
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
qrcode,
|
||||
number,
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.log(error);
|
||||
}
|
||||
|
||||
return {
|
||||
instance: {
|
||||
instanceName: instance.instanceName,
|
||||
status: 'created',
|
||||
},
|
||||
hash,
|
||||
webhook,
|
||||
webhook_by_events,
|
||||
events: getEvents,
|
||||
chatwoot: {
|
||||
enabled: true,
|
||||
account_id: chatwoot_account_id,
|
||||
token: chatwoot_token,
|
||||
url: chatwoot_url,
|
||||
sign_msg: chatwoot_sign_msg || false,
|
||||
number,
|
||||
name_inbox: instance.instanceName,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public async connectToWhatsapp({ instanceName }: InstanceDto) {
|
||||
public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) {
|
||||
try {
|
||||
this.logger.verbose(
|
||||
'requested connectToWhatsapp from ' + instanceName + ' instance',
|
||||
@@ -321,17 +202,29 @@ export class InstanceController {
|
||||
|
||||
this.logger.verbose('state: ' + state);
|
||||
|
||||
switch (state) {
|
||||
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 });
|
||||
if (state == 'open') {
|
||||
return await this.connectionState({ instanceName });
|
||||
}
|
||||
|
||||
if (state == 'connecting') {
|
||||
return instance.qrCode;
|
||||
}
|
||||
|
||||
if (state == 'close') {
|
||||
this.logger.verbose('connecting');
|
||||
await instance.connectToWhatsapp(number);
|
||||
|
||||
await delay(5000);
|
||||
return instance.qrCode;
|
||||
}
|
||||
|
||||
return {
|
||||
instance: {
|
||||
instanceName: instanceName,
|
||||
status: state,
|
||||
},
|
||||
qrcode: instance?.qrCode,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
@@ -341,24 +234,8 @@ export class InstanceController {
|
||||
try {
|
||||
this.logger.verbose('requested restartInstance from ' + instanceName + ' instance');
|
||||
|
||||
this.logger.verbose('deleting instance: ' + instanceName);
|
||||
delete this.waMonitor.waInstances[instanceName];
|
||||
|
||||
this.logger.verbose('creating instance: ' + instanceName);
|
||||
const instance = new WAStartupService(
|
||||
this.configService,
|
||||
this.eventEmitter,
|
||||
this.repository,
|
||||
this.cache,
|
||||
);
|
||||
|
||||
instance.instanceName = instanceName;
|
||||
|
||||
this.logger.verbose('instance: ' + instance.instanceName + ' created');
|
||||
|
||||
this.logger.verbose('connecting instance: ' + instanceName);
|
||||
await instance.connectToWhatsapp();
|
||||
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||
this.logger.verbose('logging out instance: ' + instanceName);
|
||||
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
||||
|
||||
return { error: false, message: 'Instance restarted' };
|
||||
} catch (error) {
|
||||
@@ -368,7 +245,12 @@ export class InstanceController {
|
||||
|
||||
public async connectionState({ instanceName }: InstanceDto) {
|
||||
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) {
|
||||
@@ -383,9 +265,9 @@ export class InstanceController {
|
||||
|
||||
public async logout({ instanceName }: InstanceDto) {
|
||||
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(
|
||||
'The "' + instanceName + '" instance is not connected',
|
||||
);
|
||||
@@ -408,15 +290,15 @@ export class InstanceController {
|
||||
|
||||
public async deleteInstance({ instanceName }: InstanceDto) {
|
||||
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(
|
||||
'The "' + instanceName + '" instance needs to be disconnected',
|
||||
);
|
||||
}
|
||||
try {
|
||||
if (stateConn.state === 'connecting') {
|
||||
if (instance.state === 'connecting') {
|
||||
this.logger.verbose('logging out instance: ' + instanceName);
|
||||
|
||||
await this.logout({ instanceName });
|
||||
|
||||
@@ -5,7 +5,6 @@ import {
|
||||
SendAudioDto,
|
||||
SendButtonDto,
|
||||
SendContactDto,
|
||||
SendLinkPreviewDto,
|
||||
SendListDto,
|
||||
SendLocationDto,
|
||||
SendMediaDto,
|
||||
@@ -31,9 +30,15 @@ export class SendMessageController {
|
||||
|
||||
public async sendMedia({ instanceName }: InstanceDto, data: SendMediaDto) {
|
||||
logger.verbose('requested sendMedia from ' + instanceName + ' instance');
|
||||
if (isBase64(data?.mediaMessage?.media) && !data?.mediaMessage?.fileName) {
|
||||
throw new BadRequestException('For bse64 the file name must be informed.');
|
||||
|
||||
if (
|
||||
isBase64(data?.mediaMessage?.media) &&
|
||||
!data?.mediaMessage?.fileName &&
|
||||
data?.mediaMessage?.mediatype === 'document'
|
||||
) {
|
||||
throw new BadRequestException('For base64 the file name must be informed.');
|
||||
}
|
||||
|
||||
logger.verbose(
|
||||
'isURL: ' +
|
||||
isURL(data?.mediaMessage?.media) +
|
||||
@@ -119,9 +124,4 @@ export class SendMessageController {
|
||||
logger.verbose('requested sendStatus from ' + instanceName + ' instance');
|
||||
return await this.waMonitor.waInstances[instanceName].statusMessage(data);
|
||||
}
|
||||
|
||||
public async sendLinkPreview({ instanceName }: InstanceDto, data: SendLinkPreviewDto) {
|
||||
logger.verbose('requested sendLinkPreview from ' + instanceName + ' instance');
|
||||
return await this.waMonitor.waInstances[instanceName].linkPreview(data);
|
||||
}
|
||||
}
|
||||
|
||||
29
src/whatsapp/controllers/settings.controller.ts
Normal file
29
src/whatsapp/controllers/settings.controller.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
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',
|
||||
);
|
||||
|
||||
if (data.reject_call && data.msg_call.trim() == '') {
|
||||
throw new BadRequestException('msg_call is required');
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 {
|
||||
name: string;
|
||||
}
|
||||
|
||||
@@ -5,4 +5,5 @@ export class ChatwootDto {
|
||||
url?: string;
|
||||
name_inbox?: string;
|
||||
sign_msg?: boolean;
|
||||
number?: string;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ export class InstanceDto {
|
||||
webhook_by_events?: boolean;
|
||||
events?: string[];
|
||||
qrcode?: boolean;
|
||||
number?: string;
|
||||
token?: string;
|
||||
chatwoot_account_id?: string;
|
||||
chatwoot_token?: string;
|
||||
|
||||
@@ -15,6 +15,8 @@ export class Options {
|
||||
presence?: WAPresence;
|
||||
quoted?: Quoted;
|
||||
mentions?: Mentions;
|
||||
linkPreview?: boolean;
|
||||
encoding?: boolean;
|
||||
}
|
||||
class OptionsMessage {
|
||||
options: Options;
|
||||
@@ -28,10 +30,6 @@ class TextMessage {
|
||||
text: string;
|
||||
}
|
||||
|
||||
class linkPreviewMessage {
|
||||
text: string;
|
||||
}
|
||||
|
||||
export class StatusMessage {
|
||||
type: string;
|
||||
content: string;
|
||||
@@ -52,10 +50,6 @@ export class SendTextDto extends Metadata {
|
||||
textMessage: TextMessage;
|
||||
}
|
||||
|
||||
export class SendLinkPreviewDto extends Metadata {
|
||||
linkPreview: linkPreviewMessage;
|
||||
}
|
||||
|
||||
export class SendStatusDto extends Metadata {
|
||||
statusMessage: StatusMessage;
|
||||
}
|
||||
|
||||
5
src/whatsapp/dto/settings.dto.ts
Normal file
5
src/whatsapp/dto/settings.dto.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export class SettingsDto {
|
||||
reject_call?: boolean;
|
||||
msg_call?: string;
|
||||
groups_ignore?: boolean;
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import {
|
||||
import { InstanceDto } from '../dto/instance.dto';
|
||||
import { cache, waMonitor } from '../whatsapp.module';
|
||||
import { Database, Redis, configService } from '../../config/env.config';
|
||||
import { RedisCache } from '../../db/redis.client';
|
||||
|
||||
async function getInstance(instanceName: string) {
|
||||
const db = configService.get<Database>('DATABASE');
|
||||
|
||||
@@ -9,6 +9,7 @@ export class ChatwootRaw {
|
||||
url?: string;
|
||||
name_inbox?: string;
|
||||
sign_msg?: boolean;
|
||||
number?: string;
|
||||
}
|
||||
|
||||
const chatwootSchema = new Schema<ChatwootRaw>({
|
||||
@@ -19,6 +20,7 @@ const chatwootSchema = new Schema<ChatwootRaw>({
|
||||
url: { type: String, required: true },
|
||||
name_inbox: { type: String, required: true },
|
||||
sign_msg: { type: Boolean, required: true },
|
||||
number: { type: String, required: true },
|
||||
});
|
||||
|
||||
export const ChatwootModel = dbserver?.model(
|
||||
|
||||
@@ -4,3 +4,4 @@ export * from './message.model';
|
||||
export * from './auth.model';
|
||||
export * from './webhook.model';
|
||||
export * from './chatwoot.model';
|
||||
export * from './settings.model';
|
||||
|
||||
23
src/whatsapp/models/settings.model.ts
Normal file
23
src/whatsapp/models/settings.model.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Schema } from 'mongoose';
|
||||
import { dbserver } from '../../db/db.connect';
|
||||
|
||||
export class SettingsRaw {
|
||||
_id?: string;
|
||||
reject_call?: boolean;
|
||||
msg_call?: string;
|
||||
groups_ignore?: 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 },
|
||||
});
|
||||
|
||||
export const SettingsModel = dbserver?.model(
|
||||
SettingsRaw.name,
|
||||
settingsSchema,
|
||||
'settings',
|
||||
);
|
||||
export type ISettingsModel = typeof SettingsModel;
|
||||
@@ -14,6 +14,7 @@ const webhookSchema = new Schema<WebhookRaw>({
|
||||
url: { type: String, required: true },
|
||||
enabled: { type: Boolean, required: true },
|
||||
events: { type: [String], required: true },
|
||||
webhook_by_events: { type: Boolean, required: true },
|
||||
});
|
||||
|
||||
export const WebhookModel = dbserver?.model(WebhookRaw.name, webhookSchema, 'webhook');
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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 { IAuthModel, AuthRaw } from '../models';
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
@@ -5,12 +5,13 @@ import { MessageUpRepository } from './messageUp.repository';
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { WebhookRepository } from './webhook.repository';
|
||||
import { ChatwootRepository } from './chatwoot.repository';
|
||||
import { SettingsRepository } from './settings.repository';
|
||||
|
||||
import { AuthRepository } from './auth.repository';
|
||||
import { Auth, ConfigService, Database } from '../../config/env.config';
|
||||
import { execSync } from 'child_process';
|
||||
import { join } from 'path';
|
||||
import fs from 'fs';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
|
||||
export class RepositoryBroker {
|
||||
constructor(
|
||||
public readonly message: MessageRepository,
|
||||
@@ -19,11 +20,11 @@ export class RepositoryBroker {
|
||||
public readonly messageUpdate: MessageUpRepository,
|
||||
public readonly webhook: WebhookRepository,
|
||||
public readonly chatwoot: ChatwootRepository,
|
||||
public readonly settings: SettingsRepository,
|
||||
public readonly auth: AuthRepository,
|
||||
private configService: ConfigService,
|
||||
dbServer?: MongoClient,
|
||||
) {
|
||||
this.logger.verbose('initializing repository broker');
|
||||
this.dbClient = dbServer;
|
||||
this.__init_repo_without_db__();
|
||||
}
|
||||
@@ -38,39 +39,78 @@ export class RepositoryBroker {
|
||||
private __init_repo_without_db__() {
|
||||
this.logger.verbose('initializing repository without db');
|
||||
if (!this.configService.get<Database>('DATABASE').ENABLED) {
|
||||
this.logger.verbose('database is disabled');
|
||||
|
||||
const storePath = join(process.cwd(), 'store');
|
||||
|
||||
this.logger.verbose('creating store path: ' + storePath);
|
||||
execSync(
|
||||
`mkdir -p ${join(
|
||||
try {
|
||||
const authDir = join(
|
||||
storePath,
|
||||
'auth',
|
||||
this.configService.get<Auth>('AUTHENTICATION').TYPE,
|
||||
)}`,
|
||||
);
|
||||
);
|
||||
const chatsDir = join(storePath, 'chats');
|
||||
const contactsDir = join(storePath, 'contacts');
|
||||
const messagesDir = join(storePath, 'messages');
|
||||
const messageUpDir = join(storePath, 'message-up');
|
||||
const webhookDir = join(storePath, 'webhook');
|
||||
const chatwootDir = join(storePath, 'chatwoot');
|
||||
const settingsDir = join(storePath, 'settings');
|
||||
const tempDir = join(storePath, 'temp');
|
||||
|
||||
this.logger.verbose('creating chats path: ' + join(storePath, 'chats'));
|
||||
execSync(`mkdir -p ${join(storePath, 'chats')}`);
|
||||
if (!fs.existsSync(authDir)) {
|
||||
this.logger.verbose('creating auth dir: ' + authDir);
|
||||
fs.mkdirSync(authDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(chatsDir)) {
|
||||
this.logger.verbose('creating chats dir: ' + chatsDir);
|
||||
fs.mkdirSync(chatsDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(contactsDir)) {
|
||||
this.logger.verbose('creating contacts dir: ' + contactsDir);
|
||||
fs.mkdirSync(contactsDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(messagesDir)) {
|
||||
this.logger.verbose('creating messages dir: ' + messagesDir);
|
||||
fs.mkdirSync(messagesDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(messageUpDir)) {
|
||||
this.logger.verbose('creating message-up dir: ' + messageUpDir);
|
||||
fs.mkdirSync(messageUpDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(webhookDir)) {
|
||||
this.logger.verbose('creating webhook dir: ' + webhookDir);
|
||||
fs.mkdirSync(webhookDir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(chatwootDir)) {
|
||||
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
|
||||
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 contacts path: ' + join(storePath, 'contacts'));
|
||||
execSync(`mkdir -p ${join(storePath, 'contacts')}`);
|
||||
this.logger.verbose('creating store path: ' + storePath);
|
||||
|
||||
this.logger.verbose('creating messages path: ' + join(storePath, 'messages'));
|
||||
execSync(`mkdir -p ${join(storePath, 'messages')}`);
|
||||
const tempDir = join(storePath, 'temp');
|
||||
|
||||
this.logger.verbose('creating message-up path: ' + join(storePath, 'message-up'));
|
||||
execSync(`mkdir -p ${join(storePath, 'message-up')}`);
|
||||
|
||||
this.logger.verbose('creating webhook path: ' + join(storePath, 'webhook'));
|
||||
execSync(`mkdir -p ${join(storePath, 'webhook')}`);
|
||||
|
||||
this.logger.verbose('creating chatwoot path: ' + join(storePath, 'chatwoot'));
|
||||
execSync(`mkdir -p ${join(storePath, 'chatwoot')}`);
|
||||
|
||||
this.logger.verbose('creating temp path: ' + join(storePath, 'temp'));
|
||||
execSync(`mkdir -p ${join(storePath, 'temp')}`);
|
||||
if (!fs.existsSync(tempDir)) {
|
||||
this.logger.verbose('creating temp dir: ' + tempDir);
|
||||
fs.mkdirSync(tempDir, { recursive: true });
|
||||
}
|
||||
try {
|
||||
} catch (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,
|
||||
profileNameSchema,
|
||||
profilePictureSchema,
|
||||
profileSchema,
|
||||
profileStatusSchema,
|
||||
readMessageSchema,
|
||||
whatsappNumberSchema,
|
||||
@@ -129,6 +130,23 @@ export class ChatRouter extends RouterBroker {
|
||||
|
||||
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) => {
|
||||
logger.verbose('request received in findContacts');
|
||||
logger.verbose('request body: ');
|
||||
|
||||
@@ -9,6 +9,8 @@ import { MessageRouter } from './sendMessage.router';
|
||||
import { ViewsRouter } from './view.router';
|
||||
import { WebhookRouter } from './webhook.router';
|
||||
import { ChatwootRouter } from './chatwoot.router';
|
||||
import fs from 'fs';
|
||||
import { SettingsRouter } from './settings.router';
|
||||
|
||||
enum HttpStatus {
|
||||
OK = 200,
|
||||
@@ -24,11 +26,14 @@ const router = Router();
|
||||
const authType = configService.get<Auth>('AUTHENTICATION').TYPE;
|
||||
const guards = [instanceExistsGuard, instanceLoggedGuard, authGuard[authType]];
|
||||
|
||||
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
|
||||
|
||||
router
|
||||
.get('/', (req, res) => {
|
||||
res.status(HttpStatus.OK).json({
|
||||
status: HttpStatus.OK,
|
||||
message: 'Welcome to the Evolution API, it is working!',
|
||||
version: packageJson.version,
|
||||
});
|
||||
})
|
||||
.use(
|
||||
@@ -40,6 +45,7 @@ router
|
||||
.use('/chat', new ChatRouter(...guards).router)
|
||||
.use('/group', new GroupRouter(...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 };
|
||||
|
||||
@@ -23,6 +23,7 @@ export class InstanceRouter extends RouterBroker {
|
||||
|
||||
logger.verbose('request query: ');
|
||||
logger.verbose(req.query);
|
||||
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: instanceNameSchema,
|
||||
|
||||
@@ -3,7 +3,6 @@ import {
|
||||
audioMessageSchema,
|
||||
buttonMessageSchema,
|
||||
contactMessageSchema,
|
||||
linkPreviewSchema,
|
||||
listMessageSchema,
|
||||
locationMessageSchema,
|
||||
mediaMessageSchema,
|
||||
@@ -17,7 +16,6 @@ import {
|
||||
SendAudioDto,
|
||||
SendButtonDto,
|
||||
SendContactDto,
|
||||
SendLinkPreviewDto,
|
||||
SendListDto,
|
||||
SendLocationDto,
|
||||
SendMediaDto,
|
||||
@@ -199,23 +197,6 @@ export class MessageRouter extends RouterBroker {
|
||||
|
||||
return res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.post(this.routerPath('sendLinkPreview'), ...guards, async (req, res) => {
|
||||
logger.verbose('request received in sendLinkPreview');
|
||||
logger.verbose('request body: ');
|
||||
logger.verbose(req.body);
|
||||
|
||||
logger.verbose('request query: ');
|
||||
logger.verbose(req.query);
|
||||
const response = await this.dataValidate<SendLinkPreviewDto>({
|
||||
request: req,
|
||||
schema: linkPreviewSchema,
|
||||
ClassRef: SendLinkPreviewDto,
|
||||
execute: (instance, data) =>
|
||||
sendMessageController.sendLinkPreview(instance, data),
|
||||
});
|
||||
|
||||
return res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.post(this.routerPath('sendSticker'), ...guards, async (req, res) => {
|
||||
logger.verbose('request received in sendSticker');
|
||||
logger.verbose('request body: ');
|
||||
|
||||
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();
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import mimeTypes from 'mime-types';
|
||||
import { SendAudioDto } from '../dto/sendMessage.dto';
|
||||
import { SendMediaDto } from '../dto/sendMessage.dto';
|
||||
import { ROOT_DIR } from '../../config/path.config';
|
||||
import { ConfigService, HttpServer } from '../../config/env.config';
|
||||
|
||||
export class ChatwootService {
|
||||
private messageCacheFile: string;
|
||||
@@ -21,7 +22,10 @@ export class ChatwootService {
|
||||
|
||||
private provider: any;
|
||||
|
||||
constructor(private readonly waMonitor: WAMonitoringService) {
|
||||
constructor(
|
||||
private readonly waMonitor: WAMonitoringService,
|
||||
private readonly configService: ConfigService,
|
||||
) {
|
||||
this.messageCache = new Set();
|
||||
}
|
||||
|
||||
@@ -43,6 +47,12 @@ export class ChatwootService {
|
||||
this.logger.verbose('message cache saved');
|
||||
}
|
||||
|
||||
private clearMessageCache() {
|
||||
this.logger.verbose('clear message cache');
|
||||
this.messageCache.clear();
|
||||
this.saveMessageCache();
|
||||
}
|
||||
|
||||
private async getProvider(instance: InstanceDto) {
|
||||
this.logger.verbose('get provider to instance: ' + instance.instanceName);
|
||||
try {
|
||||
@@ -144,6 +154,7 @@ export class ChatwootService {
|
||||
inboxName: string,
|
||||
webhookUrl: string,
|
||||
qrcode: boolean,
|
||||
number: string,
|
||||
) {
|
||||
this.logger.verbose('init instance chatwoot: ' + instance.instanceName);
|
||||
|
||||
@@ -233,11 +244,18 @@ export class ChatwootService {
|
||||
}
|
||||
|
||||
this.logger.verbose('create message for init instance in chatwoot');
|
||||
|
||||
let contentMsg = '/init';
|
||||
|
||||
if (number) {
|
||||
contentMsg = `/init:${number}`;
|
||||
}
|
||||
|
||||
const message = await client.messages.create({
|
||||
accountId: this.provider.account_id,
|
||||
conversationId: conversation.id,
|
||||
data: {
|
||||
content: '/iniciar',
|
||||
content: contentMsg,
|
||||
message_type: 'outgoing',
|
||||
},
|
||||
});
|
||||
@@ -258,6 +276,7 @@ export class ChatwootService {
|
||||
inboxId: number,
|
||||
isGroup: boolean,
|
||||
name?: string,
|
||||
avatar_url?: string,
|
||||
) {
|
||||
this.logger.verbose('create contact to instance: ' + instance.instanceName);
|
||||
|
||||
@@ -275,6 +294,7 @@ export class ChatwootService {
|
||||
inbox_id: inboxId,
|
||||
name: name || phoneNumber,
|
||||
phone_number: `+${phoneNumber}`,
|
||||
avatar_url: avatar_url,
|
||||
};
|
||||
} else {
|
||||
this.logger.verbose('create contact group in chatwoot');
|
||||
@@ -282,6 +302,7 @@ export class ChatwootService {
|
||||
inbox_id: inboxId,
|
||||
name: name || phoneNumber,
|
||||
identifier: phoneNumber,
|
||||
avatar_url: avatar_url,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -404,34 +425,86 @@ export class ChatwootService {
|
||||
nameContact = `${group.subject} (GROUP)`;
|
||||
|
||||
this.logger.verbose('find or create participant in chatwoot');
|
||||
const participant =
|
||||
(await this.findContact(instance, body.key.participant.split('@')[0])) ||
|
||||
((await this.createContact(
|
||||
|
||||
const picture_url = await this.waMonitor.waInstances[
|
||||
instance.instanceName
|
||||
].profilePicture(body.key.participant.split('@')[0]);
|
||||
|
||||
const findParticipant = await this.findContact(
|
||||
instance,
|
||||
body.key.participant.split('@')[0],
|
||||
);
|
||||
|
||||
if (findParticipant) {
|
||||
if (!findParticipant.name || findParticipant.name === chatId) {
|
||||
await this.updateContact(instance, findParticipant.id, {
|
||||
name: body.pushName,
|
||||
avatar_url: picture_url.profilePictureUrl || null,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await this.createContact(
|
||||
instance,
|
||||
body.key.participant.split('@')[0],
|
||||
filterInbox.id,
|
||||
false,
|
||||
body.pushName || body.key.participant.split('@')[0],
|
||||
)) as any);
|
||||
body.pushName,
|
||||
picture_url.profilePictureUrl || null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.verbose('find or create contact in chatwoot');
|
||||
const contact =
|
||||
(await this.findContact(instance, chatId)) ||
|
||||
((await this.createContact(
|
||||
instance,
|
||||
chatId,
|
||||
filterInbox.id,
|
||||
isGroup,
|
||||
nameContact,
|
||||
)) as any);
|
||||
|
||||
const picture_url = await this.waMonitor.waInstances[
|
||||
instance.instanceName
|
||||
].profilePicture(chatId);
|
||||
|
||||
const findContact = await this.findContact(instance, chatId);
|
||||
|
||||
let contact: any;
|
||||
if (body.key.fromMe) {
|
||||
if (findContact) {
|
||||
contact = findContact;
|
||||
} else {
|
||||
contact = await this.createContact(
|
||||
instance,
|
||||
chatId,
|
||||
filterInbox.id,
|
||||
isGroup,
|
||||
nameContact,
|
||||
picture_url.profilePictureUrl || null,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (findContact) {
|
||||
if (!findContact.name || findContact.name === chatId) {
|
||||
contact = await this.updateContact(instance, findContact.id, {
|
||||
name: nameContact,
|
||||
avatar_url: picture_url.profilePictureUrl || null,
|
||||
});
|
||||
} else {
|
||||
contact = findContact;
|
||||
}
|
||||
} else {
|
||||
contact = await this.createContact(
|
||||
instance,
|
||||
chatId,
|
||||
filterInbox.id,
|
||||
isGroup,
|
||||
nameContact,
|
||||
picture_url.profilePictureUrl || null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!contact) {
|
||||
this.logger.warn('contact not found');
|
||||
return null;
|
||||
}
|
||||
|
||||
const contactId = contact.id || contact.payload.contact.id;
|
||||
const contactId =
|
||||
contact?.payload?.id || contact?.payload?.contact?.id || contact?.id;
|
||||
|
||||
if (!body.key.fromMe && contact.name === chatId && nameContact !== chatId) {
|
||||
this.logger.verbose('update contact name in chatwoot');
|
||||
@@ -888,18 +961,19 @@ export class ChatwootService {
|
||||
|
||||
const command = messageReceived.replace('/', '');
|
||||
|
||||
if (command === 'iniciar') {
|
||||
this.logger.verbose('command iniciar found');
|
||||
if (command.includes('init') || command.includes('iniciar')) {
|
||||
this.logger.verbose('command init found');
|
||||
const state = waInstance?.connectionStatus?.state;
|
||||
|
||||
if (state !== 'open') {
|
||||
this.logger.verbose('connect to whatsapp');
|
||||
await waInstance.connectToWhatsapp();
|
||||
const number = command.split(':')[1];
|
||||
await waInstance.connectToWhatsapp(number);
|
||||
} else {
|
||||
this.logger.verbose('whatsapp already connected');
|
||||
await this.createBotMessage(
|
||||
instance,
|
||||
`🚨 Instância ${body.inbox.name} já está conectada.`,
|
||||
`🚨 ${body.inbox.name} instance is connected.`,
|
||||
'incoming',
|
||||
);
|
||||
}
|
||||
@@ -914,7 +988,7 @@ export class ChatwootService {
|
||||
this.logger.verbose('state not found');
|
||||
await this.createBotMessage(
|
||||
instance,
|
||||
`⚠️ Instância ${body.inbox.name} não existe.`,
|
||||
`⚠️ ${body.inbox.name} instance not found.`,
|
||||
'incoming',
|
||||
);
|
||||
}
|
||||
@@ -923,16 +997,16 @@ export class ChatwootService {
|
||||
this.logger.verbose('state: ' + state + ' found');
|
||||
await this.createBotMessage(
|
||||
instance,
|
||||
`⚠️ Status da instância ${body.inbox.name}: *${state}*`,
|
||||
`⚠️ ${body.inbox.name} instance status: *${state}*`,
|
||||
'incoming',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (command === 'desconectar') {
|
||||
this.logger.verbose('command desconectar found');
|
||||
if (command === 'disconnect' || command === 'desconectar') {
|
||||
this.logger.verbose('command disconnect found');
|
||||
|
||||
const msgLogout = `🚨 Desconectando Whatsapp da caixa de entrada *${body.inbox.name}*: `;
|
||||
const msgLogout = `🚨 Disconnecting Whatsapp from inbox *${body.inbox.name}*: `;
|
||||
|
||||
this.logger.verbose('send message to chatwoot');
|
||||
await this.createBotMessage(instance, msgLogout, 'incoming');
|
||||
@@ -941,6 +1015,37 @@ export class ChatwootService {
|
||||
await waInstance?.client?.logout('Log out instance: ' + instance.instanceName);
|
||||
await waInstance?.client?.ws?.close();
|
||||
}
|
||||
|
||||
if (command.includes('new_instance')) {
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
|
||||
|
||||
const data = {
|
||||
instanceName: command.split(':')[1],
|
||||
qrcode: true,
|
||||
chatwoot_account_id: this.provider.account_id,
|
||||
chatwoot_token: this.provider.token,
|
||||
chatwoot_url: this.provider.url,
|
||||
chatwoot_sign_msg: this.provider.sign_msg,
|
||||
};
|
||||
|
||||
if (command.split(':')[2]) {
|
||||
data['number'] = command.split(':')[2];
|
||||
}
|
||||
|
||||
const config = {
|
||||
method: 'post',
|
||||
maxBodyLength: Infinity,
|
||||
url: `${urlServer}/instance/create`,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
apikey: apiKey,
|
||||
},
|
||||
data: data,
|
||||
};
|
||||
|
||||
await axios.request(config);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
@@ -968,6 +1073,9 @@ export class ChatwootService {
|
||||
return { message: 'bot' };
|
||||
}
|
||||
|
||||
this.logger.verbose('clear cache');
|
||||
this.clearMessageCache();
|
||||
|
||||
this.logger.verbose('Format message to send');
|
||||
let formatText: string;
|
||||
if (senderName === null || senderName === undefined) {
|
||||
@@ -1016,7 +1124,7 @@ export class ChatwootService {
|
||||
}
|
||||
}
|
||||
|
||||
if (body.message_type === 'template' && body.content_type === 'input_csat') {
|
||||
if (body.message_type === 'template' && body.event === 'message_created') {
|
||||
this.logger.verbose('check if is csat');
|
||||
|
||||
const data: SendTextDto = {
|
||||
@@ -1076,6 +1184,8 @@ export class ChatwootService {
|
||||
documentWithCaptionMessage:
|
||||
msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
|
||||
audioMessage: msg.audioMessage?.caption,
|
||||
contactMessage: msg.contactMessage?.vcard,
|
||||
contactsArrayMessage: msg.contactsArrayMessage,
|
||||
};
|
||||
|
||||
this.logger.verbose('type message: ' + types);
|
||||
@@ -1089,6 +1199,71 @@ export class ChatwootService {
|
||||
|
||||
const result = typeKey ? types[typeKey] : undefined;
|
||||
|
||||
if (typeKey === 'stickerMessage') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeKey === 'contactMessage') {
|
||||
const vCardData = result.split('\n');
|
||||
const contactInfo = {};
|
||||
|
||||
vCardData.forEach((line) => {
|
||||
const [key, value] = line.split(':');
|
||||
if (key && value) {
|
||||
contactInfo[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
let formattedContact = `**Contact:**
|
||||
**name:** ${contactInfo['FN']}`;
|
||||
|
||||
let numberCount = 1;
|
||||
Object.keys(contactInfo).forEach((key) => {
|
||||
if (key.startsWith('item') && key.includes('TEL')) {
|
||||
const phoneNumber = contactInfo[key];
|
||||
formattedContact += `\n**number ${numberCount}:** ${phoneNumber}`;
|
||||
numberCount++;
|
||||
}
|
||||
});
|
||||
|
||||
this.logger.verbose('message content: ' + formattedContact);
|
||||
return formattedContact;
|
||||
}
|
||||
|
||||
if (typeKey === 'contactsArrayMessage') {
|
||||
const formattedContacts = result.contacts.map((contact) => {
|
||||
const vCardData = contact.vcard.split('\n');
|
||||
const contactInfo = {};
|
||||
|
||||
vCardData.forEach((line) => {
|
||||
const [key, value] = line.split(':');
|
||||
if (key && value) {
|
||||
contactInfo[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
let formattedContact = `**Contact:**
|
||||
**name:** ${contact.displayName}`;
|
||||
|
||||
let numberCount = 1;
|
||||
Object.keys(contactInfo).forEach((key) => {
|
||||
if (key.startsWith('item') && key.includes('TEL')) {
|
||||
const phoneNumber = contactInfo[key];
|
||||
formattedContact += `\n**number ${numberCount}:** ${phoneNumber}`;
|
||||
numberCount++;
|
||||
}
|
||||
});
|
||||
|
||||
return formattedContact;
|
||||
});
|
||||
|
||||
const formattedContactsArray = formattedContacts.join('\n\n');
|
||||
|
||||
this.logger.verbose('formatted contacts: ' + formattedContactsArray);
|
||||
|
||||
return formattedContactsArray;
|
||||
}
|
||||
|
||||
this.logger.verbose('message content: ' + result);
|
||||
|
||||
return result;
|
||||
@@ -1131,6 +1306,16 @@ export class ChatwootService {
|
||||
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');
|
||||
const getConversion = await this.createConversation(instance, body);
|
||||
|
||||
@@ -1143,18 +1328,8 @@ export class ChatwootService {
|
||||
|
||||
this.logger.verbose('message type: ' + messageType);
|
||||
|
||||
const isMedia = this.isMediaMessage(body.message);
|
||||
|
||||
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');
|
||||
if (isMedia) {
|
||||
this.logger.verbose('message is media');
|
||||
@@ -1343,7 +1518,7 @@ export class ChatwootService {
|
||||
return;
|
||||
}
|
||||
|
||||
const msgStatus = `⚡️ Status da instância ${inbox.name}: ${data.status}`;
|
||||
const msgStatus = `⚡️ Instance status ${inbox.name}: ${data.status}`;
|
||||
|
||||
this.logger.verbose('send message to chatwoot');
|
||||
await this.createBotMessage(instance, msgStatus, 'incoming');
|
||||
@@ -1351,43 +1526,20 @@ export class ChatwootService {
|
||||
|
||||
if (event === 'connection.update') {
|
||||
this.logger.verbose('event connection.update');
|
||||
if (body.state === 'open') {
|
||||
const msgConnection = `🚀 Conexão realizada com sucesso!`;
|
||||
|
||||
if (body.status === 'open') {
|
||||
const msgConnection = `🚀 Connection successfully established!`;
|
||||
|
||||
this.logger.verbose('send message to chatwoot');
|
||||
await this.createBotMessage(instance, msgConnection, 'incoming');
|
||||
}
|
||||
}
|
||||
|
||||
if (event === 'contacts.update') {
|
||||
this.logger.verbose('event contacts.update');
|
||||
const data = body;
|
||||
|
||||
if (data.length) {
|
||||
this.logger.verbose('contacts found');
|
||||
for (const item of data) {
|
||||
const number = item.id.split('@')[0];
|
||||
const photo = item.profilePictureUrl || null;
|
||||
this.logger.verbose('find contact in chatwoot');
|
||||
const find = await this.findContact(instance, number);
|
||||
|
||||
if (find) {
|
||||
this.logger.verbose('contact found');
|
||||
|
||||
this.logger.verbose('update contact in chatwoot');
|
||||
await this.updateContact(instance, find.id, {
|
||||
avatar_url: photo,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (event === 'qrcode.updated') {
|
||||
this.logger.verbose('event qrcode.updated');
|
||||
if (body.statusCode === 500) {
|
||||
this.logger.verbose('qrcode error');
|
||||
const erroQRcode = `🚨 Limite de geração de QRCode atingido, para gerar um novo QRCode, envie a mensagem /iniciar novamente.`;
|
||||
const erroQRcode = `🚨 QRCode generation limit reached, to generate a new QRCode, send the /init message again.`;
|
||||
|
||||
this.logger.verbose('send message to chatwoot');
|
||||
return await this.createBotMessage(instance, erroQRcode, 'incoming');
|
||||
@@ -1412,12 +1564,21 @@ export class ChatwootService {
|
||||
this.logger.verbose('send qrcode to chatwoot');
|
||||
await this.createBotQr(
|
||||
instance,
|
||||
'QRCode gerado com sucesso!',
|
||||
'QRCode successfully generated!',
|
||||
'incoming',
|
||||
fileName,
|
||||
);
|
||||
|
||||
const msgQrCode = `⚡️ QRCode gerado com sucesso!\n\nDigitalize este código QR nos próximos 40 segundos:`;
|
||||
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');
|
||||
await this.createBotMessage(instance, msgQrCode, 'incoming');
|
||||
@@ -1427,4 +1588,49 @@ export class ChatwootService {
|
||||
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 { NotFoundException } from '../../exceptions';
|
||||
import { Db } from 'mongodb';
|
||||
import { initInstance } from '../whatsapp.module';
|
||||
import { RedisCache } from '../../db/redis.client';
|
||||
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 {
|
||||
constructor(
|
||||
@@ -45,6 +55,8 @@ export class WAMonitoringService {
|
||||
|
||||
private dbInstance: Db;
|
||||
|
||||
private dbStore = dbserver;
|
||||
|
||||
private readonly logger = new Logger(WAMonitoringService.name);
|
||||
public readonly waInstances: Record<string, WAStartupService> = {};
|
||||
|
||||
@@ -90,7 +102,7 @@ export class WAMonitoringService {
|
||||
|
||||
const findChatwoot = await this.waInstances[key].findChatwoot();
|
||||
|
||||
if (findChatwoot.enabled) {
|
||||
if (findChatwoot && findChatwoot.enabled) {
|
||||
chatwoot = {
|
||||
...findChatwoot,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${key}`,
|
||||
@@ -99,72 +111,54 @@ export class WAMonitoringService {
|
||||
|
||||
if (value.connectionStatus.state === 'open') {
|
||||
this.logger.verbose('instance: ' + key + ' - connectionStatus: open');
|
||||
let apikey: string;
|
||||
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
||||
this.logger.verbose(
|
||||
'instance: ' + key + ' - hash exposed in fetch instances',
|
||||
);
|
||||
const tokenStore = await this.repository.auth.find(key);
|
||||
apikey = tokenStore.apikey || 'Apikey not found';
|
||||
|
||||
instances.push({
|
||||
instance: {
|
||||
instanceName: key,
|
||||
owner: value.wuid,
|
||||
profileName: (await value.getProfileName()) || 'not loaded',
|
||||
profilePictureUrl: value.profilePictureUrl,
|
||||
profileStatus: (await value.getProfileStatus()) || '',
|
||||
status: value.connectionStatus.state,
|
||||
apikey,
|
||||
chatwoot,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
this.logger.verbose(
|
||||
'instance: ' + key + ' - hash not exposed in fetch instances',
|
||||
);
|
||||
instances.push({
|
||||
instance: {
|
||||
instanceName: key,
|
||||
owner: value.wuid,
|
||||
profileName: (await value.getProfileName()) || 'not loaded',
|
||||
profilePictureUrl: value.profilePictureUrl,
|
||||
profileStatus: (await value.getProfileStatus()) || '',
|
||||
status: value.connectionStatus.state,
|
||||
},
|
||||
});
|
||||
const instanceData = {
|
||||
instance: {
|
||||
instanceName: key,
|
||||
owner: value.wuid,
|
||||
profileName: (await value.getProfileName()) || 'not loaded',
|
||||
profilePictureUrl: value.profilePictureUrl,
|
||||
profileStatus: (await value.getProfileStatus()) || '',
|
||||
status: value.connectionStatus.state,
|
||||
},
|
||||
};
|
||||
|
||||
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
||||
instanceData.instance['serverUrl'] =
|
||||
this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
instanceData.instance['apikey'] = (
|
||||
await this.repository.auth.find(key)
|
||||
).apikey;
|
||||
|
||||
instanceData.instance['chatwoot'] = chatwoot;
|
||||
}
|
||||
|
||||
instances.push(instanceData);
|
||||
} else {
|
||||
this.logger.verbose(
|
||||
'instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state,
|
||||
);
|
||||
let apikey: string;
|
||||
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
||||
this.logger.verbose(
|
||||
'instance: ' + key + ' - hash exposed in fetch instances',
|
||||
);
|
||||
const tokenStore = await this.repository.auth.find(key);
|
||||
apikey = tokenStore.apikey || 'Apikey not found';
|
||||
|
||||
instances.push({
|
||||
instance: {
|
||||
instanceName: key,
|
||||
status: value.connectionStatus.state,
|
||||
apikey,
|
||||
chatwoot,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
this.logger.verbose(
|
||||
'instance: ' + key + ' - hash not exposed in fetch instances',
|
||||
);
|
||||
instances.push({
|
||||
instance: {
|
||||
instanceName: key,
|
||||
status: value.connectionStatus.state,
|
||||
},
|
||||
});
|
||||
const instanceData = {
|
||||
instance: {
|
||||
instanceName: key,
|
||||
status: value.connectionStatus.state,
|
||||
},
|
||||
};
|
||||
|
||||
if (this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
||||
instanceData.instance['serverUrl'] =
|
||||
this.configService.get<HttpServer>('SERVER').URL;
|
||||
|
||||
instanceData.instance['apikey'] = (
|
||||
await this.repository.auth.find(key)
|
||||
).apikey;
|
||||
|
||||
instanceData.instance['chatwoot'] = chatwoot;
|
||||
}
|
||||
|
||||
instances.push(instanceData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -236,11 +230,8 @@ export class WAMonitoringService {
|
||||
}
|
||||
|
||||
public async cleaningStoreFiles(instanceName: string) {
|
||||
this.logger.verbose('cleaning store files instance: ' + instanceName);
|
||||
|
||||
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 });
|
||||
|
||||
execSync(`rm -rf ${join(STORE_DIR, 'chats', instanceName)}`);
|
||||
@@ -251,7 +242,23 @@ export class WAMonitoringService {
|
||||
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, '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() {
|
||||
@@ -282,7 +289,6 @@ export class WAMonitoringService {
|
||||
keys.forEach(async (k) => await set(k.split(':')[1]));
|
||||
} else {
|
||||
this.logger.verbose('no instance keys found');
|
||||
initInstance();
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -298,7 +304,6 @@ export class WAMonitoringService {
|
||||
);
|
||||
} else {
|
||||
this.logger.verbose('no collections found');
|
||||
initInstance();
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -319,7 +324,6 @@ export class WAMonitoringService {
|
||||
await set(dirent.name);
|
||||
} else {
|
||||
this.logger.verbose('no instance files found');
|
||||
initInstance();
|
||||
}
|
||||
}
|
||||
} 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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,17 @@ export class WebhookService {
|
||||
public async find(instance: InstanceDto): Promise<WebhookDto> {
|
||||
try {
|
||||
this.logger.verbose('find webhook: ' + instance.instanceName);
|
||||
return await this.waMonitor.waInstances[instance.instanceName].findWebhook();
|
||||
const result = await this.waMonitor.waInstances[
|
||||
instance.instanceName
|
||||
].findWebhook();
|
||||
|
||||
if (Object.keys(result).length === 0) {
|
||||
throw new Error('Webhook not found');
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
return { enabled: null, url: '' };
|
||||
return { enabled: false, url: '', events: [], webhook_by_events: false };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +76,6 @@ import {
|
||||
SendReactionDto,
|
||||
SendTextDto,
|
||||
SendPollDto,
|
||||
SendLinkPreviewDto,
|
||||
SendStickerDto,
|
||||
SendStatusDto,
|
||||
StatusMessage,
|
||||
@@ -85,6 +84,7 @@ import { arrayUnique, isBase64, isURL } from 'class-validator';
|
||||
import {
|
||||
ArchiveChatDto,
|
||||
DeleteMessage,
|
||||
NumberBusiness,
|
||||
OnWhatsAppDto,
|
||||
PrivacySettingDto,
|
||||
ReadMessageDto,
|
||||
@@ -125,6 +125,7 @@ import { Log } from '../../config/env.config';
|
||||
import ProxyAgent from 'proxy-agent';
|
||||
import { ChatwootService } from './chatwoot.service';
|
||||
import { waMonitor } from '../whatsapp.module';
|
||||
import { SettingsRaw } from '../models';
|
||||
|
||||
export class WAStartupService {
|
||||
constructor(
|
||||
@@ -143,6 +144,7 @@ export class WAStartupService {
|
||||
public client: WASocket;
|
||||
private readonly localWebhook: wa.LocalWebHook = {};
|
||||
private readonly localChatwoot: wa.LocalChatwoot = {};
|
||||
private readonly localSettings: wa.LocalSettings = {};
|
||||
private stateConnection: wa.StateConnection = { state: 'close' };
|
||||
public readonly storePath = join(ROOT_DIR, 'store');
|
||||
private readonly msgRetryCounterCache: CacheStore = new NodeCache();
|
||||
@@ -150,7 +152,9 @@ export class WAStartupService {
|
||||
private endSession = false;
|
||||
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
|
||||
|
||||
private chatwootService = new ChatwootService(waMonitor);
|
||||
private phoneNumber: string;
|
||||
|
||||
private chatwootService = new ChatwootService(waMonitor, this.configService);
|
||||
|
||||
public set instanceName(name: string) {
|
||||
this.logger.verbose(`Initializing instance '${name}'`);
|
||||
@@ -239,6 +243,12 @@ export class WAStartupService {
|
||||
|
||||
public get qrCode(): wa.QrCode {
|
||||
this.logger.verbose('Getting qrcode');
|
||||
if (this.instance.qrcode?.pairingCode) {
|
||||
return {
|
||||
pairingCode: this.instance.qrcode?.pairingCode,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
code: this.instance.qrcode?.code,
|
||||
base64: this.instance.qrcode?.base64,
|
||||
@@ -329,7 +339,7 @@ export class WAStartupService {
|
||||
|
||||
if (!data) {
|
||||
this.logger.verbose('Chatwoot not found');
|
||||
throw new NotFoundException('Chatwoot not found');
|
||||
return null;
|
||||
}
|
||||
|
||||
this.logger.verbose(`Chatwoot account id: ${data.account_id}`);
|
||||
@@ -341,15 +351,61 @@ export class WAStartupService {
|
||||
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.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}`);
|
||||
Object.assign(this.localSettings, data);
|
||||
this.logger.verbose('Settings set');
|
||||
}
|
||||
|
||||
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');
|
||||
throw new NotFoundException('Settings not found');
|
||||
}
|
||||
|
||||
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}`);
|
||||
return data;
|
||||
}
|
||||
|
||||
public async sendDataWebhook<T = any>(event: Events, data: T, local = true) {
|
||||
const webhookGlobal = this.configService.get<Webhook>('WEBHOOK');
|
||||
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
||||
const webhookLocal = this.localWebhook.events;
|
||||
const serverUrl = this.configService.get<HttpServer>('SERVER').URL;
|
||||
const we = event.replace(/[\.-]/gm, '_').toUpperCase();
|
||||
const transformedWe = we.replace(/_/gm, '-').toLowerCase();
|
||||
const instance = this.configService.get<Auth>('AUTHENTICATION').INSTANCE;
|
||||
|
||||
if (local && instance.MODE !== 'container') {
|
||||
const expose =
|
||||
this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES;
|
||||
const tokenStore = await this.repository.auth.find(this.instanceName);
|
||||
const instanceApikey = tokenStore?.apikey || 'Apikey not found';
|
||||
|
||||
const globalApiKey = this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
|
||||
|
||||
if (local) {
|
||||
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
|
||||
this.logger.verbose('Sending data to webhook local');
|
||||
let baseURL;
|
||||
@@ -361,27 +417,40 @@ export class WAStartupService {
|
||||
}
|
||||
|
||||
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
|
||||
this.logger.log({
|
||||
const logData = {
|
||||
local: WAStartupService.name + '.sendDataWebhook-local',
|
||||
url: baseURL,
|
||||
event,
|
||||
instance: this.instance.name,
|
||||
data,
|
||||
destination: this.localWebhook.url,
|
||||
urlServer,
|
||||
});
|
||||
server_url: serverUrl,
|
||||
apikey: (expose && instanceApikey) || null,
|
||||
};
|
||||
|
||||
if (expose && instanceApikey) {
|
||||
logData['apikey'] = instanceApikey;
|
||||
}
|
||||
|
||||
this.logger.log(logData);
|
||||
}
|
||||
|
||||
try {
|
||||
if (this.localWebhook.enabled && isURL(this.localWebhook.url)) {
|
||||
const httpService = axios.create({ baseURL });
|
||||
await httpService.post('', {
|
||||
const postData = {
|
||||
event,
|
||||
instance: this.instance.name,
|
||||
data,
|
||||
destination: this.localWebhook.url,
|
||||
urlServer,
|
||||
});
|
||||
server_url: serverUrl,
|
||||
};
|
||||
|
||||
if (expose && instanceApikey) {
|
||||
postData['apikey'] = instanceApikey;
|
||||
}
|
||||
|
||||
await httpService.post('', postData);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error({
|
||||
@@ -394,6 +463,7 @@ export class WAStartupService {
|
||||
stack: error?.stack,
|
||||
name: error?.name,
|
||||
url: baseURL,
|
||||
server_url: serverUrl,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -412,36 +482,42 @@ export class WAStartupService {
|
||||
globalURL = globalWebhook.URL;
|
||||
}
|
||||
|
||||
let localUrl;
|
||||
|
||||
if (instance.MODE === 'container') {
|
||||
localUrl = instance.WEBHOOK_URL;
|
||||
} else {
|
||||
localUrl = this.localWebhook.url;
|
||||
}
|
||||
const localUrl = this.localWebhook.url;
|
||||
|
||||
if (this.configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS')) {
|
||||
this.logger.log({
|
||||
const logData = {
|
||||
local: WAStartupService.name + '.sendDataWebhook-global',
|
||||
url: globalURL,
|
||||
event,
|
||||
instance: this.instance.name,
|
||||
data,
|
||||
destination: localUrl,
|
||||
urlServer,
|
||||
});
|
||||
server_url: serverUrl,
|
||||
};
|
||||
|
||||
if (expose && globalApiKey) {
|
||||
logData['apikey'] = globalApiKey;
|
||||
}
|
||||
|
||||
this.logger.log(logData);
|
||||
}
|
||||
|
||||
try {
|
||||
if (globalWebhook && globalWebhook?.ENABLED && isURL(globalURL)) {
|
||||
const httpService = axios.create({ baseURL: globalURL });
|
||||
await httpService.post('', {
|
||||
const postData = {
|
||||
event,
|
||||
instance: this.instance.name,
|
||||
data,
|
||||
destination: localUrl,
|
||||
urlServer,
|
||||
});
|
||||
server_url: serverUrl,
|
||||
};
|
||||
|
||||
if (expose && globalApiKey) {
|
||||
postData['apikey'] = globalApiKey;
|
||||
}
|
||||
|
||||
await httpService.post('', postData);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error({
|
||||
@@ -454,6 +530,7 @@ export class WAStartupService {
|
||||
stack: error?.stack,
|
||||
name: error?.name,
|
||||
url: globalURL,
|
||||
server_url: serverUrl,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -529,6 +606,16 @@ export class WAStartupService {
|
||||
color: { light: '#ffffff', dark: '#198754' },
|
||||
};
|
||||
|
||||
console.log(this.phoneNumber);
|
||||
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');
|
||||
qrcode.toDataURL(qr, optsQrcode, (error, base64) => {
|
||||
if (error) {
|
||||
@@ -540,7 +627,12 @@ export class WAStartupService {
|
||||
this.instance.qrcode.code = qr;
|
||||
|
||||
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) {
|
||||
@@ -548,7 +640,12 @@ export class WAStartupService {
|
||||
Events.QRCODE_UPDATED,
|
||||
{ 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,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -557,7 +654,7 @@ export class WAStartupService {
|
||||
this.logger.verbose('Generating QR code in terminal');
|
||||
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
|
||||
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,
|
||||
),
|
||||
);
|
||||
@@ -623,6 +720,17 @@ export class WAStartupService {
|
||||
│ CONNECTED TO WHATSAPP │
|
||||
└──────────────────────────────┘`.replace(/^ +/gm, ' '),
|
||||
);
|
||||
|
||||
if (this.localChatwoot.enabled) {
|
||||
this.chatwootService.eventWhatsapp(
|
||||
Events.CONNECTION_UPDATE,
|
||||
{ instanceName: this.instance.name },
|
||||
{
|
||||
instance: this.instance.name,
|
||||
status: 'open',
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -713,11 +821,12 @@ export class WAStartupService {
|
||||
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');
|
||||
try {
|
||||
this.loadWebhook();
|
||||
this.loadChatwoot();
|
||||
this.loadSettings();
|
||||
|
||||
this.instance.authState = await this.defineAuthState();
|
||||
|
||||
@@ -741,6 +850,7 @@ export class WAStartupService {
|
||||
version,
|
||||
connectTimeoutMs: 60_000,
|
||||
qrTimeout: 40_000,
|
||||
defaultQueryTimeoutMs: undefined,
|
||||
emitOwnEvents: false,
|
||||
msgRetryCounterCache: this.msgRetryCounterCache,
|
||||
getMessage: async (key) =>
|
||||
@@ -785,6 +895,8 @@ export class WAStartupService {
|
||||
|
||||
this.logger.verbose('Socket event handler initialized');
|
||||
|
||||
this.phoneNumber = number;
|
||||
|
||||
return this.client;
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
@@ -908,14 +1020,6 @@ export class WAStartupService {
|
||||
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
|
||||
await this.sendDataWebhook(Events.CONTACTS_UPDATE, contactsRaw);
|
||||
|
||||
if (this.localChatwoot.enabled) {
|
||||
await this.chatwootService.eventWhatsapp(
|
||||
Events.CONTACTS_UPDATE,
|
||||
{ instanceName: this.instance.name },
|
||||
contactsRaw,
|
||||
);
|
||||
}
|
||||
|
||||
this.logger.verbose('Updating contacts in database');
|
||||
await this.repository.contact.update(
|
||||
contactsRaw,
|
||||
@@ -1007,13 +1111,14 @@ export class WAStartupService {
|
||||
type: MessageUpsertType;
|
||||
},
|
||||
database: Database,
|
||||
settings: SettingsRaw,
|
||||
) => {
|
||||
this.logger.verbose('Event received: messages.upsert');
|
||||
const received = messages[0];
|
||||
|
||||
if (
|
||||
type !== 'notify' ||
|
||||
received.message?.protocolMessage ||
|
||||
// received.message?.protocolMessage ||
|
||||
received.message?.pollUpdateMessage
|
||||
) {
|
||||
this.logger.verbose('message rejected');
|
||||
@@ -1024,6 +1129,11 @@ export class WAStartupService {
|
||||
received.messageTimestamp = received.messageTimestamp?.toNumber();
|
||||
}
|
||||
|
||||
if (settings.groups_ignore && received.key.remoteJid.includes('@g.us')) {
|
||||
this.logger.verbose('group ignored');
|
||||
return;
|
||||
}
|
||||
|
||||
const messageRaw: MessageRaw = {
|
||||
key: received.key,
|
||||
pushName: received.pushName,
|
||||
@@ -1115,7 +1225,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');
|
||||
const status: Record<number, wa.StatusMessage> = {
|
||||
0: 'ERROR',
|
||||
@@ -1126,11 +1240,11 @@ export class WAStartupService {
|
||||
5: 'PLAYED',
|
||||
};
|
||||
for await (const { key, update } of args) {
|
||||
if (
|
||||
key.remoteJid !== 'status@broadcast' &&
|
||||
!key?.remoteJid?.match(/(:\d+)/) &&
|
||||
key.fromMe
|
||||
) {
|
||||
if (settings.groups_ignore && key.remoteJid.includes('@g.us')) {
|
||||
this.logger.verbose('group ignored');
|
||||
return;
|
||||
}
|
||||
if (key.remoteJid !== 'status@broadcast' && !key?.remoteJid?.match(/(:\d+)/)) {
|
||||
this.logger.verbose('Message update is valid');
|
||||
|
||||
let pollUpdates: any;
|
||||
@@ -1150,6 +1264,32 @@ export class WAStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
if (status[update.status] === 'READ' && !key.fromMe) return;
|
||||
|
||||
if (update.message === null && update.status === undefined) {
|
||||
this.logger.verbose('Message deleted');
|
||||
|
||||
this.logger.verbose('Sending data to webhook in event MESSAGE_DELETE');
|
||||
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;
|
||||
}
|
||||
|
||||
const message: MessageUpdateRaw = {
|
||||
...key,
|
||||
status: status[update.status],
|
||||
@@ -1203,9 +1343,32 @@ export class WAStartupService {
|
||||
|
||||
private eventHandler() {
|
||||
this.logger.verbose('Initializing event handler');
|
||||
this.client.ev.process((events) => {
|
||||
this.client.ev.process(async (events) => {
|
||||
if (!this.endSession) {
|
||||
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.client.ev.emit('messages.upsert', {
|
||||
messages: [msg],
|
||||
type: 'notify',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (events['connection.update']) {
|
||||
this.logger.verbose('Listening event: connection.update');
|
||||
@@ -1226,37 +1389,44 @@ export class WAStartupService {
|
||||
if (events['messages.upsert']) {
|
||||
this.logger.verbose('Listening event: messages.upsert');
|
||||
const payload = events['messages.upsert'];
|
||||
this.messageHandle['messages.upsert'](payload, database);
|
||||
this.messageHandle['messages.upsert'](payload, database, settings);
|
||||
}
|
||||
|
||||
if (events['messages.update']) {
|
||||
this.logger.verbose('Listening event: messages.update');
|
||||
const payload = events['messages.update'];
|
||||
this.messageHandle['messages.update'](payload, database);
|
||||
this.messageHandle['messages.update'](payload, database, settings);
|
||||
}
|
||||
|
||||
if (events['presence.update']) {
|
||||
this.logger.verbose('Listening event: 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);
|
||||
}
|
||||
|
||||
if (events['groups.upsert']) {
|
||||
this.logger.verbose('Listening event: groups.upsert');
|
||||
const payload = events['groups.upsert'];
|
||||
this.groupHandler['groups.upsert'](payload);
|
||||
}
|
||||
if (!settings?.groups_ignore) {
|
||||
if (events['groups.upsert']) {
|
||||
this.logger.verbose('Listening event: groups.upsert');
|
||||
const payload = events['groups.upsert'];
|
||||
this.groupHandler['groups.upsert'](payload);
|
||||
}
|
||||
|
||||
if (events['groups.update']) {
|
||||
this.logger.verbose('Listening event: groups.update');
|
||||
const payload = events['groups.update'];
|
||||
this.groupHandler['groups.update'](payload);
|
||||
}
|
||||
if (events['groups.update']) {
|
||||
this.logger.verbose('Listening event: groups.update');
|
||||
const payload = events['groups.update'];
|
||||
this.groupHandler['groups.update'](payload);
|
||||
}
|
||||
|
||||
if (events['group-participants.update']) {
|
||||
this.logger.verbose('Listening event: group-participants.update');
|
||||
const payload = events['group-participants.update'];
|
||||
this.groupHandler['group-participants.update'](payload);
|
||||
if (events['group-participants.update']) {
|
||||
this.logger.verbose('Listening event: group-participants.update');
|
||||
const payload = events['group-participants.update'];
|
||||
this.groupHandler['group-participants.update'](payload);
|
||||
}
|
||||
}
|
||||
|
||||
if (events['chats.upsert']) {
|
||||
@@ -1294,16 +1464,12 @@ export class WAStartupService {
|
||||
|
||||
// Check if the number is MX or AR
|
||||
private formatMXOrARNumber(jid: string): string {
|
||||
const regexp = new RegExp(/^(\d{2})(\d{2})\d{1}(\d{8})$/);
|
||||
if (regexp.test(jid)) {
|
||||
const match = regexp.exec(jid);
|
||||
if (match && (match[1] === '52' || match[1] === '54')) {
|
||||
const joker = Number.parseInt(match[3][0]);
|
||||
const ddd = Number.parseInt(match[2]);
|
||||
if (joker < 7 || ddd < 11) {
|
||||
return match[0];
|
||||
}
|
||||
return match[1] === '52' ? '52' + match[3] : '54' + match[3];
|
||||
const countryCode = jid.substring(0, 2);
|
||||
|
||||
if (Number(countryCode) === 52 || Number(countryCode) === 54) {
|
||||
if (jid.length === 13) {
|
||||
const number = countryCode + jid.substring(3);
|
||||
return number;
|
||||
}
|
||||
|
||||
return jid;
|
||||
@@ -1332,6 +1498,7 @@ export class WAStartupService {
|
||||
|
||||
private createJid(number: string): string {
|
||||
this.logger.verbose('Creating jid with number: ' + number);
|
||||
|
||||
if (number.includes('@g.us') || number.includes('@s.whatsapp.net')) {
|
||||
this.logger.verbose('Number already contains @g.us or @s.whatsapp.net');
|
||||
return number;
|
||||
@@ -1342,29 +1509,22 @@ export class WAStartupService {
|
||||
return number;
|
||||
}
|
||||
|
||||
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`;
|
||||
}
|
||||
number = number
|
||||
?.replace(/\s/g, '')
|
||||
.replace(/\+/g, '')
|
||||
.replace(/\(/g, '')
|
||||
.replace(/\)/g, '')
|
||||
.split(/\:/)[0]
|
||||
.split('@')[0];
|
||||
|
||||
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('-')) {
|
||||
if (number.length >= 18) {
|
||||
this.logger.verbose('Jid created is group: ' + `${number}@g.us`);
|
||||
number = number.replace(/[^\d-]/g, '');
|
||||
return `${number}@g.us`;
|
||||
}
|
||||
|
||||
number = number.replace(/\D/g, '');
|
||||
|
||||
this.logger.verbose('Jid created is whatsapp: ' + `${number}@s.whatsapp.net`);
|
||||
return `${number}@s.whatsapp.net`;
|
||||
}
|
||||
@@ -1388,6 +1548,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>(
|
||||
number: string,
|
||||
message: T,
|
||||
@@ -1395,15 +1627,14 @@ export class WAStartupService {
|
||||
) {
|
||||
this.logger.verbose('Sending message with typing');
|
||||
|
||||
const jid = this.createJid(number);
|
||||
const numberWA = await this.whatsappNumber({ numbers: [jid] });
|
||||
const numberWA = await this.whatsappNumber({ numbers: [number] });
|
||||
const isWA = numberWA[0];
|
||||
|
||||
if (!isWA.exists && !isJidGroup(isWA.jid) && !isWA.jid.includes('@broadcast')) {
|
||||
throw new BadRequestException(isWA);
|
||||
}
|
||||
|
||||
const sender = isJidGroup(jid) ? jid : isWA.jid;
|
||||
const sender = isWA.jid;
|
||||
|
||||
try {
|
||||
if (options?.delay) {
|
||||
@@ -1412,7 +1643,7 @@ export class WAStartupService {
|
||||
await this.client.presenceSubscribe(sender);
|
||||
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(
|
||||
'Sending presence update: ' + options?.presence ?? 'composing',
|
||||
);
|
||||
@@ -1424,16 +1655,34 @@ export class WAStartupService {
|
||||
this.logger.verbose('Sending presence update: paused');
|
||||
}
|
||||
|
||||
const linkPreview = options?.linkPreview != false ? undefined : false;
|
||||
|
||||
let quoted: WAMessage;
|
||||
|
||||
if (options?.quoted) {
|
||||
quoted = options?.quoted;
|
||||
const m = options?.quoted;
|
||||
|
||||
const msg = m?.message
|
||||
? m
|
||||
: ((await this.getMessage(m.key, true)) as proto.IWebMessageInfo);
|
||||
|
||||
if (!msg) {
|
||||
throw 'Message not found';
|
||||
}
|
||||
|
||||
quoted = msg;
|
||||
this.logger.verbose('Quoted message');
|
||||
}
|
||||
|
||||
let mentions: string[];
|
||||
if (isJidGroup(sender)) {
|
||||
try {
|
||||
const groupMetadata = await this.client.groupMetadata(sender);
|
||||
|
||||
if (!groupMetadata) {
|
||||
throw new NotFoundException('Group not found');
|
||||
}
|
||||
|
||||
if (options?.mentions) {
|
||||
this.logger.verbose('Mentions defined');
|
||||
|
||||
@@ -1448,7 +1697,6 @@ export class WAStartupService {
|
||||
this.logger.verbose('Mentions everyone');
|
||||
|
||||
this.logger.verbose('Getting group metadata');
|
||||
const groupMetadata = await this.client.groupMetadata(sender);
|
||||
mentions = groupMetadata.participants.map((participant) => participant.id);
|
||||
this.logger.verbose('Getting group metadata for mentions');
|
||||
} else {
|
||||
@@ -1456,7 +1704,8 @@ export class WAStartupService {
|
||||
mentions = options.mentions.mentioned.map((mention) => {
|
||||
const jid = this.createJid(mention);
|
||||
if (isJidGroup(jid)) {
|
||||
throw new BadRequestException('Mentions must be a number');
|
||||
return null;
|
||||
// throw new BadRequestException('Mentions must be a number');
|
||||
}
|
||||
return jid;
|
||||
});
|
||||
@@ -1475,9 +1724,9 @@ export class WAStartupService {
|
||||
if (
|
||||
!message['audio'] &&
|
||||
!message['poll'] &&
|
||||
!message['linkPreview'] &&
|
||||
!message['sticker'] &&
|
||||
!sender.includes('@broadcast')
|
||||
!message['conversation'] &&
|
||||
sender !== 'status@broadcast'
|
||||
) {
|
||||
if (!message['audio']) {
|
||||
this.logger.verbose('Sending message');
|
||||
@@ -1495,18 +1744,20 @@ export class WAStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
if (message['linkPreview']) {
|
||||
if (message['conversation']) {
|
||||
this.logger.verbose('Sending message');
|
||||
return await this.client.sendMessage(
|
||||
sender,
|
||||
{
|
||||
text: message['linkPreview'].text,
|
||||
text: message['conversation'],
|
||||
mentions,
|
||||
linkPreview: linkPreview,
|
||||
} as unknown as AnyMessageContent,
|
||||
option as unknown as MiscMessageGenerationOptions,
|
||||
);
|
||||
}
|
||||
|
||||
if (sender.includes('@broadcast')) {
|
||||
if (sender === 'status@broadcast') {
|
||||
this.logger.verbose('Sending message');
|
||||
return await this.client.sendMessage(
|
||||
sender,
|
||||
@@ -1582,19 +1833,6 @@ export class WAStartupService {
|
||||
);
|
||||
}
|
||||
|
||||
public async linkPreview(data: SendLinkPreviewDto) {
|
||||
this.logger.verbose('Sending link preview');
|
||||
return await this.sendMessageWithTyping(
|
||||
data.number,
|
||||
{
|
||||
linkPreview: {
|
||||
text: data.linkPreview.text,
|
||||
},
|
||||
},
|
||||
data?.options,
|
||||
);
|
||||
}
|
||||
|
||||
public async pollMessage(data: SendPollDto) {
|
||||
this.logger.verbose('Sending poll message');
|
||||
return await this.sendMessageWithTyping(
|
||||
@@ -1730,8 +1968,10 @@ export class WAStartupService {
|
||||
|
||||
public async statusMessage(data: SendStatusDto) {
|
||||
this.logger.verbose('Sending status message');
|
||||
const status = await this.formatStatusMessage(data.statusMessage);
|
||||
|
||||
return await this.sendMessageWithTyping('status@broadcast', {
|
||||
status: await this.formatStatusMessage(data.statusMessage),
|
||||
status,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1932,26 +2172,45 @@ export class WAStartupService {
|
||||
|
||||
public async audioWhatsapp(data: SendAudioDto) {
|
||||
this.logger.verbose('Sending audio whatsapp');
|
||||
const convert = await this.processAudio(data.audioMessage.audio, data.number);
|
||||
if (typeof convert === 'string') {
|
||||
const audio = fs.readFileSync(convert).toString('base64');
|
||||
const result = this.sendMessageWithTyping<AnyMessageContent>(
|
||||
data.number,
|
||||
{
|
||||
audio: Buffer.from(audio, 'base64'),
|
||||
ptt: true,
|
||||
mimetype: 'audio/mp4',
|
||||
},
|
||||
{ presence: 'recording', delay: data?.options?.delay },
|
||||
);
|
||||
|
||||
fs.unlinkSync(convert);
|
||||
this.logger.verbose('Converted audio deleted');
|
||||
|
||||
return result;
|
||||
} else {
|
||||
throw new InternalServerErrorException(convert);
|
||||
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);
|
||||
if (typeof convert === 'string') {
|
||||
const audio = fs.readFileSync(convert).toString('base64');
|
||||
const result = this.sendMessageWithTyping<AnyMessageContent>(
|
||||
data.number,
|
||||
{
|
||||
audio: Buffer.from(audio, 'base64'),
|
||||
ptt: true,
|
||||
mimetype: 'audio/mp4',
|
||||
},
|
||||
{ presence: 'recording', delay: data?.options?.delay },
|
||||
);
|
||||
|
||||
fs.unlinkSync(convert);
|
||||
this.logger.verbose('Converted audio deleted');
|
||||
|
||||
return result;
|
||||
} else {
|
||||
throw new InternalServerErrorException(convert);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -2064,6 +2323,11 @@ export class WAStartupService {
|
||||
result += `URL:${contact.url}\n`;
|
||||
}
|
||||
|
||||
if (!contact.wuid) {
|
||||
this.logger.verbose('Wuid defined');
|
||||
contact.wuid = this.createJid(contact.phoneNumber);
|
||||
}
|
||||
|
||||
result +=
|
||||
`item1.TEL;waid=${contact.wuid}:${contact.phoneNumber}\n` +
|
||||
'item1.X-ABLabel:Celular\n' +
|
||||
@@ -2109,7 +2373,8 @@ export class WAStartupService {
|
||||
|
||||
const onWhatsapp: OnWhatsAppDto[] = [];
|
||||
for await (const number of data.numbers) {
|
||||
const jid = this.createJid(number);
|
||||
let jid = this.createJid(number);
|
||||
|
||||
if (isJidGroup(jid)) {
|
||||
const group = await this.findGroup({ groupJid: jid }, 'inner');
|
||||
|
||||
@@ -2117,6 +2382,7 @@ export class WAStartupService {
|
||||
|
||||
onWhatsapp.push(new OnWhatsAppDto(group.id, !!group?.id, group?.subject));
|
||||
} else {
|
||||
jid = !jid.startsWith('+') ? `+${jid}` : jid;
|
||||
const verify = await this.client.onWhatsApp(jid);
|
||||
|
||||
const result = verify[0];
|
||||
@@ -2296,6 +2562,9 @@ export class WAStartupService {
|
||||
this.logger.verbose('Fetching contacts');
|
||||
if (query?.where) {
|
||||
query.where.owner = this.instance.name;
|
||||
if (query.where?.id) {
|
||||
query.where.id = this.createJid(query.where.id);
|
||||
}
|
||||
} else {
|
||||
query = {
|
||||
where: {
|
||||
@@ -2309,6 +2578,9 @@ export class WAStartupService {
|
||||
public async fetchMessages(query: MessageQuery) {
|
||||
this.logger.verbose('Fetching messages');
|
||||
if (query?.where) {
|
||||
if (query.where?.key?.remoteJid) {
|
||||
query.where.key.remoteJid = this.createJid(query.where.key.remoteJid);
|
||||
}
|
||||
query.where.owner = this.instance.name;
|
||||
} else {
|
||||
query = {
|
||||
@@ -2324,6 +2596,9 @@ export class WAStartupService {
|
||||
public async fetchStatusMessage(query: MessageUpQuery) {
|
||||
this.logger.verbose('Fetching status messages');
|
||||
if (query?.where) {
|
||||
if (query.where?.remoteJid) {
|
||||
query.where.remoteJid = this.createJid(query.where.remoteJid);
|
||||
}
|
||||
query.where.owner = this.instance.name;
|
||||
} else {
|
||||
query = {
|
||||
@@ -2368,8 +2643,19 @@ export class WAStartupService {
|
||||
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) {
|
||||
throw new InternalServerErrorException(
|
||||
'Error updating privacy settings',
|
||||
@@ -2378,29 +2664,29 @@ export class WAStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
public async fetchBusinessProfile(number: string) {
|
||||
public async fetchBusinessProfile(number: string): Promise<NumberBusiness> {
|
||||
this.logger.verbose('Fetching business profile');
|
||||
try {
|
||||
let jid;
|
||||
|
||||
if (!number) {
|
||||
jid = this.instance.wuid;
|
||||
} else {
|
||||
jid = this.createJid(number);
|
||||
}
|
||||
const jid = number ? this.createJid(number) : this.instance.wuid;
|
||||
|
||||
const profile = await this.client.getBusinessProfile(jid);
|
||||
this.logger.verbose('Trying to get business profile');
|
||||
|
||||
if (!profile) {
|
||||
const info = await this.whatsappNumber({ numbers: [jid] });
|
||||
|
||||
return {
|
||||
exists: false,
|
||||
message: 'Business profile not found',
|
||||
isBusiness: false,
|
||||
message: 'Not is business profile',
|
||||
...info?.shift(),
|
||||
};
|
||||
}
|
||||
|
||||
this.logger.verbose('Business profile fetched');
|
||||
return profile;
|
||||
return {
|
||||
isBusiness: true,
|
||||
...profile,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new InternalServerErrorException(
|
||||
'Error updating profile name',
|
||||
@@ -2645,9 +2931,7 @@ export class WAStartupService {
|
||||
const msg = `${description}\n\n${inviteUrl}`;
|
||||
|
||||
const message = {
|
||||
linkPreview: {
|
||||
text: msg,
|
||||
},
|
||||
conversation: msg,
|
||||
};
|
||||
|
||||
for await (const number of numbers) {
|
||||
|
||||
@@ -9,6 +9,7 @@ export enum Events {
|
||||
MESSAGES_SET = 'messages.set',
|
||||
MESSAGES_UPSERT = 'messages.upsert',
|
||||
MESSAGES_UPDATE = 'messages.update',
|
||||
MESSAGES_DELETE = 'messages.delete',
|
||||
SEND_MESSAGE = 'send.message',
|
||||
CONTACTS_SET = 'contacts.set',
|
||||
CONTACTS_UPSERT = 'contacts.upsert',
|
||||
@@ -24,9 +25,15 @@ export enum Events {
|
||||
}
|
||||
|
||||
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 = {
|
||||
qrcode?: QrCode;
|
||||
pairingCode?: string;
|
||||
authState?: { state: AuthenticationState; saveCreds: () => void };
|
||||
name?: string;
|
||||
wuid?: string;
|
||||
@@ -50,6 +57,12 @@ export declare namespace wa {
|
||||
sign_msg?: boolean;
|
||||
};
|
||||
|
||||
export type LocalSettings = {
|
||||
reject_call?: boolean;
|
||||
msg_call?: string;
|
||||
groups_ignore?: boolean;
|
||||
};
|
||||
|
||||
export type StateConnection = {
|
||||
instance?: string;
|
||||
state?: WAConnectionState | 'refused';
|
||||
@@ -62,6 +75,7 @@ export declare namespace wa {
|
||||
| 'SERVER_ACK'
|
||||
| 'DELIVERY_ACK'
|
||||
| 'READ'
|
||||
| 'DELETED'
|
||||
| 'PLAYED';
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
MessageUpModel,
|
||||
ChatwootModel,
|
||||
WebhookModel,
|
||||
SettingsModel,
|
||||
} from './models';
|
||||
import { dbserver } from '../db/db.connect';
|
||||
import { WebhookRepository } from './repository/webhook.repository';
|
||||
@@ -34,6 +35,9 @@ import { WAStartupService } from './services/whatsapp.service';
|
||||
import { delay } from '@whiskeysockets/baileys';
|
||||
import { Events } from './types/wa.types';
|
||||
import { RedisCache } from '../db/redis.client';
|
||||
import { SettingsRepository } from './repository/settings.repository';
|
||||
import { SettingsService } from './services/settings.service';
|
||||
import { SettingsController } from './controllers/settings.controller';
|
||||
|
||||
const logger = new Logger('WA MODULE');
|
||||
|
||||
@@ -43,6 +47,7 @@ const contactRepository = new ContactRepository(ContactModel, configService);
|
||||
const messageUpdateRepository = new MessageUpRepository(MessageUpModel, configService);
|
||||
const webhookRepository = new WebhookRepository(WebhookModel, configService);
|
||||
const chatwootRepository = new ChatwootRepository(ChatwootModel, configService);
|
||||
const settingsRepository = new SettingsRepository(SettingsModel, configService);
|
||||
const authRepository = new AuthRepository(AuthModel, configService);
|
||||
|
||||
export const repository = new RepositoryBroker(
|
||||
@@ -52,6 +57,7 @@ export const repository = new RepositoryBroker(
|
||||
messageUpdateRepository,
|
||||
webhookRepository,
|
||||
chatwootRepository,
|
||||
settingsRepository,
|
||||
authRepository,
|
||||
configService,
|
||||
dbserver?.getClient(),
|
||||
@@ -72,10 +78,14 @@ const webhookService = new WebhookService(waMonitor);
|
||||
|
||||
export const webhookController = new WebhookController(webhookService);
|
||||
|
||||
const chatwootService = new ChatwootService(waMonitor);
|
||||
const chatwootService = new ChatwootService(waMonitor, configService);
|
||||
|
||||
export const chatwootController = new ChatwootController(chatwootService, configService);
|
||||
|
||||
const settingsService = new SettingsService(waMonitor);
|
||||
|
||||
export const settingsController = new SettingsController(settingsService);
|
||||
|
||||
export const instanceController = new InstanceController(
|
||||
waMonitor,
|
||||
configService,
|
||||
@@ -91,111 +101,4 @@ export const sendMessageController = new SendMessageController(waMonitor);
|
||||
export const chatController = new ChatController(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');
|
||||
|
||||
Reference in New Issue
Block a user