Compare commits
107 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97abb256d5 | ||
|
|
a342911dae | ||
|
|
1f43563295 | ||
|
|
f23ebf1e99 | ||
|
|
d66b751c2e | ||
|
|
6a7c76a9ba | ||
|
|
2338787dbb | ||
|
|
a216c9cc37 | ||
|
|
b7da8d2193 | ||
|
|
41f191902b | ||
|
|
37397c7a69 | ||
|
|
153695288e | ||
|
|
a5e29758a4 | ||
|
|
964427e533 | ||
|
|
0a925df2a9 | ||
|
|
db95de6731 | ||
|
|
bc2191eae6 | ||
|
|
9fed844e59 | ||
|
|
a2c125ee90 | ||
|
|
a2779612be | ||
|
|
f51c3b6519 | ||
|
|
870968a7c5 | ||
|
|
c4f39ab85c | ||
|
|
8cb431ad40 | ||
|
|
77f98c2438 | ||
|
|
d7d0e5ec82 | ||
|
|
2519efb3ea | ||
|
|
b1c527cb71 | ||
|
|
b87f687e55 | ||
|
|
a62d27ffc2 | ||
|
|
a9a1c6c49b | ||
|
|
4989d6ddfa | ||
|
|
fd01b9de36 | ||
|
|
1017010e15 | ||
|
|
e0bd06489f | ||
|
|
4a6301c2af | ||
|
|
bca830dc54 | ||
|
|
77bde5325e | ||
|
|
bdca506dd4 | ||
|
|
34faaa28ca | ||
|
|
a08d433d0a | ||
|
|
6d08162012 | ||
|
|
2481650028 | ||
|
|
5e551fee41 | ||
|
|
e05690af4c | ||
|
|
8e65dd0546 | ||
|
|
509442a2f3 | ||
|
|
a5102d1b94 | ||
|
|
19ee2b3f34 | ||
|
|
5d29c2d881 | ||
|
|
a08bbab9dc | ||
|
|
84f6394f1f | ||
|
|
d359949310 | ||
|
|
30cd8a03eb | ||
|
|
1b7015c0df | ||
|
|
55b14641e0 | ||
|
|
4936d7fcc6 | ||
|
|
b8fa43296d | ||
|
|
631dd01c92 | ||
|
|
fdc72bc84e | ||
|
|
c63da9cd6e | ||
|
|
849d570bcb | ||
|
|
85e6efb8b0 | ||
|
|
485c8c3113 | ||
|
|
f2d0a8eb8c | ||
|
|
9201bc1022 | ||
|
|
2847a95c57 | ||
|
|
fc30bb9852 | ||
|
|
0f360d34e8 | ||
|
|
ec435e086f | ||
|
|
1efe7f7cde | ||
|
|
5759341c52 | ||
|
|
ea9ba27f22 | ||
|
|
a28bbce1f9 | ||
|
|
75b48aa8ac | ||
|
|
573e56cd8c | ||
|
|
ab28b4c0c5 | ||
|
|
55e36f24a5 | ||
|
|
ea8d6bf6c8 | ||
|
|
75b8bcb853 | ||
|
|
2b2cc2effc | ||
|
|
ae1c5908ae | ||
|
|
f067cb99c8 | ||
|
|
7e7d3a1659 | ||
|
|
c5f364f3e4 | ||
|
|
2a6366ef85 | ||
|
|
66fa855485 | ||
|
|
829b078b25 | ||
|
|
5cf1eacd1f | ||
|
|
7041cd7483 | ||
|
|
2fa2bbf847 | ||
|
|
97d0339bdf | ||
|
|
84e03db79d | ||
|
|
a54b38e150 | ||
|
|
ce254548cc | ||
|
|
6a6a1f0a10 | ||
|
|
c9e18ea06f | ||
|
|
264eae5184 | ||
|
|
0a27f6835a | ||
|
|
53627ca9ea | ||
|
|
3c8a3a5219 | ||
|
|
bdadc1c6b7 | ||
|
|
a66233e445 | ||
|
|
1b1f60da9a | ||
|
|
4e78ac9cea | ||
|
|
15adf4463f | ||
|
|
7c2e9f8f4b |
5
.gitignore
vendored
@@ -11,6 +11,8 @@ yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
/docker-compose-data
|
||||
|
||||
# Package
|
||||
/yarn.lock
|
||||
/package-lock.json
|
||||
@@ -30,3 +32,6 @@ lerna-debug.log*
|
||||
!/instances/.gitkeep
|
||||
/test/
|
||||
/src/env.yml
|
||||
/store
|
||||
|
||||
/temp/*
|
||||
|
||||
100
CHANGELOG.md
Normal file
@@ -0,0 +1,100 @@
|
||||
# 1.1.3 (2023-07-06 11:43)
|
||||
|
||||
### Features
|
||||
|
||||
* Added configuration for Baileys log level in env
|
||||
* Added audio to mp4 converter in optionally get Base64 From MediaMessage
|
||||
* Added organization name in vcard
|
||||
* Added email in vcard
|
||||
* Added url in vcard
|
||||
* Added verbose logs
|
||||
|
||||
### Fixed
|
||||
|
||||
* Added timestamp internally in urls to avoid caching
|
||||
* Correction in decryption of poll votes
|
||||
* Change in the way the api sent and saved the sent messages, now it goes in the messages.upsert event
|
||||
* Fixed cash when sending stickers via url
|
||||
* Improved how Redis works for instances
|
||||
* Fixed problem when disconnecting the instance it removes the instance
|
||||
* Fixed problem sending ack when preview is done by me
|
||||
* Adjust in store files
|
||||
|
||||
# 1.1.2 (2023-06-28 13:43)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed baileys version in package.json
|
||||
* Fixed problem that did not validate if the token passed in create instance already existed
|
||||
* Fixed problem that does not delete instance files in server mode
|
||||
|
||||
# 1.1.1 (2023-06-28 10:27)
|
||||
|
||||
### Features
|
||||
|
||||
* Added group invitation sending
|
||||
* Added webhook configuration per event in the individual instance registration
|
||||
|
||||
### Fixed
|
||||
|
||||
* Adjust dockerfile variables
|
||||
|
||||
# 1.1.0 (2023-06-21 11:17)
|
||||
|
||||
### Features
|
||||
|
||||
* Improved fetch instances endpoint, now it also fetch other instances even if they are not connected
|
||||
* Added conversion of audios for sending recorded audio, now it is possible to send mp3 audios and not just ogg
|
||||
* Route to fetch all groups that the connection is part of
|
||||
* Route to fetch all privacy settings
|
||||
* Route to update the privacy settings
|
||||
* Route to update group subject
|
||||
* Route to update group description
|
||||
* Route to accept invite code
|
||||
* Added configuration of events by webhook of instances
|
||||
* Now the api key can be exposed in fetch instances if the EXPOSE_IN_FETCH_INSTANCES variable is set to true
|
||||
* Added option to generate qrcode as soon as the instance is created
|
||||
* The created instance token can now also be optionally defined manually in the creation endpoint
|
||||
* Route to send Sticker
|
||||
|
||||
### Fixed
|
||||
|
||||
* Adjust dockerfile variables
|
||||
* tweaks in docker-compose to pass variables
|
||||
* Adjust the route getProfileBusiness to fetchProfileBusiness
|
||||
* fix error after logout and try to get status or to connect again
|
||||
* fix sending narrated audio on whatsapp android and ios
|
||||
* fixed the problem of not disabling the global webhook by the variable
|
||||
* Adjustment in the recording of temporary files and periodic cleaning
|
||||
* Fix for container mode also work only with files
|
||||
* Remove recording of old messages on sync
|
||||
|
||||
# 1.0.9 (2023-06-10)
|
||||
|
||||
### Fixed
|
||||
|
||||
* Adjust dockerfile variables
|
||||
|
||||
# 1.0.8 (2023-06-09)
|
||||
|
||||
### Features
|
||||
|
||||
* Added Docker compose file
|
||||
* Added ChangeLog file
|
||||
|
||||
# 1.0.7 (2023-06-08)
|
||||
|
||||
### Features
|
||||
|
||||
* Ghost mention
|
||||
* Mention in reply
|
||||
* Profile photo change
|
||||
* Profile name change
|
||||
* Profile status change
|
||||
* Sending a poll
|
||||
* Creation of LinkPreview if message contains URL
|
||||
* New webhooks system, which can be separated into a url per event
|
||||
* Sending the local webhook url as destination in the webhook data for webhook redirection
|
||||
* Startup modes, server or container
|
||||
* Server Mode works normally as everyone is used to
|
||||
* Container mode made to use one instance per container, when starting the application an instance is already created and the qrcode is generated and it starts sending webhook without having to call it manually, it only allows one instance at a time.
|
||||
80
Docker/.env
@@ -1,80 +0,0 @@
|
||||
CORS_ORIGIN='*' # Or separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'
|
||||
CORS_METHODS='POST,GET,PUT,DELETE'
|
||||
CORS_CREDENTIALS=true
|
||||
|
||||
# Determine the logs to be displayed
|
||||
LOG_LEVEL='ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK'
|
||||
LOG_COLOR=true
|
||||
|
||||
# Determine how long the instance should be deleted from memory in case of no connection.
|
||||
# Default time: 5 minutes
|
||||
# If you don't even want an expiration, enter the value false
|
||||
DEL_INSTANCE=5
|
||||
|
||||
# Temporary data storage
|
||||
STORE_CLEANING_INTERVAL=7200 # seconds ===2h
|
||||
STORE_MESSAGE=true
|
||||
STORE_CONTACTS=false
|
||||
STORE_CHATS=false
|
||||
|
||||
# Permanent data storage
|
||||
DATABASE_ENABLED=false
|
||||
DATABASE_CONNECTION_URI='<uri>'
|
||||
DATABASE_CONNECTION_DB_PREFIX_NAME='evolution'
|
||||
DATABASE_SAVE_DATA_INSTANCE=false
|
||||
DATABASE_SAVE_DATA_OLD_MESSAGE=false
|
||||
DATABASE_SAVE_DATA_NEW_MESSAGE=true
|
||||
DATABASE_SAVE_MESSAGE_UPDATE=true
|
||||
DATABASE_SAVE_DATA_CONTACTS=true
|
||||
DATABASE_SAVE_DATA_CHATS=true
|
||||
|
||||
REDIS_ENABLED=true
|
||||
REDIS_URI='<uri>/1'
|
||||
REDIS_PREFIX_KEY='evolution'
|
||||
|
||||
# Webhook Settings
|
||||
## Define a global webhook that will listen for enabled events from all instances
|
||||
WEBHOOK_GLOBAL_URL='<url>'
|
||||
WEBHOOK_GLOBAL_ENABLED=false
|
||||
WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=false
|
||||
## Set the events you want to hear
|
||||
WEBHOOK_EVENTS_STATUS_INSTANCE=true
|
||||
WEBHOOK_EVENTS_APPLICATION_STARTUP=true
|
||||
WEBHOOK_EVENTS_QRCODE_UPDATED=true
|
||||
WEBHOOK_EVENTS_MESSAGES_SET=true
|
||||
WEBHOOK_EVENTS_MESSAGES_UPDATE=true
|
||||
WEBHOOK_EVENTS_MESSAGES_UPSERT=true
|
||||
WEBHOOK_EVENTS_SEND_MESSAGE=true
|
||||
WEBHOOK_EVENTS_CONTACTS_SET=true
|
||||
WEBHOOK_EVENTS_CONTACTS_UPSERT=true
|
||||
WEBHOOK_EVENTS_CONTACTS_UPDATE=true
|
||||
WEBHOOK_EVENTS_PRESENCE_UPDATE=true
|
||||
WEBHOOK_EVENTS_CHATS_SET=true
|
||||
WEBHOOK_EVENTS_CHATS_UPSERT=true
|
||||
WEBHOOK_EVENTS_CHATS_UPDATE=true
|
||||
WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
||||
WEBHOOK_EVENTS_GROUPS_UPSERT=false
|
||||
WEBHOOK_EVENTS_GROUPS_UPDATE=false
|
||||
WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=false
|
||||
## This event fires every time a new token is requested via the refresh route
|
||||
WEBHOOK_EVENTS_NEW_JWT_TOKEN=true
|
||||
|
||||
CONFIG_SESSION_PHONE_CLIENT='Evolution API'
|
||||
CONFIG_SESSION_PHONE_NAME='Chrome'
|
||||
|
||||
# Set qrcode display limit
|
||||
QRCODE_LIMIT=6
|
||||
|
||||
# Defines an authentication type for the api
|
||||
AUTHENTICATION_TYPE='jwt' # or '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='t8OOEeISKzpmc3jjcMqBWYSaJsafdefer'
|
||||
## Set the secret key to encrypt and decrypt your token and its expiration time
|
||||
AUTHENTICATION_JWT_EXPIRIN_IN=3600 # seconds - 3600s ===1h | zero (0) - never expires
|
||||
AUTHENTICATION_JWT_SECRET='L0YWtjb2w554WFqPG'
|
||||
|
||||
AUTHENTICATION_INSTANCE_NAME='evolution'
|
||||
AUTHENTICATION_INSTANCE_WEBHOOK_URL='<url>'
|
||||
AUTHENTICATION_INSTANCE_MODE='container' # or 'server'
|
||||
AUTHENTICATION_INSTANCE_WEBHOOK_BY_EVENTS=false
|
||||
27
Docker/mongodb/docker-compose.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
version: '3.3'
|
||||
|
||||
networks:
|
||||
evolution-net:
|
||||
driver: bridge
|
||||
|
||||
services:
|
||||
mongodb:
|
||||
container_name: mongodb
|
||||
image: mongo
|
||||
restart: always
|
||||
volumes:
|
||||
- evolution_mongodb_data:/data/db
|
||||
- evolution_mongodb_configdb:/data/configdb
|
||||
ports:
|
||||
- 27017:27017
|
||||
environment:
|
||||
MONGO_INITDB_ROOT_USERNAME: root
|
||||
MONGO_INITDB_ROOT_PASSWORD: root
|
||||
networks:
|
||||
- evolution-net
|
||||
expose:
|
||||
- 27017
|
||||
|
||||
volumes:
|
||||
evolution_mongodb_data:
|
||||
evolution_mongodb_configdb:
|
||||
27
Docker/redis/docker-compose.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
version: '3.3'
|
||||
|
||||
networks:
|
||||
evolution-net:
|
||||
driver: bridge
|
||||
|
||||
services:
|
||||
redis:
|
||||
image: redis:latest
|
||||
command: >
|
||||
redis-server
|
||||
--port 6379
|
||||
--appendonly yes
|
||||
--save 900 1
|
||||
--save 300 10
|
||||
--save 60 10000
|
||||
--appendfsync everysec
|
||||
volumes:
|
||||
- evolution_redis:/data
|
||||
container_name: redis
|
||||
ports:
|
||||
- 6379:6379
|
||||
networks:
|
||||
- evolution-net
|
||||
|
||||
volumes:
|
||||
evolution_redis:
|
||||
89
Dockerfile
@@ -1,5 +1,9 @@
|
||||
FROM node:16.18-alpine
|
||||
|
||||
LABEL version="1.1.3" description="Api to control whatsapp features through http requests."
|
||||
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
|
||||
LABEL contact="contato@agenciadgcode.com"
|
||||
|
||||
RUN apk update && apk upgrade && \
|
||||
apk add --no-cache git
|
||||
|
||||
@@ -9,76 +13,81 @@ COPY ./package.json .
|
||||
|
||||
ENV DOCKER_ENV=true
|
||||
|
||||
ENV TZ=America/Sao_Paulo
|
||||
ENV SERVER_TYPE="http"
|
||||
ENV SERVER_PORT=8080
|
||||
|
||||
ENV CORS_ORIGIN="*"
|
||||
ENV CORS_METHODS="POST,GET,PUT,DELETE"
|
||||
ENV CORS_CREDENTIALS=true
|
||||
|
||||
ENV LOG_LEVEL="ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK"
|
||||
ENV LOG_LEVEL=$LOG_LEVEL
|
||||
ENV LOG_COLOR=true
|
||||
|
||||
ENV DEL_INSTANCE=false
|
||||
ENV DEL_INSTANCE=$DEL_INSTANCE
|
||||
|
||||
ENV STORE_CLEANING_INTERVAL=7200
|
||||
ENV STORE_MESSAGE=true
|
||||
ENV STORE_CONTACTS=true
|
||||
ENV STORE_CHATS=true
|
||||
ENV STORE_MESSAGES=$STORE_MESSAGE
|
||||
ENV STORE_MESSAGE_UP=$STORE_MESSAGE_UP
|
||||
ENV STORE_CONTACTS=$STORE_CONTACTS
|
||||
ENV STORE_CHATS=$STORE_CHATS
|
||||
|
||||
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 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=false
|
||||
ENV DATABASE_SAVE_DATA_OLD_MESSAGE=false
|
||||
ENV DATABASE_SAVE_DATA_NEW_MESSAGE=true
|
||||
ENV DATABASE_SAVE_MESSAGE_UPDATE=false
|
||||
ENV DATABASE_SAVE_DATA_CONTACTS=true
|
||||
ENV DATABASE_SAVE_DATA_CHATS=true
|
||||
ENV DATABASE_SAVE_DATA_INSTANCE=$DATABASE_SAVE_DATA_INSTANCE
|
||||
ENV DATABASE_SAVE_DATA_OLD_MESSAGE=$DATABASE_SAVE_DATA_OLD_MESSAGE
|
||||
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 REDIS_ENABLED=$REDIS_ENABLED
|
||||
ENV REDIS_URI=$REDIS_URI
|
||||
|
||||
ENV WEBHOOK_GLOBAL_URL=$WEBHOOK_GLOBAL_URL
|
||||
ENV WEBHOOK_GLOBAL_ENABLED=true
|
||||
ENV WEBHOOK_GLOBAL_ENABLED=$WEBHOOK_GLOBAL_ENABLED
|
||||
ENV WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS=$WEBHOOK_GLOBAL_WEBHOOK_BY_EVENTS
|
||||
|
||||
ENV WEBHOOK_EVENTS_STATUS_INSTANCE=true
|
||||
ENV WEBHOOK_EVENTS_APPLICATION_STARTUP=true
|
||||
ENV WEBHOOK_EVENTS_QRCODE_UPDATED=true
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_SET=true
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_UPDATE=true
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_UPSERT=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_CONNECTION_UPDATE=true
|
||||
ENV WEBHOOK_EVENTS_GROUPS_UPSERT=true
|
||||
ENV WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
||||
ENV WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
||||
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_UPDATE=$WEBHOOK_EVENTS_MESSAGES_UPDATE
|
||||
ENV WEBHOOK_EVENTS_MESSAGES_UPSERT=$WEBHOOK_EVENTS_MESSAGES_UPSERT
|
||||
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_EVENTS_NEW_JWT_TOKEN=true
|
||||
ENV WEBHOOK_EVENTS_NEW_JWT_TOKEN=$WEBHOOK_EVENTS_NEW_JWT_TOKEN
|
||||
|
||||
ENV CONFIG_SESSION_PHONE_CLIENT="Evolution API"
|
||||
ENV CONFIG_SESSION_PHONE_NAME="Chrome"
|
||||
ENV CONFIG_SESSION_PHONE_CLIENT=$CONFIG_SESSION_PHONE_CLIENT
|
||||
ENV CONFIG_SESSION_PHONE_NAME=$CONFIG_SESSION_PHONE_NAME
|
||||
|
||||
ENV QRCODE_LIMIT=30
|
||||
ENV QRCODE_LIMIT=$QRCODE_LIMIT
|
||||
|
||||
ENV AUTHENTICATION_TYPE="apikey"
|
||||
ENV AUTHENTICATION_TYPE=$AUTHENTICATION_TYPE
|
||||
|
||||
ENV AUTHENTICATION_API_KEY=$AUTHENTICATION_API_KEY
|
||||
ENV AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=$AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES
|
||||
|
||||
ENV AUTHENTICATION_JWT_EXPIRIN_IN=0
|
||||
ENV AUTHENTICATION_JWT_SECRET="L0YWtjb2w554WFqPG"
|
||||
ENV AUTHENTICATION_JWT_EXPIRIN_IN=$AUTHENTICATION_JWT_EXPIRIN_IN
|
||||
ENV AUTHENTICATION_JWT_SECRET="L=0YWt]b2w[WF>#>:&E`"
|
||||
|
||||
ENV AUTHENTICATION_INSTANCE_NAME=$AUTHENTICATION_INSTANCE_NAME
|
||||
ENV AUTHENTICATION_INSTANCE_WEBHOOK_URL=$AUTHENTICATION_INSTANCE_WEBHOOK_URL
|
||||
ENV AUTHENTICATION_INSTANCE_MODE=$AUTHENTICATION_INSTANCE_MODE
|
||||
ENV AUTHENTICATION_INSTANCE_WEBHOOK_BY_EVENTS=$AUTHENTICATION_INSTANCE_WEBHOOK_BY_EVENTS
|
||||
|
||||
RUN npm install
|
||||
|
||||
|
||||
243
README.md
@@ -1,247 +1,22 @@
|
||||
<h1 align="center">Evolution Api</h1>
|
||||
<!--
|
||||
</br>
|
||||
<hr style="height: 5px;background: #007500;margin: 20px 0;box-shadow: 0px 3px 5px 0px rgb(204 204 204);">-->
|
||||
|
||||
<!-- <div align="center"> -->
|
||||
<div align="center">
|
||||
|
||||
<!-- [](#)
|
||||
[](#)
|
||||
[](https://evolution-api.com/whatsapp)
|
||||
[](https://evolution-api.com/docs/evolution-documentation/getting-started/postman-collection/)
|
||||
[](https://evolution-api.com)
|
||||
[](./LICENSE)
|
||||
[](https://app.picpay.com/user/davidsongomes1998) -->
|
||||
[](https://app.picpay.com/user/davidsongomes1998)
|
||||
|
||||
<!-- </div> -->
|
||||
</div>
|
||||
|
||||
<!-- <div align="center"><img src="./public/images/cover.png"></div>-> -->
|
||||
<div align="center"><img src="./public/images/cover.png"></div>
|
||||
|
||||
## WhatsApp-Api-NodeJs
|
||||
|
||||
This project is based on the [CodeChat](https://github.com/code-chat-br/whatsapp-api). The original project is an implementation of [Baileys](https://github.com/adiwajshing/Baileys), serving as a Restful API service that controls WhatsApp functions.</br>
|
||||
This project is based on the [CodeChat](https://github.com/code-chat-br/whatsapp-api). The original project is an implementation of [Baileys](https://github.com/WhiskeySockets/Baileys), serving as a Restful API service that controls WhatsApp functions.</br>
|
||||
The code allows the creation of multiservice chats, service bots, or any other system that utilizes WhatsApp. The documentation provides instructions on how to set up and use the project, as well as additional information about its features and configuration options.
|
||||
|
||||
## Infrastructure
|
||||
|
||||
### Nvm installation
|
||||
|
||||
```sh
|
||||
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
|
||||
# or
|
||||
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
|
||||
```
|
||||
>
|
||||
> After finishing, restart the terminal to load the new information.
|
||||
>
|
||||
|
||||
### Docker installation \[optional\]
|
||||
|
||||
```sh
|
||||
curl -fsSL https://get.docker.com -o get-docker.sh
|
||||
|
||||
sudo sh get-docker.sh
|
||||
|
||||
sudo usermod -aG docker ${USER}
|
||||
```
|
||||
### Nodejs installation
|
||||
|
||||
```sh
|
||||
nvm install 16.17.0
|
||||
```
|
||||
|
||||
### pm2 installation
|
||||
```sh
|
||||
npm i -g pm2
|
||||
```
|
||||
|
||||
```sh
|
||||
docker --version
|
||||
|
||||
node --version
|
||||
```
|
||||
## MongoDb [optional]
|
||||
|
||||
After installing docker and docker-compose, up the container.
|
||||
- [compose from mongodb](./mongodb/docker-compose.yaml)
|
||||
|
||||
In the same directory where the file is located, run the following command:
|
||||
```sh
|
||||
bash docker.sh
|
||||
```
|
||||
Using the database is optional.
|
||||
|
||||
## Application startup
|
||||
|
||||
Cloning the Repository
|
||||
```
|
||||
git clone https://github.com/code-chat-br/whatsapp-api.git
|
||||
```
|
||||
|
||||
Go to the project directory and install all dependencies.</br>
|
||||
```sh
|
||||
cd whatsapp-api
|
||||
|
||||
npm i
|
||||
```
|
||||
|
||||
Finally, run the command below to start the application:
|
||||
```sh
|
||||
# Under development
|
||||
npm run start
|
||||
|
||||
# In production
|
||||
npm run start:prod
|
||||
|
||||
# pm2
|
||||
pm2 start 'npm run start:prod' --name ApiCodechat
|
||||
```
|
||||
## Authentication
|
||||
|
||||
You can define two authentication **types** for the routes in the **[env file](./src/dev-env.yml)**.
|
||||
Authentications must be inserted in the request header.
|
||||
|
||||
1. **apikey**
|
||||
|
||||
2. **jwt:** A JWT is a standard for authentication and information exchange defined with a signature.
|
||||
|
||||
> Authentications are generated at instance creation time.
|
||||
|
||||
**Note:** There is also the possibility to define a global api key, which can access and control all instances.
|
||||
|
||||
### Connection
|
||||
|
||||
#### Create an instance
|
||||
|
||||
##### HTTP
|
||||
|
||||
> *NOTE:* This key must be inserted in the request header to create an instance.
|
||||
|
||||
```http
|
||||
POST /instance/create HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Content-Type: application/json
|
||||
apikey: t8OOEeISKzpmc3jjcMqBWYSaJH2PIxns
|
||||
|
||||
```
|
||||
##### cURL
|
||||
|
||||
```bash
|
||||
curl --location --request POST 'http://localhost:8080/instance/create' \
|
||||
--header 'Content-Type: application/json' \
|
||||
--header 'apikey: t8OOEeISKzpmc3jjcMqBWYSaJH2PIxns' \
|
||||
--data-raw '{
|
||||
"instanceName": "codechat"
|
||||
}'
|
||||
```
|
||||
### Response
|
||||
|
||||
```ts
|
||||
{
|
||||
"instance": {
|
||||
"instanceName": "codechat",
|
||||
"status": "created"
|
||||
},
|
||||
"hash": {
|
||||
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. [...]"
|
||||
|
||||
// or
|
||||
// "apikey": "88513847-1B0E-4188-8D76-4A2750C9B6C3"
|
||||
}
|
||||
}
|
||||
```
|
||||
#### Connection with qrcode
|
||||
|
||||
##### HTTP
|
||||
|
||||
```http
|
||||
GET /instance/connect/codechat HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. [...]
|
||||
```
|
||||
```http
|
||||
GET /instance/connect/codechat HTTP/1.1
|
||||
Host: localhost:8080
|
||||
apikey: 88513847-1B0E-4188-8D76-4A2750C9B6C3
|
||||
```
|
||||
##### cURL
|
||||
|
||||
```bash
|
||||
curl --location --request GET 'http://localhost:8080/instance/connect/codechat' \
|
||||
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. [...]'
|
||||
```
|
||||
```bash
|
||||
curl --location --request GET 'http://localhost:8080/instance/connect/codechat' \
|
||||
--header 'apikey: 88513847-1B0E-4188-8D76-4A2750C9B6C3'
|
||||
```
|
||||
|
||||
### Response
|
||||
|
||||
```ts
|
||||
{
|
||||
"code": "2@nXSUgRJSBY6T0XJmiFKZ0 [...] ,XsgJhJHYa+0MPpXANdPHHt6Ke/I7O2QyXT/Lsge0uSg=",
|
||||
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhE [...] LkMtqAAAAABJRU5ErkJggg=="
|
||||
}
|
||||
```
|
||||
|
||||
### App in Docker
|
||||
- [docker run](./docker.sh)
|
||||
- [docker-compose](./docker-compose.yml)
|
||||
- [env for docker](./Docker/.env)
|
||||
- [DockerHub-codechat/api](https://hub.docker.com/r/codechat/api)
|
||||
|
||||
After building the application, in the same directory as the files above, run the following command:
|
||||
```sh
|
||||
docker-compose up
|
||||
```
|
||||
## Send Messages
|
||||
| | |
|
||||
|-----|---|
|
||||
| Send Text | ✔ |
|
||||
| Send Buttons | ✔ |
|
||||
| Send Template | ❌ |
|
||||
| Send Media: audio - video - image - document - gif <br></br>base64: ```true``` | ✔ |
|
||||
| Send Media File | ❌ |
|
||||
| Send Audio type WhatsApp | ✔ |
|
||||
| Send Location | ✔ |
|
||||
| Send List | ✔ |
|
||||
| Send Link Preview | ✔ |
|
||||
| Send Contact | ✔ |
|
||||
| Send Reaction - emoji | ✔ |
|
||||
| Send Poll Message | ✔ |
|
||||
|
||||
## Postman collections
|
||||
- [Postman Json](./postman.json)
|
||||
|
||||
## Webhook Events
|
||||
|
||||
| Name | Event | TypeData | Description |
|
||||
|------|-------|-----------|------------|
|
||||
| APPLICATION_STARTUP | application.startup | json | Notifies you when a application startup |
|
||||
| QRCODE_UPDATED | qrcode.updated | json | Sends the base64 of the qrcode for reading |
|
||||
| CONNECTION_UPDATE | connection.update | json | Informs the status of the connection with whatsapp |
|
||||
| MESSAGES_SET | message.set | json | Sends a list of all your messages uploaded on whatsapp</br>This event occurs only once |
|
||||
| MESSAGES_UPSERT | message.upsert | json | Notifies you when a message is received |
|
||||
| MESSAGES_UPDATE | message.update | json | Tells you when a message is updated |
|
||||
| SEND_MESSAGE | send.message | json | Notifies when a message is sent |
|
||||
| CONTACTS_SET | contacts.set | json | Performs initial loading of all contacts</br>This event occurs only once |
|
||||
| CONTACTS_UPSERT | contacts.upsert | json | Reloads all contacts with additional information</br>This event occurs only once |
|
||||
| CONTACTS_UPDATE | contacts.update | json | Informs you when the chat is updated |
|
||||
| PRESENCE_UPDATE | presence.update | json | Informs if the user is online, if he is performing some action like writing or recording and his last seen</br>'unavailable' | 'available' | 'composing' | 'recording' | 'paused' |
|
||||
| CHATS_SET | chats.set | json | Send a list of all loaded chats |
|
||||
| CHATS_UPDATE | chats.update | json | Informs you when the chat is updated |
|
||||
| CHATS_UPSERT | chats.upsert | json | Sends any new chat information |
|
||||
| GROUPS_UPSERT | groups.upsert | JSON | Notifies when a group is created |
|
||||
| GROUPS_UPDATE | groups.update | JSON | Notifies when a group has its information updated |
|
||||
| GROUP_PARTICIPANTS_UPDATE | group-participants.update | JSON | Notifies when an action occurs involving a participant</br>'add' | 'remove' | 'promote' | 'demote' |
|
||||
| NEW_TOKEN | new.jwt | JSON | Notifies when the token (jwt) is updated
|
||||
|
||||
## Env File
|
||||
|
||||
See additional settings that can be applied through the **env** file by clicking **[here](./src/dev-env.yml)**.
|
||||
|
||||
> **⚠️Attention⚠️:** rename the **dev-env.yml** file to **env.yml**.
|
||||
|
||||
## SSL
|
||||
|
||||
To install the SSL certificate, follow the **[instructions](https://certbot.eff.org/instructions?ws=other&os=ubuntufocal)** below.
|
||||
## SSL
|
||||
|
||||
To install the SSL certificate, follow the **[instructions](https://certbot.eff.org/instructions?ws=other&os=ubuntufocal)** below.
|
||||
@@ -254,8 +29,6 @@ This code was produced based on the baileys library and it is still under develo
|
||||
|
||||
# Donate to the project.
|
||||
|
||||
|
||||
<div align="center">
|
||||
#### PicPay
|
||||
|
||||
<div align="center">
|
||||
|
||||
102
docker-compose.yaml
Normal file
@@ -0,0 +1,102 @@
|
||||
version: '3.3'
|
||||
|
||||
networks:
|
||||
evolution-net:
|
||||
driver: bridge
|
||||
|
||||
services:
|
||||
api:
|
||||
container_name: evolution_api
|
||||
image: evolution/api:local
|
||||
ports:
|
||||
- 8080:8080
|
||||
volumes:
|
||||
- evolution_instances:/evolution/instances
|
||||
- evolution_store:/evolution/store
|
||||
environment:
|
||||
- LOG_LEVEL=ERROR,WARN,DEBUG,INFO,LOG,VERBOSE,DARK,WEBHOOKS
|
||||
- LOG_BAILEYS=error
|
||||
# Determine how long the instance should be deleted from memory in case of no connection.
|
||||
# Default time: 5 minutes
|
||||
# If you don't even want an expiration, enter the value false
|
||||
- DEL_INSTANCE=false # 5 or false
|
||||
# Temporary data storage
|
||||
- STORE_MESSAGES=true
|
||||
- STORE_MESSAGE_UP=true
|
||||
- STORE_CONTACTS=true
|
||||
- STORE_CHATS=true
|
||||
- CLEAN_STORE_CLEANING_INTERVAL=7200 # seconds === 2h
|
||||
- CLEAN_STORE_MESSAGES=true
|
||||
- CLEAN_STORE_MESSAGE_UP=true
|
||||
- CLEAN_STORE_CONTACTS=true
|
||||
- CLEAN_STORE_CHATS=true
|
||||
# Permanent data storage
|
||||
- DATABASE_ENABLED=false
|
||||
- DATABASE_CONNECTION_URI=mongodb://root:root@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true
|
||||
- DATABASE_CONNECTION_DB_PREFIX_NAME=evolution
|
||||
# Choose the data you want to save in the application's database or store
|
||||
- DATABASE_SAVE_DATA_INSTANCE=true
|
||||
- DATABASE_SAVE_DATA_OLD_MESSAGE=false
|
||||
- DATABASE_SAVE_DATA_NEW_MESSAGE=true
|
||||
- DATABASE_SAVE_MESSAGE_UPDATE=true
|
||||
- DATABASE_SAVE_DATA_CONTACTS=true
|
||||
- DATABASE_SAVE_DATA_CHATS=true
|
||||
- REDIS_ENABLED=true
|
||||
- REDIS_URI=redis://redis:6379/1
|
||||
- REDIS_PREFIX_KEY=evolution
|
||||
# Webhook Settings
|
||||
# Define a global webhook that will listen for enabled events from all instances
|
||||
- WEBHOOK_GLOBAL_URL=<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
|
||||
# Automatically maps webhook paths
|
||||
# Set the events you want to hear
|
||||
- WEBHOOK_EVENTS_APPLICATION_STARTUP=false
|
||||
- WEBHOOK_EVENTS_QRCODE_UPDATED=true
|
||||
- WEBHOOK_EVENTS_MESSAGES_SET=true
|
||||
- WEBHOOK_EVENTS_MESSAGES_UPSERT=true
|
||||
- WEBHOOK_EVENTS_MESSAGES_UPDATE=true
|
||||
- WEBHOOK_EVENTS_CONTACTS_SET=true
|
||||
- WEBHOOK_EVENTS_CONTACTS_UPSERT=true
|
||||
- WEBHOOK_EVENTS_CONTACTS_UPDATE=true
|
||||
- WEBHOOK_EVENTS_PRESENCE_UPDATE=true
|
||||
- WEBHOOK_EVENTS_CHATS_SET=true
|
||||
- WEBHOOK_EVENTS_CHATS_UPSERT=true
|
||||
- WEBHOOK_EVENTS_CHATS_UPDATE=true
|
||||
- WEBHOOK_EVENTS_CHATS_DELETE=true
|
||||
- WEBHOOK_EVENTS_GROUPS_UPSERT=true
|
||||
- WEBHOOK_EVENTS_GROUPS_UPDATE=true
|
||||
- WEBHOOK_EVENTS_GROUP_PARTICIPANTS_UPDATE=true
|
||||
- WEBHOOK_EVENTS_CONNECTION_UPDATE=true
|
||||
# This event fires every time a new token is requested via the refresh route
|
||||
- WEBHOOK_EVENTS_NEW_JWT_TOKEN=true
|
||||
# Name that will be displayed on smartphone connection
|
||||
- CONFIG_SESSION_PHONE_CLIENT=Evolution API
|
||||
- CONFIG_SESSION_PHONE_NAME=chrome # chrome | firefox | edge | opera | safari
|
||||
# Set qrcode display limit
|
||||
- QRCODE_LIMIT=30
|
||||
# Defines an authentication type for the api
|
||||
- AUTHENTICATION_TYPE=apikey # jwt or 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
|
||||
# Expose the api key on return from fetch instances
|
||||
- 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
|
||||
# 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>
|
||||
command: ['node', './dist/src/main.js']
|
||||
networks:
|
||||
- evolution-net
|
||||
expose:
|
||||
- 8080
|
||||
|
||||
volumes:
|
||||
evolution_instances:
|
||||
evolution_store:
|
||||
20
docker.sh
Executable file
@@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
|
||||
NET='evolution-net'
|
||||
IMAGE='evolution/api:local'
|
||||
|
||||
if !(docker network ls | grep ${NET} > /dev/null)
|
||||
then
|
||||
docker network create -d bridge ${NET}
|
||||
fi
|
||||
|
||||
# sudo mkdir -p ./docker-data/instances
|
||||
# sudo mkdir -p ./docker-data/mongodb
|
||||
# sudo mkdir -p ./docker-data/mongodb/data
|
||||
# sudo mkdir -p ./docker-data/mongodb/configdb
|
||||
# sudo mkdir -p ./docker-data/redis
|
||||
# sudo mkdir -p ./docker-data/redis/data
|
||||
|
||||
docker build -t ${IMAGE} .
|
||||
|
||||
docker compose up -d
|
||||
@@ -1,28 +0,0 @@
|
||||
|
||||
version: '3.8'
|
||||
|
||||
networks:
|
||||
api-net:
|
||||
driver: bridge
|
||||
|
||||
services:
|
||||
mongodb:
|
||||
container_name: mongodb
|
||||
|
||||
# This image already has a single replica set
|
||||
image: mongo
|
||||
|
||||
restart: always
|
||||
volumes:
|
||||
# sudo mkdir -p /data/mongodb
|
||||
- /data/mongodb:/data/db
|
||||
ports:
|
||||
- 26712:27017
|
||||
environment:
|
||||
MONGO_INITDB_ROOT_USERNAME: root
|
||||
# Set a password to access the bank
|
||||
MONGO_INITDB_ROOT_PASSWORD: <password>
|
||||
networks:
|
||||
- api-net
|
||||
expose:
|
||||
- 26712
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "evolution-api",
|
||||
"version": "1.2.0",
|
||||
"version": "1.1.3",
|
||||
"description": "Rest api for communication with WhatsApp",
|
||||
"main": "index.js",
|
||||
"main": "./dist/src/main.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "ts-node --files --transpile-only ./src/main.ts",
|
||||
@@ -41,8 +41,9 @@
|
||||
"homepage": "https://github.com/DavidsonGomes/evolution-api#readme",
|
||||
"dependencies": {
|
||||
"@adiwajshing/keyed-db": "^0.2.4",
|
||||
"@evolution/base": "github:WhiskeySockets/Baileys",
|
||||
"@ffmpeg-installer/ffmpeg": "^1.1.0",
|
||||
"@hapi/boom": "^10.0.1",
|
||||
"@whiskeysockets/baileys": "^6.3.0",
|
||||
"axios": "^1.3.5",
|
||||
"class-validator": "^0.13.2",
|
||||
"compression": "^1.7.4",
|
||||
@@ -50,6 +51,7 @@
|
||||
"cross-env": "^7.0.3",
|
||||
"dayjs": "^1.11.7",
|
||||
"eventemitter2": "^6.4.9",
|
||||
"exiftool-vendored": "^22.0.0",
|
||||
"express": "^4.18.2",
|
||||
"express-async-errors": "^3.1.1",
|
||||
"hbs": "^4.2.0",
|
||||
@@ -66,6 +68,7 @@
|
||||
"qrcode": "^1.5.1",
|
||||
"qrcode-terminal": "^0.12.0",
|
||||
"redis": "^4.6.5",
|
||||
"sharp": "^0.30.7",
|
||||
"uuid": "^9.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
2045
postman.json
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 120 KiB After Width: | Height: | Size: 17 KiB |
|
Before Width: | Height: | Size: 108 KiB After Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 36 KiB |
@@ -2,6 +2,7 @@ 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 };
|
||||
|
||||
@@ -12,10 +13,22 @@ export type Cors = {
|
||||
CREDENTIALS: boolean;
|
||||
};
|
||||
|
||||
export type LogLevel = 'ERROR' | 'WARN' | 'DEBUG' | 'INFO' | 'LOG' | 'VERBOSE' | 'DARK';
|
||||
export type LogBaileys = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
||||
|
||||
export type LogLevel =
|
||||
| 'ERROR'
|
||||
| 'WARN'
|
||||
| 'DEBUG'
|
||||
| 'INFO'
|
||||
| 'LOG'
|
||||
| 'VERBOSE'
|
||||
| 'DARK'
|
||||
| 'WEBHOOKS';
|
||||
|
||||
export type Log = {
|
||||
LEVEL: LogLevel[];
|
||||
COLOR: boolean;
|
||||
BAILEYS: LogBaileys;
|
||||
};
|
||||
|
||||
export type SaveData = {
|
||||
@@ -28,8 +41,16 @@ export type SaveData = {
|
||||
};
|
||||
|
||||
export type StoreConf = {
|
||||
MESSAGES: boolean;
|
||||
MESSAGE_UP: boolean;
|
||||
CONTACTS: boolean;
|
||||
CHATS: boolean;
|
||||
};
|
||||
|
||||
export type CleanStoreConf = {
|
||||
CLEANING_INTERVAL: number;
|
||||
MESSAGES: boolean;
|
||||
MESSAGE_UP: boolean;
|
||||
CONTACTS: boolean;
|
||||
CHATS: boolean;
|
||||
};
|
||||
@@ -56,7 +77,6 @@ export type EventsWebhook = {
|
||||
MESSAGES_SET: boolean;
|
||||
MESSAGES_UPSERT: boolean;
|
||||
MESSAGES_UPDATE: boolean;
|
||||
SEND_MESSAGE: boolean;
|
||||
CONTACTS_SET: boolean;
|
||||
CONTACTS_UPDATE: boolean;
|
||||
CONTACTS_UPSERT: boolean;
|
||||
@@ -78,10 +98,10 @@ export type Instance = {
|
||||
NAME: string;
|
||||
WEBHOOK_URL: string;
|
||||
MODE: string;
|
||||
WEBHOOK_BY_EVENTS: boolean;
|
||||
};
|
||||
export type Auth = {
|
||||
API_KEY: ApiKey;
|
||||
EXPOSE_IN_FETCH_INSTANCES: boolean;
|
||||
JWT: Jwt;
|
||||
TYPE: 'jwt' | 'apikey';
|
||||
INSTANCE: Instance;
|
||||
@@ -105,6 +125,7 @@ export interface Env {
|
||||
CORS: Cors;
|
||||
SSL_CONF: SslConf;
|
||||
STORE: StoreConf;
|
||||
CLEAN_STORE: CleanStoreConf;
|
||||
DATABASE: Database;
|
||||
REDIS: Redis;
|
||||
LOG: Log;
|
||||
@@ -134,12 +155,14 @@ export class ConfigService {
|
||||
this.env.PRODUCTION = process.env?.NODE_ENV === 'PROD';
|
||||
if (process.env?.DOCKER_ENV === 'true') {
|
||||
this.env.SERVER.TYPE = 'http';
|
||||
this.env.SERVER.PORT = Number.parseInt(process.env?.SERVER_PORT ?? '8080');
|
||||
this.env.SERVER.PORT = 8080;
|
||||
}
|
||||
}
|
||||
|
||||
private envYaml(): Env {
|
||||
return load(readFileSync(join(SRC_DIR, 'env.yml'), { encoding: 'utf-8' })) as Env;
|
||||
return load(
|
||||
readFileSync(join(process.cwd(), 'src', 'env.yml'), { encoding: 'utf-8' }),
|
||||
) as Env;
|
||||
}
|
||||
|
||||
private envProcess(): Env {
|
||||
@@ -158,13 +181,20 @@ export class ConfigService {
|
||||
FULLCHAIN: process.env?.SSL_CONF_FULLCHAIN,
|
||||
},
|
||||
STORE: {
|
||||
CLEANING_INTERVAL: Number.isInteger(process.env?.STORE_CLEANING_TERMINAL)
|
||||
? Number.parseInt(process.env.STORE_CLEANING_TERMINAL)
|
||||
: undefined,
|
||||
MESSAGES: process.env?.STORE_MESSAGE === 'true',
|
||||
MESSAGES: process.env?.STORE_MESSAGES === 'true',
|
||||
MESSAGE_UP: process.env?.STORE_MESSAGE_UP === 'true',
|
||||
CONTACTS: process.env?.STORE_CONTACTS === 'true',
|
||||
CHATS: process.env?.STORE_CHATS === 'true',
|
||||
},
|
||||
CLEAN_STORE: {
|
||||
CLEANING_INTERVAL: Number.isInteger(process.env?.CLEAN_STORE_CLEANING_TERMINAL)
|
||||
? Number.parseInt(process.env.CLEAN_STORE_CLEANING_TERMINAL)
|
||||
: undefined,
|
||||
MESSAGES: process.env?.CLEAN_STORE_MESSAGES === 'true',
|
||||
MESSAGE_UP: process.env?.CLEAN_STORE_MESSAGE_UP === 'true',
|
||||
CONTACTS: process.env?.CLEAN_STORE_CONTACTS === 'true',
|
||||
CHATS: process.env?.CLEAN_STORE_CHATS === 'true',
|
||||
},
|
||||
DATABASE: {
|
||||
CONNECTION: {
|
||||
URI: process.env.DATABASE_CONNECTION_URI,
|
||||
@@ -188,9 +218,9 @@ export class ConfigService {
|
||||
LOG: {
|
||||
LEVEL: process.env?.LOG_LEVEL.split(',') as LogLevel[],
|
||||
COLOR: process.env?.LOG_COLOR === 'true',
|
||||
BAILEYS: (process.env?.LOG_BAILEYS as LogBaileys) || 'error',
|
||||
},
|
||||
DEL_INSTANCE:
|
||||
typeof process.env?.DEL_INSTANCE === 'boolean'
|
||||
DEL_INSTANCE: isBooleanString(process.env?.DEL_INSTANCE)
|
||||
? process.env.DEL_INSTANCE === 'true'
|
||||
: Number.parseInt(process.env.DEL_INSTANCE),
|
||||
WEBHOOK: {
|
||||
@@ -205,7 +235,6 @@ 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',
|
||||
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',
|
||||
CONTACTS_UPSERT: process.env?.WEBHOOK_EVENTS_CONTACTS_UPSERT === 'true',
|
||||
@@ -234,6 +263,8 @@ export class ConfigService {
|
||||
API_KEY: {
|
||||
KEY: process.env.AUTHENTICATION_API_KEY,
|
||||
},
|
||||
EXPOSE_IN_FETCH_INSTANCES:
|
||||
process.env?.AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES === 'true',
|
||||
JWT: {
|
||||
EXPIRIN_IN: Number.isInteger(process.env?.AUTHENTICATION_JWT_EXPIRIN_IN)
|
||||
? Number.parseInt(process.env.AUTHENTICATION_JWT_EXPIRIN_IN)
|
||||
@@ -244,8 +275,6 @@ export class ConfigService {
|
||||
NAME: process.env.AUTHENTICATION_INSTANCE_NAME,
|
||||
WEBHOOK_URL: process.env.AUTHENTICATION_INSTANCE_WEBHOOK_URL,
|
||||
MODE: process.env.AUTHENTICATION_INSTANCE_MODE,
|
||||
WEBHOOK_BY_EVENTS:
|
||||
process.env.AUTHENTICATION_INSTANCE_WEBHOOK_BY_EVENTS === 'true',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -67,7 +67,6 @@ export class Logger {
|
||||
this.configService.get<Log>('LOG').LEVEL.forEach((level) => types.push(Type[level]));
|
||||
|
||||
const typeValue = typeof value;
|
||||
|
||||
if (types.includes(type)) {
|
||||
if (configService.get<Log>('LOG').COLOR) {
|
||||
console.log(
|
||||
|
||||
@@ -2,13 +2,23 @@ import mongoose from 'mongoose';
|
||||
import { configService, Database } from '../config/env.config';
|
||||
import { Logger } from '../config/logger.config';
|
||||
|
||||
const logger = new Logger('Db Connection');
|
||||
const logger = new Logger('MongoDB');
|
||||
|
||||
export const db = configService.get<Database>('DATABASE');
|
||||
export const dbserver = db.ENABLED
|
||||
? mongoose.createConnection(db.CONNECTION.URI, {
|
||||
const db = configService.get<Database>('DATABASE');
|
||||
export const dbserver = (() => {
|
||||
if (db.ENABLED) {
|
||||
logger.verbose('connecting');
|
||||
const dbs = mongoose.createConnection(db.CONNECTION.URI, {
|
||||
dbName: db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api',
|
||||
})
|
||||
: null;
|
||||
});
|
||||
logger.verbose('connected in ' + db.CONNECTION.URI);
|
||||
logger.info('ON - dbName: ' + dbs['$dbName']);
|
||||
|
||||
db.ENABLED ? logger.info('ON - dbName: ' + dbserver['$dbName']) : null;
|
||||
process.on('beforeExit', () => {
|
||||
logger.verbose('instance destroyed');
|
||||
dbserver.destroy(true, (error) => logger.error(error));
|
||||
});
|
||||
|
||||
return dbs;
|
||||
}
|
||||
})();
|
||||
|
||||
@@ -1,33 +1,64 @@
|
||||
import { createClient, RedisClientType } from '@redis/client';
|
||||
import { Logger } from '../config/logger.config';
|
||||
import { BufferJSON } from '@evolution/base';
|
||||
import { BufferJSON } from '@whiskeysockets/baileys';
|
||||
import { Redis } from '../config/env.config';
|
||||
|
||||
export class RedisCache {
|
||||
constructor(private readonly redisEnv: Partial<Redis>, private instanceName?: string) {
|
||||
this.client = createClient({ url: this.redisEnv.URI });
|
||||
|
||||
this.client.connect();
|
||||
constructor() {
|
||||
this.logger.verbose('instance created');
|
||||
process.on('beforeExit', async () => {
|
||||
this.logger.verbose('instance destroyed');
|
||||
if (this.statusConnection) {
|
||||
this.logger.verbose('instance disconnect');
|
||||
await this.client.disconnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private statusConnection = false;
|
||||
private instanceName: string;
|
||||
private redisEnv: Redis;
|
||||
|
||||
public set reference(reference: string) {
|
||||
this.logger.verbose('set reference: ' + reference);
|
||||
this.instanceName = reference;
|
||||
}
|
||||
|
||||
public async connect(redisEnv: Redis) {
|
||||
this.logger.verbose('connecting');
|
||||
this.client = createClient({ url: redisEnv.URI });
|
||||
this.logger.verbose('connected in ' + redisEnv.URI);
|
||||
await this.client.connect();
|
||||
this.statusConnection = true;
|
||||
this.redisEnv = redisEnv;
|
||||
}
|
||||
|
||||
private readonly logger = new Logger(RedisCache.name);
|
||||
private client: RedisClientType;
|
||||
|
||||
public async instanceKeys(): Promise<string[]> {
|
||||
try {
|
||||
this.logger.verbose('instance keys: ' + this.redisEnv.PREFIX_KEY + ':*');
|
||||
return await this.client.sendCommand(['keys', this.redisEnv.PREFIX_KEY + ':*']);
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async keyExists(key?: string) {
|
||||
if (key) {
|
||||
this.logger.verbose('keyExists: ' + key);
|
||||
return !!(await this.instanceKeys()).find((i) => i === key);
|
||||
}
|
||||
this.logger.verbose('keyExists: ' + this.instanceName);
|
||||
return !!(await this.instanceKeys()).find((i) => i === this.instanceName);
|
||||
}
|
||||
|
||||
public async writeData(field: string, data: any) {
|
||||
try {
|
||||
this.logger.verbose('writeData: ' + field);
|
||||
const json = JSON.stringify(data, BufferJSON.replacer);
|
||||
|
||||
return await this.client.hSet(
|
||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||
field,
|
||||
@@ -40,13 +71,19 @@ export class RedisCache {
|
||||
|
||||
public async readData(field: string) {
|
||||
try {
|
||||
this.logger.verbose('readData: ' + field);
|
||||
const data = await this.client.hGet(
|
||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||
field,
|
||||
);
|
||||
|
||||
if (data) {
|
||||
this.logger.verbose('readData: ' + field + ' success');
|
||||
return JSON.parse(data, BufferJSON.reviver);
|
||||
}
|
||||
|
||||
this.logger.verbose('readData: ' + field + ' not found');
|
||||
return null;
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
@@ -54,6 +91,7 @@ export class RedisCache {
|
||||
|
||||
public async removeData(field: string) {
|
||||
try {
|
||||
this.logger.verbose('removeData: ' + field);
|
||||
return await this.client.hDel(
|
||||
this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||
field,
|
||||
@@ -65,6 +103,7 @@ export class RedisCache {
|
||||
|
||||
public async delAll(hash?: string) {
|
||||
try {
|
||||
this.logger.verbose('instance delAll: ' + hash);
|
||||
return await this.client.del(
|
||||
hash || this.redisEnv.PREFIX_KEY + ':' + this.instanceName,
|
||||
);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
# Choose the server type for the application
|
||||
SERVER:
|
||||
TYPE: http # https
|
||||
PORT: 8083 # 443
|
||||
PORT: 8080 # 443
|
||||
|
||||
CORS:
|
||||
ORIGIN:
|
||||
@@ -36,7 +36,9 @@ LOG:
|
||||
- LOG
|
||||
- VERBOSE
|
||||
- DARK
|
||||
- WEBHOOKS
|
||||
COLOR: true
|
||||
BAILEYS: error # "fatal" | "error" | "warn" | "info" | "debug" | "trace"
|
||||
|
||||
# Determine how long the instance should be deleted from memory in case of no connection.
|
||||
# Default time: 5 minutes
|
||||
@@ -45,8 +47,15 @@ DEL_INSTANCE: false # or false
|
||||
|
||||
# Temporary data storage
|
||||
STORE:
|
||||
CLEANING_INTERVAL: 7200 # seconds === 2h
|
||||
MESSAGE: true
|
||||
MESSAGES: true
|
||||
MESSAGE_UP: true
|
||||
CONTACTS: true
|
||||
CHATS: true
|
||||
|
||||
CLEAN_STORE:
|
||||
CLEANING_INTERVAL: 7200 # 7200 seconds === 2h
|
||||
MESSAGES: true
|
||||
MESSAGE_UP: true
|
||||
CONTACTS: true
|
||||
CHATS: true
|
||||
|
||||
@@ -58,7 +67,7 @@ DATABASE:
|
||||
DB_PREFIX_NAME: evolution
|
||||
# Choose the data you want to save in the application's database or store
|
||||
SAVE_DATA:
|
||||
INSTANCE: true
|
||||
INSTANCE: false
|
||||
OLD_MESSAGE: false
|
||||
NEW_MESSAGE: true
|
||||
MESSAGE_UPDATE: true
|
||||
@@ -77,7 +86,7 @@ WEBHOOK:
|
||||
URL: <url>
|
||||
ENABLED: true
|
||||
# With this option activated, you work with a url per webhook event, respecting the global url and the name of each event
|
||||
WEBHOOK_BY_EVENTS: true
|
||||
WEBHOOK_BY_EVENTS: false
|
||||
# Automatically maps webhook paths
|
||||
# Set the events you want to hear
|
||||
EVENTS:
|
||||
@@ -86,7 +95,6 @@ WEBHOOK:
|
||||
MESSAGES_SET: true
|
||||
MESSAGES_UPSERT: true
|
||||
MESSAGES_UPDATE: true
|
||||
SEND_MESSAGE: true
|
||||
CONTACTS_SET: true
|
||||
CONTACTS_UPSERT: true
|
||||
CONTACTS_UPDATE: true
|
||||
@@ -105,7 +113,7 @@ WEBHOOK:
|
||||
CONFIG_SESSION_PHONE:
|
||||
# Name that will be displayed on smartphone connection
|
||||
CLIENT: 'Evolution API'
|
||||
NAME: Chrome # firefox | edge | opera | safari
|
||||
NAME: chrome # chrome | firefox | edge | opera | safari
|
||||
|
||||
# Set qrcode display limit
|
||||
QRCODE:
|
||||
@@ -113,11 +121,13 @@ QRCODE:
|
||||
|
||||
# Defines an authentication type for the api
|
||||
AUTHENTICATION:
|
||||
TYPE: apikey # or jwt apikey
|
||||
TYPE: apikey # jwt or apikey
|
||||
# Define a global apikey to access all instances
|
||||
API_KEY:
|
||||
# OBS: This key must be inserted in the request header to create an instance.
|
||||
KEY: B6D711FC-DE4D-4FD5-9365-44120E713976
|
||||
KEY: B6D711FCDE4D4FD5936544120E713976
|
||||
# Expose the api key on return from fetch instances
|
||||
EXPOSE_IN_FETCH_INSTANCES: true
|
||||
# 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
|
||||
@@ -125,7 +135,6 @@ AUTHENTICATION:
|
||||
# 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
|
||||
WEBHOOK_BY_EVENTS: false
|
||||
MODE: server # container or server
|
||||
# if you are using container mode, set the container name and the webhook url to default instance
|
||||
NAME: evolution
|
||||
|
||||
@@ -16,8 +16,6 @@ function initWA() {
|
||||
}
|
||||
|
||||
function bootstrap() {
|
||||
initWA();
|
||||
|
||||
const logger = new Logger('SERVER');
|
||||
const app = express();
|
||||
|
||||
@@ -34,8 +32,8 @@ function bootstrap() {
|
||||
methods: [...configService.get<Cors>('CORS').METHODS],
|
||||
credentials: configService.get<Cors>('CORS').CREDENTIALS,
|
||||
}),
|
||||
urlencoded({ extended: true, limit: '50mb' }),
|
||||
json({ limit: '50mb' }),
|
||||
urlencoded({ extended: true, limit: '136mb' }),
|
||||
json({ limit: '136mb' }),
|
||||
compression(),
|
||||
);
|
||||
|
||||
@@ -73,6 +71,8 @@ function bootstrap() {
|
||||
logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT),
|
||||
);
|
||||
|
||||
initWA();
|
||||
|
||||
onUnexpectedError();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
initAuthCreds,
|
||||
proto,
|
||||
SignalDataTypeMap,
|
||||
} from '@evolution/base';
|
||||
} from '@whiskeysockets/baileys';
|
||||
import { configService, Database } from '../config/env.config';
|
||||
import { Logger } from '../config/logger.config';
|
||||
import { dbserver } from '../db/db.connect';
|
||||
|
||||
@@ -4,22 +4,17 @@ import {
|
||||
initAuthCreds,
|
||||
proto,
|
||||
SignalDataTypeMap,
|
||||
} from '@evolution/base';
|
||||
} from '@whiskeysockets/baileys';
|
||||
import { RedisCache } from '../db/redis.client';
|
||||
import { Logger } from '../config/logger.config';
|
||||
import { Redis } from '../config/env.config';
|
||||
|
||||
export async function useMultiFileAuthStateRedisDb(
|
||||
redisEnv: Partial<Redis>,
|
||||
instanceName: string,
|
||||
): Promise<{
|
||||
export async function useMultiFileAuthStateRedisDb(cache: RedisCache): Promise<{
|
||||
state: AuthenticationState;
|
||||
saveCreds: () => Promise<void>;
|
||||
}> {
|
||||
const logger = new Logger(useMultiFileAuthStateRedisDb.name);
|
||||
|
||||
const cache = new RedisCache(redisEnv, instanceName);
|
||||
|
||||
const writeData = async (data: any, key: string): Promise<any> => {
|
||||
try {
|
||||
return await cache.writeData(key, data);
|
||||
|
||||
@@ -27,6 +27,36 @@ export const instanceNameSchema: JSONSchema7 = {
|
||||
properties: {
|
||||
instanceName: { type: 'string' },
|
||||
webhook: { type: 'string' },
|
||||
webhook_by_events: { type: 'boolean' },
|
||||
events: {
|
||||
type: 'array',
|
||||
minItems: 1,
|
||||
items: {
|
||||
type: 'string',
|
||||
enum: [
|
||||
'APPLICATION_STARTUP',
|
||||
'QRCODE_UPDATED',
|
||||
'MESSAGES_SET',
|
||||
'MESSAGES_UPSERT',
|
||||
'MESSAGES_UPDATE',
|
||||
'CONTACTS_SET',
|
||||
'CONTACTS_UPSERT',
|
||||
'CONTACTS_UPDATE',
|
||||
'PRESENCE_UPDATE',
|
||||
'CHATS_SET',
|
||||
'CHATS_UPSERT',
|
||||
'CHATS_UPDATE',
|
||||
'CHATS_DELETE',
|
||||
'GROUPS_UPSERT',
|
||||
'GROUP_UPDATE',
|
||||
'GROUP_PARTICIPANTS_UPDATE',
|
||||
'CONNECTION_UPDATE',
|
||||
'NEW_JWT_TOKEN',
|
||||
],
|
||||
},
|
||||
},
|
||||
qrcode: { type: 'boolean', enum: [true, false] },
|
||||
token: { type: 'string' },
|
||||
},
|
||||
...isNotEmpty('instanceName'),
|
||||
};
|
||||
@@ -180,6 +210,24 @@ export const mediaMessageSchema: JSONSchema7 = {
|
||||
required: ['mediaMessage', 'number'],
|
||||
};
|
||||
|
||||
export const stickerMessageSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
number: { ...numberDefinition },
|
||||
options: { ...optionsSchema },
|
||||
stickerMessage: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
image: { type: 'string' },
|
||||
},
|
||||
required: ['image'],
|
||||
...isNotEmpty('image'),
|
||||
},
|
||||
},
|
||||
required: ['stickerMessage', 'number'],
|
||||
};
|
||||
|
||||
export const audioMessageSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
@@ -331,6 +379,9 @@ export const contactMessageSchema: JSONSchema7 = {
|
||||
description: '"wuid" must be a numeric string',
|
||||
},
|
||||
phoneNumber: { type: 'string', minLength: 10 },
|
||||
organization: { type: 'string' },
|
||||
email: { type: 'string' },
|
||||
url: { type: 'string' },
|
||||
},
|
||||
required: ['fullName', 'wuid', 'phoneNumber'],
|
||||
...isNotEmpty('fullName'),
|
||||
@@ -408,6 +459,36 @@ export const readMessageSchema: JSONSchema7 = {
|
||||
required: ['readMessages'],
|
||||
};
|
||||
|
||||
export const privacySettingsSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
privacySettings: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
readreceipts: { type: 'string', enum: ['all', 'none'] },
|
||||
profile: {
|
||||
type: 'string',
|
||||
enum: ['all', 'contacts', 'contact_blacklist', 'none'],
|
||||
},
|
||||
status: {
|
||||
type: 'string',
|
||||
enum: ['all', 'contacts', 'contact_blacklist', 'none'],
|
||||
},
|
||||
online: { type: 'string', enum: ['all', 'match_last_seen'] },
|
||||
last: { type: 'string', enum: ['all', 'contacts', 'contact_blacklist', 'none'] },
|
||||
groupadd: {
|
||||
type: 'string',
|
||||
enum: ['all', 'contacts', 'contact_blacklist', 'none'],
|
||||
},
|
||||
},
|
||||
required: ['readreceipts', 'profile', 'status', 'online', 'last', 'groupadd'],
|
||||
...isNotEmpty('readreceipts', 'profile', 'status', 'online', 'last', 'groupadd'),
|
||||
},
|
||||
},
|
||||
required: ['privacySettings'],
|
||||
};
|
||||
|
||||
export const archiveChatSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
@@ -587,6 +668,28 @@ export const groupJidSchema: JSONSchema7 = {
|
||||
...isNotEmpty('groupJid'),
|
||||
};
|
||||
|
||||
export const groupSendInviteSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
groupJid: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
numbers: {
|
||||
type: 'array',
|
||||
minItems: 1,
|
||||
uniqueItems: true,
|
||||
items: {
|
||||
type: 'string',
|
||||
minLength: 10,
|
||||
pattern: '\\d+',
|
||||
description: '"numbers" must be an array of numeric strings',
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ['groupJid', 'description', 'numbers'],
|
||||
...isNotEmpty('groupJid', 'description', 'numbers'),
|
||||
};
|
||||
|
||||
export const groupInviteSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
@@ -650,7 +753,7 @@ export const toggleEphemeralSchema: JSONSchema7 = {
|
||||
...isNotEmpty('groupJid', 'expiration'),
|
||||
};
|
||||
|
||||
export const updateGroupPicture: JSONSchema7 = {
|
||||
export const updateGroupPictureSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
@@ -661,6 +764,28 @@ export const updateGroupPicture: JSONSchema7 = {
|
||||
...isNotEmpty('groupJid', 'image'),
|
||||
};
|
||||
|
||||
export const updateGroupSubjectSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
groupJid: { type: 'string' },
|
||||
subject: { type: 'string' },
|
||||
},
|
||||
required: ['groupJid', 'subject'],
|
||||
...isNotEmpty('groupJid', 'subject'),
|
||||
};
|
||||
|
||||
export const updateGroupDescriptionSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
groupJid: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
},
|
||||
required: ['groupJid', 'description'],
|
||||
...isNotEmpty('groupJid', 'description'),
|
||||
};
|
||||
|
||||
// Webhook Schema
|
||||
export const webhookSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
@@ -668,6 +793,33 @@ export const webhookSchema: JSONSchema7 = {
|
||||
properties: {
|
||||
url: { type: 'string' },
|
||||
enabled: { type: 'boolean', enum: [true, false] },
|
||||
events: {
|
||||
type: 'array',
|
||||
minItems: 1,
|
||||
items: {
|
||||
type: 'string',
|
||||
enum: [
|
||||
'APPLICATION_STARTUP',
|
||||
'QRCODE_UPDATED',
|
||||
'MESSAGES_SET',
|
||||
'MESSAGES_UPSERT',
|
||||
'MESSAGES_UPDATE',
|
||||
'CONTACTS_SET',
|
||||
'CONTACTS_UPSERT',
|
||||
'CONTACTS_UPDATE',
|
||||
'PRESENCE_UPDATE',
|
||||
'CHATS_SET',
|
||||
'CHATS_UPSERT',
|
||||
'CHATS_UPDATE',
|
||||
'CHATS_DELETE',
|
||||
'GROUPS_UPSERT',
|
||||
'GROUP_UPDATE',
|
||||
'GROUP_PARTICIPANTS_UPDATE',
|
||||
'CONNECTION_UPDATE',
|
||||
'NEW_JWT_TOKEN',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ['url', 'enabled'],
|
||||
...isNotEmpty('url'),
|
||||
|
||||
@@ -65,6 +65,37 @@ export abstract class RouterBroker {
|
||||
return await execute(instance, ref);
|
||||
}
|
||||
|
||||
public async groupNoValidate<T>(args: DataValidate<T>) {
|
||||
const { request, ClassRef, schema, execute } = args;
|
||||
|
||||
const instance = request.params as unknown as InstanceDto;
|
||||
|
||||
const ref = new ClassRef();
|
||||
|
||||
Object.assign(ref, request.body);
|
||||
|
||||
const v = validate(ref, schema);
|
||||
|
||||
if (!v.valid) {
|
||||
const message: any[] = v.errors.map(({ property, stack, schema }) => {
|
||||
let message: string;
|
||||
if (schema['description']) {
|
||||
message = schema['description'];
|
||||
} else {
|
||||
message = stack.replace('instance.', '');
|
||||
}
|
||||
return {
|
||||
property: property.replace('instance.', ''),
|
||||
message,
|
||||
};
|
||||
});
|
||||
logger.error([...message]);
|
||||
throw new BadRequestException(...message);
|
||||
}
|
||||
|
||||
return await execute(instance, ref);
|
||||
}
|
||||
|
||||
public async groupValidate<T>(args: DataValidate<T>) {
|
||||
const { request, ClassRef, schema, execute } = args;
|
||||
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { proto } from '@evolution/base';
|
||||
import { proto } from '@whiskeysockets/baileys';
|
||||
import {
|
||||
ArchiveChatDto,
|
||||
DeleteMessage,
|
||||
NumberDto,
|
||||
PrivacySettingDto,
|
||||
ProfileNameDto,
|
||||
ProfilePictureDto,
|
||||
ProfileStatusDto,
|
||||
ReadMessageDto,
|
||||
WhatsAppNumberDto,
|
||||
getBase64FromMediaMessageDto,
|
||||
} from '../dto/chat.dto';
|
||||
import { InstanceDto } from '../dto/instance.dto';
|
||||
import { ContactQuery } from '../repository/contact.repository';
|
||||
@@ -44,11 +46,9 @@ export class ChatController {
|
||||
|
||||
public async getBase64FromMediaMessage(
|
||||
{ instanceName }: InstanceDto,
|
||||
message: proto.IWebMessageInfo,
|
||||
data: getBase64FromMediaMessageDto,
|
||||
) {
|
||||
return await this.waMonitor.waInstances[instanceName].getBase64FromMediaMessage(
|
||||
message,
|
||||
);
|
||||
return await this.waMonitor.waInstances[instanceName].getBase64FromMediaMessage(data);
|
||||
}
|
||||
|
||||
public async fetchMessages({ instanceName }: InstanceDto, query: MessageQuery) {
|
||||
@@ -63,11 +63,24 @@ export class ChatController {
|
||||
return await this.waMonitor.waInstances[instanceName].fetchChats();
|
||||
}
|
||||
|
||||
public async getBusinessProfile(
|
||||
public async fetchPrivacySettings({ instanceName }: InstanceDto) {
|
||||
return await this.waMonitor.waInstances[instanceName].fetchPrivacySettings();
|
||||
}
|
||||
|
||||
public async updatePrivacySettings(
|
||||
{ instanceName }: InstanceDto,
|
||||
data: PrivacySettingDto,
|
||||
) {
|
||||
return await this.waMonitor.waInstances[instanceName].updatePrivacySettings(data);
|
||||
}
|
||||
|
||||
public async fetchBusinessProfile(
|
||||
{ instanceName }: InstanceDto,
|
||||
data: ProfilePictureDto,
|
||||
) {
|
||||
return await this.waMonitor.waInstances[instanceName].getBusinessProfile(data.number);
|
||||
return await this.waMonitor.waInstances[instanceName].fetchBusinessProfile(
|
||||
data.number,
|
||||
);
|
||||
}
|
||||
|
||||
public async updateProfileName({ instanceName }: InstanceDto, data: ProfileNameDto) {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import {
|
||||
CreateGroupDto,
|
||||
GroupDescriptionDto,
|
||||
GroupInvite,
|
||||
GroupJid,
|
||||
GroupPictureDto,
|
||||
GroupSendInvite,
|
||||
GroupSubjectDto,
|
||||
GroupToggleEphemeralDto,
|
||||
GroupUpdateParticipantDto,
|
||||
GroupUpdateSettingDto,
|
||||
@@ -23,10 +26,29 @@ export class GroupController {
|
||||
);
|
||||
}
|
||||
|
||||
public async updateGroupSubject(instance: InstanceDto, update: GroupSubjectDto) {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].updateGroupSubject(
|
||||
update,
|
||||
);
|
||||
}
|
||||
|
||||
public async updateGroupDescription(
|
||||
instance: InstanceDto,
|
||||
update: GroupDescriptionDto,
|
||||
) {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].updateGroupDescription(
|
||||
update,
|
||||
);
|
||||
}
|
||||
|
||||
public async findGroupInfo(instance: InstanceDto, groupJid: GroupJid) {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].findGroup(groupJid);
|
||||
}
|
||||
|
||||
public async fetchAllGroups(instance: InstanceDto) {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].fetchAllGroups();
|
||||
}
|
||||
|
||||
public async inviteCode(instance: InstanceDto, groupJid: GroupJid) {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].inviteCode(groupJid);
|
||||
}
|
||||
@@ -35,6 +57,10 @@ export class GroupController {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].inviteInfo(inviteCode);
|
||||
}
|
||||
|
||||
public async sendInvite(instance: InstanceDto, data: GroupSendInvite) {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].sendInvite(data);
|
||||
}
|
||||
|
||||
public async revokeInviteCode(instance: InstanceDto, groupJid: GroupJid) {
|
||||
return await this.waMonitor.waInstances[instance.instanceName].revokeInviteCode(
|
||||
groupJid,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { delay } from '@evolution/base';
|
||||
import { delay } from '@whiskeysockets/baileys';
|
||||
import EventEmitter2 from 'eventemitter2';
|
||||
import { Auth, ConfigService } from '../../config/env.config';
|
||||
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
|
||||
@@ -9,6 +9,8 @@ import { WAMonitoringService } from '../services/monitor.service';
|
||||
import { WAStartupService } from '../services/whatsapp.service';
|
||||
import { WebhookService } from '../services/webhook.service';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { wa } from '../types/wa.types';
|
||||
import { RedisCache } from '../../db/redis.client';
|
||||
|
||||
export class InstanceController {
|
||||
constructor(
|
||||
@@ -18,16 +20,22 @@ export class InstanceController {
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly authService: AuthService,
|
||||
private readonly webhookService: WebhookService,
|
||||
private readonly cache: RedisCache,
|
||||
) {}
|
||||
|
||||
private readonly logger = new Logger(InstanceController.name);
|
||||
|
||||
public async createInstance({ instanceName, webhook }: InstanceDto) {
|
||||
//verifica se modo da instancia é container
|
||||
public async createInstance({
|
||||
instanceName,
|
||||
webhook,
|
||||
webhook_by_events,
|
||||
events,
|
||||
qrcode,
|
||||
token,
|
||||
}: InstanceDto) {
|
||||
const mode = this.configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
|
||||
|
||||
if (mode === 'container') {
|
||||
//verifica se ja existe uma instancia criada com qualquer nome
|
||||
if (Object.keys(this.waMonitor.waInstances).length > 0) {
|
||||
throw new BadRequestException([
|
||||
'Instance already created',
|
||||
@@ -35,22 +43,37 @@ export class InstanceController {
|
||||
]);
|
||||
}
|
||||
|
||||
await this.authService.checkDuplicateToken(token);
|
||||
|
||||
const instance = new WAStartupService(
|
||||
this.configService,
|
||||
this.eventEmitter,
|
||||
this.repository,
|
||||
this.cache,
|
||||
);
|
||||
instance.instanceName = instanceName;
|
||||
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||
this.waMonitor.delInstanceTime(instance.instanceName);
|
||||
|
||||
const hash = await this.authService.generateHash({
|
||||
const hash = await this.authService.generateHash(
|
||||
{
|
||||
instanceName: instance.instanceName,
|
||||
});
|
||||
},
|
||||
token,
|
||||
);
|
||||
|
||||
let getEvents: string[];
|
||||
|
||||
if (webhook) {
|
||||
try {
|
||||
this.webhookService.create(instance, { enabled: true, url: webhook });
|
||||
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);
|
||||
}
|
||||
@@ -63,29 +86,53 @@ export class InstanceController {
|
||||
},
|
||||
hash,
|
||||
webhook,
|
||||
events: getEvents,
|
||||
};
|
||||
} else {
|
||||
await this.authService.checkDuplicateToken(token);
|
||||
|
||||
const instance = new WAStartupService(
|
||||
this.configService,
|
||||
this.eventEmitter,
|
||||
this.repository,
|
||||
this.cache,
|
||||
);
|
||||
instance.instanceName = instanceName;
|
||||
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||
this.waMonitor.delInstanceTime(instance.instanceName);
|
||||
|
||||
const hash = await this.authService.generateHash({
|
||||
const hash = await this.authService.generateHash(
|
||||
{
|
||||
instanceName: instance.instanceName,
|
||||
});
|
||||
},
|
||||
token,
|
||||
);
|
||||
|
||||
let getEvents: string[];
|
||||
|
||||
if (webhook) {
|
||||
try {
|
||||
this.webhookService.create(instance, { enabled: true, url: webhook });
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
let getQrcode: wa.QrCode;
|
||||
|
||||
if (qrcode) {
|
||||
await instance.connectToWhatsapp();
|
||||
await delay(2000);
|
||||
getQrcode = instance.qrCode;
|
||||
}
|
||||
|
||||
return {
|
||||
instance: {
|
||||
instanceName: instance.instanceName,
|
||||
@@ -93,6 +140,9 @@ export class InstanceController {
|
||||
},
|
||||
hash,
|
||||
webhook,
|
||||
webhook_by_events,
|
||||
events: getEvents,
|
||||
qrcode: getQrcode,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -100,7 +150,7 @@ export class InstanceController {
|
||||
public async connectToWhatsapp({ instanceName }: InstanceDto) {
|
||||
try {
|
||||
const instance = this.waMonitor.waInstances[instanceName];
|
||||
const state = instance.connectionStatus?.state;
|
||||
const state = instance?.connectionStatus?.state;
|
||||
|
||||
switch (state) {
|
||||
case 'close':
|
||||
@@ -113,12 +163,33 @@ export class InstanceController {
|
||||
return await this.connectionState({ instanceName });
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.log(error);
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async restartInstance({ instanceName }: InstanceDto) {
|
||||
try {
|
||||
delete this.waMonitor.waInstances[instanceName];
|
||||
console.log(this.waMonitor.waInstances[instanceName]);
|
||||
const instance = new WAStartupService(
|
||||
this.configService,
|
||||
this.eventEmitter,
|
||||
this.repository,
|
||||
this.cache,
|
||||
);
|
||||
|
||||
instance.instanceName = instanceName;
|
||||
await instance.connectToWhatsapp();
|
||||
this.waMonitor.waInstances[instance.instanceName] = instance;
|
||||
|
||||
return { error: false, message: 'Instance restarted' };
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async connectionState({ instanceName }: InstanceDto) {
|
||||
return this.waMonitor.waInstances[instanceName].connectionStatus;
|
||||
return this.waMonitor.waInstances[instanceName]?.connectionStatus;
|
||||
}
|
||||
|
||||
public async fetchInstances({ instanceName }: InstanceDto) {
|
||||
@@ -136,7 +207,6 @@ export class InstanceController {
|
||||
);
|
||||
|
||||
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
||||
this.waMonitor.waInstances[instanceName]?.client?.end(undefined);
|
||||
|
||||
return { error: false, message: 'Instance logged out' };
|
||||
} catch (error) {
|
||||
@@ -153,8 +223,15 @@ export class InstanceController {
|
||||
]);
|
||||
}
|
||||
try {
|
||||
if (stateConn.state === 'connecting') {
|
||||
await this.logout({ instanceName });
|
||||
delete this.waMonitor.waInstances[instanceName];
|
||||
return { error: false, message: 'Instance deleted' };
|
||||
} else {
|
||||
delete this.waMonitor.waInstances[instanceName];
|
||||
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
|
||||
return { error: false, message: 'Instance deleted' };
|
||||
}
|
||||
} catch (error) {
|
||||
throw new BadRequestException(error.toString());
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
SendMediaDto,
|
||||
SendPollDto,
|
||||
SendReactionDto,
|
||||
SendStickerDto,
|
||||
SendTextDto,
|
||||
} from '../dto/sendMessage.dto';
|
||||
import { WAMonitoringService } from '../services/monitor.service';
|
||||
@@ -32,6 +33,13 @@ export class SendMessageController {
|
||||
throw new BadRequestException('Owned media must be a url or base64');
|
||||
}
|
||||
|
||||
public async sendSticker({ instanceName }: InstanceDto, data: SendStickerDto) {
|
||||
if (isURL(data.stickerMessage.image) || isBase64(data.stickerMessage.image)) {
|
||||
return await this.waMonitor.waInstances[instanceName].mediaSticker(data);
|
||||
}
|
||||
throw new BadRequestException('Owned media must be a url or base64');
|
||||
}
|
||||
|
||||
public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto) {
|
||||
if (isURL(data.audioMessage.audio) || isBase64(data.audioMessage.audio)) {
|
||||
return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data);
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
import {
|
||||
WAPrivacyOnlineValue,
|
||||
WAPrivacyValue,
|
||||
WAReadReceiptsValue,
|
||||
proto,
|
||||
} from '@whiskeysockets/baileys';
|
||||
|
||||
export class OnWhatsAppDto {
|
||||
constructor(
|
||||
public readonly jid: string,
|
||||
@@ -6,6 +13,11 @@ export class OnWhatsAppDto {
|
||||
) {}
|
||||
}
|
||||
|
||||
export class getBase64FromMediaMessageDto {
|
||||
message: proto.WebMessageInfo;
|
||||
convertToMp4?: boolean;
|
||||
}
|
||||
|
||||
export class WhatsAppNumberDto {
|
||||
numbers: string[];
|
||||
}
|
||||
@@ -47,6 +59,19 @@ export class ArchiveChatDto {
|
||||
archive: boolean;
|
||||
}
|
||||
|
||||
class PrivacySetting {
|
||||
readreceipts: WAReadReceiptsValue;
|
||||
profile: WAPrivacyValue;
|
||||
status: WAPrivacyValue;
|
||||
online: WAPrivacyOnlineValue;
|
||||
last: WAPrivacyValue;
|
||||
groupadd: WAPrivacyValue;
|
||||
}
|
||||
|
||||
export class PrivacySettingDto {
|
||||
privacySettings: PrivacySetting;
|
||||
}
|
||||
|
||||
export class DeleteMessage {
|
||||
id: string;
|
||||
fromMe: boolean;
|
||||
|
||||
@@ -9,6 +9,16 @@ export class GroupPictureDto {
|
||||
image: string;
|
||||
}
|
||||
|
||||
export class GroupSubjectDto {
|
||||
groupJid: string;
|
||||
subject: string;
|
||||
}
|
||||
|
||||
export class GroupDescriptionDto {
|
||||
groupJid: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export class GroupJid {
|
||||
groupJid: string;
|
||||
}
|
||||
@@ -17,6 +27,12 @@ export class GroupInvite {
|
||||
inviteCode: string;
|
||||
}
|
||||
|
||||
export class GroupSendInvite {
|
||||
groupJid: string;
|
||||
description: string;
|
||||
numbers: string[];
|
||||
}
|
||||
|
||||
export class GroupUpdateParticipantDto extends GroupJid {
|
||||
action: 'add' | 'remove' | 'promote' | 'demote';
|
||||
participants: string[];
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
export class InstanceDto {
|
||||
instanceName: string;
|
||||
webhook?: string;
|
||||
webhook_by_events?: boolean;
|
||||
events?: string[];
|
||||
qrcode?: boolean;
|
||||
token?: string;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { proto, WAPresence } from '@evolution/base';
|
||||
import { proto, WAPresence } from '@whiskeysockets/baileys';
|
||||
|
||||
export class Quoted {
|
||||
key: proto.IMessageKey;
|
||||
@@ -62,6 +62,12 @@ export class MediaMessage {
|
||||
export class SendMediaDto extends Metadata {
|
||||
mediaMessage: MediaMessage;
|
||||
}
|
||||
class Sticker {
|
||||
image: string;
|
||||
}
|
||||
export class SendStickerDto extends Metadata {
|
||||
stickerMessage: Sticker;
|
||||
}
|
||||
|
||||
class Audio {
|
||||
audio: string;
|
||||
@@ -119,6 +125,9 @@ export class ContactMessage {
|
||||
fullName: string;
|
||||
wuid: string;
|
||||
phoneNumber: string;
|
||||
organization?: string;
|
||||
email?: string;
|
||||
url?: string;
|
||||
}
|
||||
export class SendContactDto extends Metadata {
|
||||
contactMessage: ContactMessage[];
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
export class WebhookDto {
|
||||
enabled?: boolean;
|
||||
url?: string;
|
||||
events?: string[];
|
||||
webhook_by_events?: boolean;
|
||||
}
|
||||
|
||||
@@ -2,17 +2,27 @@ import { NextFunction, Request, Response } from 'express';
|
||||
import { existsSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { INSTANCE_DIR } from '../../config/path.config';
|
||||
import { db, dbserver } from '../../db/db.connect';
|
||||
import { dbserver } from '../../db/db.connect';
|
||||
import {
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
NotFoundException,
|
||||
} from '../../exceptions';
|
||||
import { InstanceDto } from '../dto/instance.dto';
|
||||
import { waMonitor } from '../whatsapp.module';
|
||||
import { cache, waMonitor } from '../whatsapp.module';
|
||||
import { Database, Redis, configService } from '../../config/env.config';
|
||||
import { RedisCache } from '../../db/redis.client';
|
||||
|
||||
async function getInstance(instanceName: string) {
|
||||
const exists = waMonitor.waInstances[instanceName];
|
||||
const db = configService.get<Database>('DATABASE');
|
||||
const redisConf = configService.get<Redis>('REDIS');
|
||||
|
||||
const exists = !!waMonitor.waInstances[instanceName];
|
||||
|
||||
if (redisConf.ENABLED) {
|
||||
const keyExists = await cache.keyExists();
|
||||
return exists || keyExists;
|
||||
}
|
||||
|
||||
if (db.ENABLED) {
|
||||
const collection = dbserver
|
||||
|
||||
@@ -50,6 +50,7 @@ export class MessageUpdateRaw {
|
||||
datetime?: number;
|
||||
status?: wa.StatusMessage;
|
||||
owner: string;
|
||||
pollUpdates?: any;
|
||||
}
|
||||
|
||||
const messageUpdateSchema = new Schema<MessageUpdateRaw>({
|
||||
|
||||
@@ -5,12 +5,15 @@ export class WebhookRaw {
|
||||
_id?: string;
|
||||
url?: string;
|
||||
enabled?: boolean;
|
||||
events?: string[];
|
||||
webhook_by_events?: boolean;
|
||||
}
|
||||
|
||||
const webhookSchema = new Schema<WebhookRaw>({
|
||||
_id: { type: String, _id: true },
|
||||
url: { type: String, required: true },
|
||||
enabled: { type: Boolean, required: true },
|
||||
events: { type: [String], required: true },
|
||||
});
|
||||
|
||||
export const WebhookModel = dbserver?.model(WebhookRaw.name, webhookSchema, 'webhook');
|
||||
|
||||
@@ -4,6 +4,7 @@ import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { IAuthModel, AuthRaw } from '../models';
|
||||
import { readFileSync } from 'fs';
|
||||
import { AUTH_DIR } from '../../config/path.config';
|
||||
import { InstanceDto } from '../dto/instance.dto';
|
||||
|
||||
export class AuthRepository extends Repository {
|
||||
constructor(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { join } from 'path';
|
||||
import { ConfigService } from '../../config/env.config';
|
||||
import { ConfigService, StoreConf } from '../../config/env.config';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { opendirSync, readFileSync, rmSync } from 'fs';
|
||||
import { ChatRaw, IChatModel } from '../models';
|
||||
@@ -27,6 +27,9 @@ export class ChatRepository extends Repository {
|
||||
return { insertCount: insert.length };
|
||||
}
|
||||
|
||||
const store = this.configService.get<StoreConf>('STORE');
|
||||
|
||||
if (store.CHATS) {
|
||||
data.forEach((chat) => {
|
||||
this.writeStore<ChatRaw>({
|
||||
path: join(this.storePath, 'chats', chat.owner),
|
||||
@@ -36,6 +39,9 @@ export class ChatRepository extends Repository {
|
||||
});
|
||||
|
||||
return { insertCount: data.length };
|
||||
}
|
||||
|
||||
return { insertCount: 0 };
|
||||
} catch (error) {
|
||||
return error;
|
||||
} finally {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { opendirSync, readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { ConfigService } from '../../config/env.config';
|
||||
import { ConfigService, StoreConf } from '../../config/env.config';
|
||||
import { ContactRaw, IContactModel } from '../models';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
|
||||
@@ -27,6 +27,9 @@ export class ContactRepository extends Repository {
|
||||
return { insertCount: insert.length };
|
||||
}
|
||||
|
||||
const store = this.configService.get<StoreConf>('STORE');
|
||||
|
||||
if (store.CONTACTS) {
|
||||
data.forEach((contact) => {
|
||||
this.writeStore({
|
||||
path: join(this.storePath, 'contacts', contact.owner),
|
||||
@@ -36,6 +39,9 @@ export class ContactRepository extends Repository {
|
||||
});
|
||||
|
||||
return { insertCount: data.length };
|
||||
}
|
||||
|
||||
return { insertCount: 0 };
|
||||
} catch (error) {
|
||||
return error;
|
||||
} finally {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ConfigService } from '../../config/env.config';
|
||||
import { ConfigService, StoreConf } from '../../config/env.config';
|
||||
import { join } from 'path';
|
||||
import { IMessageModel, MessageRaw } from '../models';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
@@ -47,7 +47,9 @@ export class MessageRepository extends Repository {
|
||||
return { insertCount: insert.length };
|
||||
}
|
||||
|
||||
if (saveDb) {
|
||||
const store = this.configService.get<StoreConf>('STORE');
|
||||
|
||||
if (store.MESSAGES) {
|
||||
data.forEach((msg) =>
|
||||
this.writeStore<MessageRaw>({
|
||||
path: join(this.storePath, 'messages', msg.owner),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ConfigService } from '../../config/env.config';
|
||||
import { ConfigService, StoreConf } from '../../config/env.config';
|
||||
import { IMessageUpModel, MessageUpdateRaw } from '../models';
|
||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||
import { join } from 'path';
|
||||
@@ -28,6 +28,9 @@ export class MessageUpRepository extends Repository {
|
||||
return { insertCount: insert.length };
|
||||
}
|
||||
|
||||
const store = this.configService.get<StoreConf>('STORE');
|
||||
|
||||
if (store.MESSAGE_UP) {
|
||||
data.forEach((update) => {
|
||||
this.writeStore<MessageUpdateRaw>({
|
||||
path: join(this.storePath, 'message-up', update.owner),
|
||||
@@ -35,6 +38,11 @@ export class MessageUpRepository extends Repository {
|
||||
data: update,
|
||||
});
|
||||
});
|
||||
|
||||
return { insertCount: data.length };
|
||||
}
|
||||
|
||||
return { insertCount: 0 };
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ import { MessageUpRepository } from './messageUp.repository';
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { WebhookRepository } from './webhook.repository';
|
||||
import { AuthRepository } from './auth.repository';
|
||||
import { Auth, ConfigService, Database } from '../../config/env.config';
|
||||
import { execSync } from 'child_process';
|
||||
import { join } from 'path';
|
||||
|
||||
export class RepositoryBroker {
|
||||
constructor(
|
||||
@@ -14,9 +17,11 @@ export class RepositoryBroker {
|
||||
public readonly messageUpdate: MessageUpRepository,
|
||||
public readonly webhook: WebhookRepository,
|
||||
public readonly auth: AuthRepository,
|
||||
private configService: ConfigService,
|
||||
dbServer?: MongoClient,
|
||||
) {
|
||||
this.dbClient = dbServer;
|
||||
this.__init_repo_without_db__();
|
||||
}
|
||||
|
||||
private dbClient?: MongoClient;
|
||||
@@ -24,4 +29,22 @@ export class RepositoryBroker {
|
||||
public get dbServer() {
|
||||
return this.dbClient;
|
||||
}
|
||||
|
||||
private __init_repo_without_db__() {
|
||||
if (!this.configService.get<Database>('DATABASE').ENABLED) {
|
||||
const storePath = join(process.cwd(), 'store');
|
||||
execSync(
|
||||
`mkdir -p ${join(
|
||||
storePath,
|
||||
'auth',
|
||||
this.configService.get<Auth>('AUTHENTICATION').TYPE,
|
||||
)}`,
|
||||
);
|
||||
execSync(`mkdir -p ${join(storePath, 'chats')}`);
|
||||
execSync(`mkdir -p ${join(storePath, 'contacts')}`);
|
||||
execSync(`mkdir -p ${join(storePath, 'messages')}`);
|
||||
execSync(`mkdir -p ${join(storePath, 'message-up')}`);
|
||||
execSync(`mkdir -p ${join(storePath, 'webhook')}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
deleteMessageSchema,
|
||||
messageUpSchema,
|
||||
messageValidateSchema,
|
||||
privacySettingsSchema,
|
||||
profileNameSchema,
|
||||
profilePictureSchema,
|
||||
profileStatusSchema,
|
||||
@@ -15,11 +16,13 @@ import {
|
||||
ArchiveChatDto,
|
||||
DeleteMessage,
|
||||
NumberDto,
|
||||
PrivacySettingDto,
|
||||
ProfileNameDto,
|
||||
ProfilePictureDto,
|
||||
ProfileStatusDto,
|
||||
ReadMessageDto,
|
||||
WhatsAppNumberDto,
|
||||
getBase64FromMediaMessageDto,
|
||||
} from '../dto/chat.dto';
|
||||
import { ContactQuery } from '../repository/contact.repository';
|
||||
import { MessageQuery } from '../repository/message.repository';
|
||||
@@ -27,7 +30,7 @@ import { chatController } from '../whatsapp.module';
|
||||
import { RouterBroker } from '../abstract/abstract.router';
|
||||
import { HttpStatus } from './index.router';
|
||||
import { MessageUpQuery } from '../repository/messageUp.repository';
|
||||
import { proto } from '@evolution/base';
|
||||
import { proto } from '@whiskeysockets/baileys';
|
||||
import { InstanceDto } from '../dto/instance.dto';
|
||||
|
||||
export class ChatRouter extends RouterBroker {
|
||||
@@ -99,10 +102,10 @@ export class ChatRouter extends RouterBroker {
|
||||
return res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.post(this.routerPath('getBase64FromMediaMessage'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<proto.IWebMessageInfo>({
|
||||
const response = await this.dataValidate<getBase64FromMediaMessageDto>({
|
||||
request: req,
|
||||
schema: null,
|
||||
ClassRef: Object,
|
||||
ClassRef: getBase64FromMediaMessageDto,
|
||||
execute: (instance, data) =>
|
||||
chatController.getBase64FromMediaMessage(instance, data),
|
||||
});
|
||||
@@ -140,12 +143,34 @@ export class ChatRouter extends RouterBroker {
|
||||
return res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
// Profile routes
|
||||
.post(this.routerPath('getBusinessProfile'), ...guards, async (req, res) => {
|
||||
.get(this.routerPath('fetchPrivacySettings'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: null,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => chatController.fetchPrivacySettings(instance),
|
||||
});
|
||||
|
||||
return res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.put(this.routerPath('updatePrivacySettings'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<PrivacySettingDto>({
|
||||
request: req,
|
||||
schema: privacySettingsSchema,
|
||||
ClassRef: PrivacySettingDto,
|
||||
execute: (instance, data) =>
|
||||
chatController.updatePrivacySettings(instance, data),
|
||||
});
|
||||
|
||||
return res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.post(this.routerPath('fetchBusinessProfile'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<ProfilePictureDto>({
|
||||
request: req,
|
||||
schema: profilePictureSchema,
|
||||
ClassRef: ProfilePictureDto,
|
||||
execute: (instance, data) => chatController.getBusinessProfile(instance, data),
|
||||
execute: (instance, data) =>
|
||||
chatController.fetchBusinessProfile(instance, data),
|
||||
});
|
||||
|
||||
return res.status(HttpStatus.OK).json(response);
|
||||
|
||||
@@ -5,8 +5,11 @@ import {
|
||||
updateParticipantsSchema,
|
||||
updateSettingsSchema,
|
||||
toggleEphemeralSchema,
|
||||
updateGroupPicture,
|
||||
updateGroupPictureSchema,
|
||||
updateGroupSubjectSchema,
|
||||
updateGroupDescriptionSchema,
|
||||
groupInviteSchema,
|
||||
groupSendInviteSchema,
|
||||
} from '../../validate/validate.schema';
|
||||
import { RouterBroker } from '../abstract/abstract.router';
|
||||
import {
|
||||
@@ -14,9 +17,12 @@ import {
|
||||
GroupInvite,
|
||||
GroupJid,
|
||||
GroupPictureDto,
|
||||
GroupSubjectDto,
|
||||
GroupDescriptionDto,
|
||||
GroupUpdateParticipantDto,
|
||||
GroupUpdateSettingDto,
|
||||
GroupToggleEphemeralDto,
|
||||
GroupSendInvite,
|
||||
} from '../dto/group.dto';
|
||||
import { groupController } from '../whatsapp.module';
|
||||
import { HttpStatus } from './index.router';
|
||||
@@ -35,16 +41,37 @@ export class GroupRouter extends RouterBroker {
|
||||
|
||||
res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.put(this.routerPath('updateGroupSubject'), ...guards, async (req, res) => {
|
||||
const response = await this.groupValidate<GroupSubjectDto>({
|
||||
request: req,
|
||||
schema: updateGroupSubjectSchema,
|
||||
ClassRef: GroupSubjectDto,
|
||||
execute: (instance, data) => groupController.updateGroupSubject(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.put(this.routerPath('updateGroupPicture'), ...guards, async (req, res) => {
|
||||
const response = await this.groupValidate<GroupPictureDto>({
|
||||
request: req,
|
||||
schema: updateGroupPicture,
|
||||
schema: updateGroupPictureSchema,
|
||||
ClassRef: GroupPictureDto,
|
||||
execute: (instance, data) => groupController.updateGroupPicture(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.put(this.routerPath('updateGroupDescription'), ...guards, async (req, res) => {
|
||||
const response = await this.groupValidate<GroupDescriptionDto>({
|
||||
request: req,
|
||||
schema: updateGroupDescriptionSchema,
|
||||
ClassRef: GroupDescriptionDto,
|
||||
execute: (instance, data) =>
|
||||
groupController.updateGroupDescription(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.get(this.routerPath('findGroupInfos'), ...guards, async (req, res) => {
|
||||
const response = await this.groupValidate<GroupJid>({
|
||||
request: req,
|
||||
@@ -55,6 +82,16 @@ export class GroupRouter extends RouterBroker {
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.get(this.routerPath('fetchAllGroups'), ...guards, async (req, res) => {
|
||||
const response = await this.groupNoValidate<GroupJid>({
|
||||
request: req,
|
||||
schema: {},
|
||||
ClassRef: GroupJid,
|
||||
execute: (instance) => groupController.fetchAllGroups(instance),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.get(this.routerPath('participants'), ...guards, async (req, res) => {
|
||||
const response = await this.groupValidate<GroupJid>({
|
||||
request: req,
|
||||
@@ -85,6 +122,16 @@ export class GroupRouter extends RouterBroker {
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.post(this.routerPath('sendInvite'), ...guards, async (req, res) => {
|
||||
const response = await this.groupNoValidate<GroupSendInvite>({
|
||||
request: req,
|
||||
schema: groupSendInviteSchema,
|
||||
ClassRef: GroupSendInvite,
|
||||
execute: (instance, data) => groupController.sendInvite(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.put(this.routerPath('revokeInviteCode'), ...guards, async (req, res) => {
|
||||
const response = await this.groupValidate<GroupJid>({
|
||||
request: req,
|
||||
|
||||
@@ -7,7 +7,6 @@ import { HttpStatus } from './index.router';
|
||||
import { OldToken } from '../services/auth.service';
|
||||
import { Auth, ConfigService, Database } from '../../config/env.config';
|
||||
import { dbserver } from '../../db/db.connect';
|
||||
import { BadRequestException, InternalServerErrorException } from '../../exceptions';
|
||||
|
||||
export class InstanceRouter extends RouterBroker {
|
||||
constructor(readonly configService: ConfigService, ...guards: RequestHandler[]) {
|
||||
@@ -24,6 +23,16 @@ export class InstanceRouter extends RouterBroker {
|
||||
|
||||
return res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.put(this.routerPath('restart'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: instanceNameSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => instanceController.restartInstance(instance),
|
||||
});
|
||||
|
||||
return res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.get(this.routerPath('connect'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
mediaMessageSchema,
|
||||
pollMessageSchema,
|
||||
reactionMessageSchema,
|
||||
stickerMessageSchema,
|
||||
textMessageSchema,
|
||||
} from '../../validate/validate.schema';
|
||||
import {
|
||||
@@ -21,6 +22,7 @@ import {
|
||||
SendMediaDto,
|
||||
SendPollDto,
|
||||
SendReactionDto,
|
||||
SendStickerDto,
|
||||
SendTextDto,
|
||||
} from '../dto/sendMessage.dto';
|
||||
import { sendMessageController } from '../whatsapp.module';
|
||||
@@ -131,6 +133,16 @@ export class MessageRouter extends RouterBroker {
|
||||
sendMessageController.sendLinkPreview(instance, data),
|
||||
});
|
||||
|
||||
return res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.post(this.routerPath('sendSticker'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<SendStickerDto>({
|
||||
request: req,
|
||||
schema: stickerMessageSchema,
|
||||
ClassRef: SendStickerDto,
|
||||
execute: (instance, data) => sendMessageController.sendSticker(instance, data),
|
||||
});
|
||||
|
||||
return res.status(HttpStatus.CREATED).json(response);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -43,8 +43,12 @@ export class AuthService {
|
||||
{ expiresIn: jwtOpts.EXPIRIN_IN, encoding: 'utf8', subject: 'g-t' },
|
||||
);
|
||||
|
||||
this.logger.verbose('JWT token created: ' + token);
|
||||
|
||||
const auth = await this.repository.auth.create({ jwt: token }, instance.instanceName);
|
||||
|
||||
this.logger.verbose('JWT token saved in database');
|
||||
|
||||
if (auth['error']) {
|
||||
this.logger.error({
|
||||
localError: AuthService.name + '.jwt',
|
||||
@@ -56,14 +60,20 @@ export class AuthService {
|
||||
return { jwt: token };
|
||||
}
|
||||
|
||||
private async apikey(instance: InstanceDto) {
|
||||
const apikey = v4().toUpperCase();
|
||||
private async apikey(instance: InstanceDto, token?: string) {
|
||||
const apikey = token ? token : v4().toUpperCase();
|
||||
|
||||
this.logger.verbose(
|
||||
token ? 'APIKEY defined: ' + apikey : 'APIKEY created: ' + apikey,
|
||||
);
|
||||
|
||||
const auth = await this.repository.auth.create({ apikey }, instance.instanceName);
|
||||
|
||||
this.logger.verbose('APIKEY saved in database');
|
||||
|
||||
if (auth['error']) {
|
||||
this.logger.error({
|
||||
localError: AuthService.name + '.jwt',
|
||||
localError: AuthService.name + '.apikey',
|
||||
error: auth['error'],
|
||||
});
|
||||
throw new BadRequestException('Authentication error', auth['error']?.toString());
|
||||
@@ -72,43 +82,80 @@ export class AuthService {
|
||||
return { apikey };
|
||||
}
|
||||
|
||||
public async generateHash(instance: InstanceDto) {
|
||||
public async checkDuplicateToken(token: string) {
|
||||
const instances = await this.waMonitor.instanceInfo();
|
||||
|
||||
this.logger.verbose('checking duplicate token');
|
||||
|
||||
const instance = instances.find((instance) => instance.instance.apikey === token);
|
||||
|
||||
if (instance) {
|
||||
throw new BadRequestException('Token already exists');
|
||||
}
|
||||
|
||||
this.logger.verbose('available token');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async generateHash(instance: InstanceDto, token?: string) {
|
||||
const options = this.configService.get<Auth>('AUTHENTICATION');
|
||||
return (await this[options.TYPE](instance)) as { jwt: string } | { apikey: string };
|
||||
|
||||
this.logger.verbose(
|
||||
'generating hash ' + options.TYPE + ' to instance: ' + instance.instanceName,
|
||||
);
|
||||
|
||||
return (await this[options.TYPE](instance, token)) as
|
||||
| { jwt: string }
|
||||
| { apikey: string };
|
||||
}
|
||||
|
||||
public async refreshToken({ oldToken }: OldToken) {
|
||||
this.logger.verbose('refreshing token');
|
||||
|
||||
if (!isJWT(oldToken)) {
|
||||
throw new BadRequestException('Invalid "oldToken"');
|
||||
}
|
||||
|
||||
try {
|
||||
const jwtOpts = this.configService.get<Auth>('AUTHENTICATION').JWT;
|
||||
|
||||
this.logger.verbose('checking oldToken');
|
||||
|
||||
const decode = verify(oldToken, jwtOpts.SECRET, {
|
||||
ignoreExpiration: true,
|
||||
}) as Pick<JwtPayload, 'apiName' | 'instanceName' | 'tokenId'>;
|
||||
|
||||
this.logger.verbose('checking token in database');
|
||||
|
||||
const tokenStore = await this.repository.auth.find(decode.instanceName);
|
||||
|
||||
const decodeTokenStore = verify(tokenStore.jwt, jwtOpts.SECRET, {
|
||||
ignoreExpiration: true,
|
||||
}) as Pick<JwtPayload, 'apiName' | 'instanceName' | 'tokenId'>;
|
||||
|
||||
this.logger.verbose('checking tokenId');
|
||||
|
||||
if (decode.tokenId !== decodeTokenStore.tokenId) {
|
||||
throw new BadRequestException('Invalid "oldToken"');
|
||||
}
|
||||
|
||||
this.logger.verbose('generating new token');
|
||||
|
||||
const token = {
|
||||
jwt: (await this.jwt({ instanceName: decode.instanceName })).jwt,
|
||||
instanceName: decode.instanceName,
|
||||
};
|
||||
|
||||
try {
|
||||
this.logger.verbose('checking webhook');
|
||||
const webhook = await this.repository.webhook.find(decode.instanceName);
|
||||
if (
|
||||
webhook?.enabled &&
|
||||
this.configService.get<Webhook>('WEBHOOK').EVENTS.NEW_JWT_TOKEN
|
||||
) {
|
||||
this.logger.verbose('sending webhook');
|
||||
|
||||
const httpService = axios.create({ baseURL: webhook.url });
|
||||
await httpService.post(
|
||||
'',
|
||||
@@ -124,6 +171,8 @@ export class AuthService {
|
||||
this.logger.error(error);
|
||||
}
|
||||
|
||||
this.logger.verbose('token refreshed');
|
||||
|
||||
return token;
|
||||
} catch (error) {
|
||||
this.logger.error({
|
||||
|
||||
@@ -4,20 +4,28 @@ import { INSTANCE_DIR } from '../../config/path.config';
|
||||
import EventEmitter2 from 'eventemitter2';
|
||||
import { join } from 'path';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { ConfigService, Database, DelInstance, Redis } from '../../config/env.config';
|
||||
import {
|
||||
Auth,
|
||||
ConfigService,
|
||||
Database,
|
||||
DelInstance,
|
||||
Redis,
|
||||
} from '../../config/env.config';
|
||||
import { RepositoryBroker } from '../repository/repository.manager';
|
||||
import { NotFoundException } from '../../exceptions';
|
||||
import { Db } from 'mongodb';
|
||||
import { RedisCache } from '../../db/redis.client';
|
||||
import { initInstance } from '../whatsapp.module';
|
||||
import { ValidationError } from 'class-validator';
|
||||
import { RedisCache } from '../../db/redis.client';
|
||||
|
||||
export class WAMonitoringService {
|
||||
constructor(
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly configService: ConfigService,
|
||||
private readonly repository: RepositoryBroker,
|
||||
private readonly cache: RedisCache,
|
||||
) {
|
||||
this.logger.verbose('instance created');
|
||||
|
||||
this.removeInstance();
|
||||
this.noConnection();
|
||||
this.delInstanceFiles();
|
||||
@@ -28,15 +36,12 @@ export class WAMonitoringService {
|
||||
this.dbInstance = this.db.ENABLED
|
||||
? this.repository.dbServer?.db(this.db.CONNECTION.DB_PREFIX_NAME + '-instances')
|
||||
: undefined;
|
||||
|
||||
this.redisCache = this.redis.ENABLED ? new RedisCache(this.redis) : undefined;
|
||||
}
|
||||
|
||||
private readonly db: Partial<Database> = {};
|
||||
private readonly redis: Partial<Redis> = {};
|
||||
|
||||
private dbInstance: Db;
|
||||
private redisCache: RedisCache;
|
||||
|
||||
private readonly logger = new Logger(WAMonitoringService.name);
|
||||
public readonly waInstances: Record<string, WAStartupService> = {};
|
||||
@@ -44,15 +49,30 @@ export class WAMonitoringService {
|
||||
public delInstanceTime(instance: string) {
|
||||
const time = this.configService.get<DelInstance>('DEL_INSTANCE');
|
||||
if (typeof time === 'number' && time > 0) {
|
||||
setTimeout(() => {
|
||||
this.logger.verbose(
|
||||
`Instance "${instance}" don't have connection, will be removed in ${time} minutes`,
|
||||
);
|
||||
|
||||
setTimeout(async () => {
|
||||
if (this.waInstances[instance]?.connectionStatus?.state !== 'open') {
|
||||
if (this.waInstances[instance]?.connectionStatus?.state === 'connecting') {
|
||||
await this.waInstances[instance]?.client?.logout(
|
||||
'Log out instance: ' + instance,
|
||||
);
|
||||
this.waInstances[instance]?.client?.ws?.close();
|
||||
this.waInstances[instance]?.client?.end(undefined);
|
||||
delete this.waInstances[instance];
|
||||
} else {
|
||||
delete this.waInstances[instance];
|
||||
this.eventEmitter.emit('remove.instance', instance, 'inner');
|
||||
}
|
||||
}
|
||||
}, 1000 * 60 * time);
|
||||
}
|
||||
}
|
||||
|
||||
public async instanceInfo(instanceName?: string) {
|
||||
this.logger.verbose('get instance info');
|
||||
if (instanceName && !this.waInstances[instanceName]) {
|
||||
throw new NotFoundException(`Instance "${instanceName}" not found`);
|
||||
}
|
||||
@@ -60,7 +80,32 @@ export class WAMonitoringService {
|
||||
const instances: any[] = [];
|
||||
|
||||
for await (const [key, value] of Object.entries(this.waInstances)) {
|
||||
if (value && value.connectionStatus.state === 'open') {
|
||||
if (value) {
|
||||
this.logger.verbose('get instance info: ' + key);
|
||||
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,
|
||||
status: (await value.getProfileStatus()) || '',
|
||||
apikey,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
this.logger.verbose(
|
||||
'instance: ' + key + ' - hash not exposed in fetch instances',
|
||||
);
|
||||
instances.push({
|
||||
instance: {
|
||||
instanceName: key,
|
||||
@@ -71,12 +116,47 @@ export class WAMonitoringService {
|
||||
},
|
||||
});
|
||||
}
|
||||
} 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,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
this.logger.verbose(
|
||||
'instance: ' + key + ' - hash not exposed in fetch instances',
|
||||
);
|
||||
instances.push({
|
||||
instance: {
|
||||
instanceName: key,
|
||||
status: value.connectionStatus.state,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.verbose('return instance info: ' + instances.length);
|
||||
|
||||
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
|
||||
}
|
||||
|
||||
private delInstanceFiles() {
|
||||
this.logger.verbose('cron to delete instance files started');
|
||||
setInterval(async () => {
|
||||
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
||||
const collections = await this.dbInstance.collections();
|
||||
@@ -88,7 +168,9 @@ export class WAMonitoringService {
|
||||
{ _id: { $regex: /^session-.*/ } },
|
||||
],
|
||||
});
|
||||
this.logger.verbose('instance files deleted: ' + name);
|
||||
});
|
||||
} else if (this.redis.ENABLED) {
|
||||
} else {
|
||||
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
||||
for await (const dirent of dir) {
|
||||
@@ -104,14 +186,17 @@ export class WAMonitoringService {
|
||||
});
|
||||
}
|
||||
});
|
||||
this.logger.verbose('instance files deleted: ' + dirent.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 3600 * 1000 * 2);
|
||||
}
|
||||
|
||||
private async cleaningUp(instanceName: string) {
|
||||
public async cleaningUp(instanceName: string) {
|
||||
this.logger.verbose('cleaning up instance: ' + instanceName);
|
||||
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
||||
this.logger.verbose('cleaning up instance in database: ' + instanceName);
|
||||
await this.repository.dbServer.connect();
|
||||
const collections: any[] = await this.dbInstance.collections();
|
||||
if (collections.length > 0) {
|
||||
@@ -121,52 +206,70 @@ export class WAMonitoringService {
|
||||
}
|
||||
|
||||
if (this.redis.ENABLED) {
|
||||
this.redisCache.reference = instanceName;
|
||||
await this.redisCache.delAll();
|
||||
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
|
||||
this.cache.reference = instanceName;
|
||||
await this.cache.delAll();
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.verbose('cleaning up instance in files: ' + instanceName);
|
||||
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
|
||||
}
|
||||
|
||||
public async loadInstance() {
|
||||
this.logger.verbose('load instances');
|
||||
const set = async (name: string) => {
|
||||
const instance = new WAStartupService(
|
||||
this.configService,
|
||||
this.eventEmitter,
|
||||
this.repository,
|
||||
this.cache,
|
||||
);
|
||||
instance.instanceName = name;
|
||||
this.logger.verbose('instance loaded: ' + name);
|
||||
|
||||
await instance.connectToWhatsapp();
|
||||
this.logger.verbose('connectToWhatsapp: ' + name);
|
||||
|
||||
this.waInstances[name] = instance;
|
||||
};
|
||||
|
||||
try {
|
||||
if (this.redis.ENABLED) {
|
||||
const keys = await this.redisCache.instanceKeys();
|
||||
this.logger.verbose('redis enabled');
|
||||
await this.cache.connect(this.redis as Redis);
|
||||
const keys = await this.cache.instanceKeys();
|
||||
if (keys?.length > 0) {
|
||||
this.logger.verbose('reading instance keys and setting instances');
|
||||
keys.forEach(async (k) => await set(k.split(':')[1]));
|
||||
} else {
|
||||
this.logger.verbose('no instance keys found');
|
||||
initInstance();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
||||
this.logger.verbose('database enabled');
|
||||
await this.repository.dbServer.connect();
|
||||
const collections: any[] = await this.dbInstance.collections();
|
||||
if (collections.length > 0) {
|
||||
this.logger.verbose('reading collections and setting instances');
|
||||
collections.forEach(
|
||||
async (coll) => await set(coll.namespace.replace(/^[\w-]+\./, '')),
|
||||
);
|
||||
} else {
|
||||
this.logger.verbose('no collections found');
|
||||
initInstance();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.verbose('store in files enabled');
|
||||
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });
|
||||
for await (const dirent of dir) {
|
||||
if (dirent.isDirectory()) {
|
||||
this.logger.verbose('reading instance files and setting instances');
|
||||
const files = readdirSync(join(INSTANCE_DIR, dirent.name), {
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
@@ -176,6 +279,9 @@ export class WAMonitoringService {
|
||||
}
|
||||
|
||||
await set(dirent.name);
|
||||
} else {
|
||||
this.logger.verbose('no instance files found');
|
||||
initInstance();
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -185,22 +291,38 @@ export class WAMonitoringService {
|
||||
|
||||
private removeInstance() {
|
||||
this.eventEmitter.on('remove.instance', async (instanceName: string) => {
|
||||
this.logger.verbose('remove instance: ' + instanceName);
|
||||
try {
|
||||
this.logger.verbose('instance: ' + instanceName + ' - removing from memory');
|
||||
this.waInstances[instanceName] = undefined;
|
||||
} catch {}
|
||||
|
||||
try {
|
||||
this.logger.verbose('request cleaning up instance: ' + instanceName);
|
||||
this.cleaningUp(instanceName);
|
||||
} finally {
|
||||
this.logger.warn(`Instance "${instanceName}" - REMOVED`);
|
||||
}
|
||||
});
|
||||
this.eventEmitter.on('logout.instance', async (instanceName: string) => {
|
||||
this.logger.verbose('logout instance: ' + instanceName);
|
||||
try {
|
||||
this.logger.verbose('request cleaning up instance: ' + instanceName);
|
||||
this.cleaningUp(instanceName);
|
||||
} finally {
|
||||
this.logger.warn(`Instance "${instanceName}" - LOGOUT`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private noConnection() {
|
||||
this.logger.verbose('checking instances without connection');
|
||||
this.eventEmitter.on('no.connection', async (instanceName) => {
|
||||
try {
|
||||
this.logger.verbose('instance: ' + instanceName + ' - removing from memory');
|
||||
this.waInstances[instanceName] = undefined;
|
||||
|
||||
this.logger.verbose('request cleaning up instance: ' + instanceName);
|
||||
this.cleaningUp(instanceName);
|
||||
} catch (error) {
|
||||
this.logger.error({
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
import { InstanceDto } from '../dto/instance.dto';
|
||||
import { WebhookDto } from '../dto/webhook.dto';
|
||||
import { WAMonitoringService } from './monitor.service';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
|
||||
export class WebhookService {
|
||||
constructor(private readonly waMonitor: WAMonitoringService) {}
|
||||
|
||||
private readonly logger = new Logger(WebhookService.name);
|
||||
|
||||
public create(instance: InstanceDto, data: WebhookDto) {
|
||||
this.logger.verbose('create webhook: ' + instance.instanceName);
|
||||
this.waMonitor.waInstances[instance.instanceName].setWebhook(data);
|
||||
|
||||
return { webhook: { ...instance, webhook: data } };
|
||||
@@ -13,6 +17,7 @@ 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();
|
||||
} catch (error) {
|
||||
return { enabled: null, url: '' };
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/no-namespace */
|
||||
import { AuthenticationState, WAConnectionState } from '@evolution/base';
|
||||
import { AuthenticationState, WAConnectionState } from '@whiskeysockets/baileys';
|
||||
|
||||
export enum Events {
|
||||
APPLICATION_STARTUP = 'application.startup',
|
||||
@@ -9,7 +9,6 @@ export enum Events {
|
||||
MESSAGES_SET = 'messages.set',
|
||||
MESSAGES_UPSERT = 'messages.upsert',
|
||||
MESSAGES_UPDATE = 'messages.update',
|
||||
SEND_MESSAGE = 'send.message',
|
||||
CONTACTS_SET = 'contacts.set',
|
||||
CONTACTS_UPSERT = 'contacts.upsert',
|
||||
CONTACTS_UPDATE = 'contacts.update',
|
||||
@@ -34,7 +33,12 @@ export declare namespace wa {
|
||||
profilePictureUrl?: string;
|
||||
};
|
||||
|
||||
export type LocalWebHook = { enabled?: boolean; url?: string };
|
||||
export type LocalWebHook = {
|
||||
enabled?: boolean;
|
||||
url?: string;
|
||||
events?: string[];
|
||||
webhook_by_events?: boolean;
|
||||
};
|
||||
|
||||
export type StateConnection = {
|
||||
instance?: string;
|
||||
|
||||
@@ -27,8 +27,9 @@ import { WebhookRepository } from './repository/webhook.repository';
|
||||
import { WebhookModel } from './models/webhook.model';
|
||||
import { AuthRepository } from './repository/auth.repository';
|
||||
import { WAStartupService } from './services/whatsapp.service';
|
||||
import { delay } from '@evolution/base';
|
||||
import { delay } from '@whiskeysockets/baileys';
|
||||
import { Events } from './types/wa.types';
|
||||
import { RedisCache } from '../db/redis.client';
|
||||
|
||||
const logger = new Logger('WA MODULE');
|
||||
|
||||
@@ -46,10 +47,18 @@ export const repository = new RepositoryBroker(
|
||||
messageUpdateRepository,
|
||||
webhookRepository,
|
||||
authRepository,
|
||||
configService,
|
||||
dbserver?.getClient(),
|
||||
);
|
||||
|
||||
export const waMonitor = new WAMonitoringService(eventEmitter, configService, repository);
|
||||
export const cache = new RedisCache();
|
||||
|
||||
export const waMonitor = new WAMonitoringService(
|
||||
eventEmitter,
|
||||
configService,
|
||||
repository,
|
||||
cache,
|
||||
);
|
||||
|
||||
const authService = new AuthService(configService, waMonitor, repository);
|
||||
|
||||
@@ -64,6 +73,7 @@ export const instanceController = new InstanceController(
|
||||
eventEmitter,
|
||||
authService,
|
||||
webhookService,
|
||||
cache,
|
||||
);
|
||||
export const viewsController = new ViewsController(waMonitor, configService);
|
||||
export const sendMessageController = new SendMessageController(waMonitor);
|
||||
@@ -71,10 +81,11 @@ export const chatController = new ChatController(waMonitor);
|
||||
export const groupController = new GroupController(waMonitor);
|
||||
|
||||
export async function initInstance() {
|
||||
const instance = new WAStartupService(configService, eventEmitter, repository);
|
||||
const instance = new WAStartupService(configService, eventEmitter, repository, cache);
|
||||
|
||||
const mode = configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
|
||||
|
||||
logger.verbose('Sending data webhook for event: ' + Events.APPLICATION_STARTUP);
|
||||
instance.sendDataWebhook(
|
||||
Events.APPLICATION_STARTUP,
|
||||
{
|
||||
@@ -85,9 +96,14 @@ export async function initInstance() {
|
||||
);
|
||||
|
||||
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);
|
||||
|
||||
instance.instanceName = instanceName;
|
||||
|
||||
@@ -96,13 +112,17 @@ export async function initInstance() {
|
||||
|
||||
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) {
|
||||
this.logger.log(error);
|
||||
logger.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +140,7 @@ export async function initInstance() {
|
||||
return await this.connectionState({ instanceName });
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.log(error);
|
||||
logger.log(error);
|
||||
}
|
||||
|
||||
const result = {
|
||||
|
||||
24
start.sh
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
|
||||
if [ "$DOCKER_ENV" = "true" ];
|
||||
then
|
||||
echo "Enabling environment variables for Docker"
|
||||
echo "DOCKER_ENV=$DOCKER_ENV"
|
||||
echo
|
||||
else
|
||||
cp ./src/env.yml ./dist/src
|
||||
fi
|
||||
echo "> removing dist"
|
||||
rm -rf ./dist
|
||||
echo
|
||||
echo "> transpiling..."
|
||||
npm run build
|
||||
|
||||
echo
|
||||
echo "> Successfully build "
|
||||
|
||||
echo
|
||||
echo "> Starting application..."
|
||||
echo
|
||||
|
||||
node ./dist/src/main.js
|
||||