API WHATSAPP (OPEN SOURCE)
Evolution API: Crie Sua Própria API para Whatsapp
Gostaria de compartilhar uma ferramenta incrivelmente útil: a Evolution API. Com esta plataforma, você pode facilmente desenvolver e personalizar a sua própria API.
Basta implementá-la em sua VPS ou servidor dedicado e comece a criar múltiplas instâncias para atender às suas necessidades específicas.
NPM Installation Tested on v1.5.0
ENVIRONMENT PREPARATION FOR NPM
Download and install Node.js
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - apt-get install -y nodejs
Update NPM to the latest version
npm install -g npm@latest
Install the other necessary/recommended apps for installing and running the Evolution API
npm install -g pm2@latest apt-get install -y git zip unzip nload snapd curl wget sudo
Update the Time Zone according to your installation location
dpkg-reconfigure tzdata
Choose the corresponding TimeZone
TIMEZONE
Update the system and install the necessary packages.
apt update && apt -y upgrade
Note:
If a window appears with information about apps to restart, check all of them and confirm.
Reboot the system
reboot
Important:
After receiving the message that it is now available, press CTRL + SHIFT + P to reconnect INSTALLING THE EVOLUTION API
Clone the Evoluton API repository
git clone https://github.com/EvolutionAPI/evolution-api.git
Access the API folder and install
cd evolution-api npm install
Copy and edit the API configuration file
cp src/dev-env.yml src/env.yml nano src/env.yml
⚠️ ALL SETTINGS DEFINED IN THIS FILE ARE APPLIED TO ALL INSTANCES.
⚠️ RENAME THIS FILE TO env.yml
Choose the server type for the application
SERVER: TYPE: http # https PORT: 8080 # 443 URL: API-DOMAIN
CORS: ORIGIN: - "*" # - yourdomain.com METHODS: - POST - GET - PUT - DELETE CREDENTIALS: true
Install ssl certificate and replace string with domain name
Access: https://certbot.eff.org/instructions?ws=other&os=ubuntufocal
SSL_CONF: PRIVKEY: /etc/letsencrypt/live//privkey.pem FULLCHAIN: /etc/letsencrypt/live//fullchain.pem
Determine the logs to be displayed
LOG: LEVEL: - ERROR - WARN - DEBUG - INFO - 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
If you don't even want an expiration, enter the value false
DEL_INSTANCE: false # or false
Temporary data storage
STORE: 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
Permanent data storage
DATABASE: ENABLED: false CONNECTION: URI: "mongodb://root:root@localhost:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true" DB_PREFIX_NAME: evolution
Choose the data you want to save in the application's database or store
SAVE_DATA: INSTANCE: false NEW_MESSAGE: false MESSAGE_UPDATE: false CONTACTS: false CHATS: false
REDIS: ENABLED: false URI: "redis://localhost:6379" PREFIX_KEY: "evolution"
RABBITMQ: ENABLED: false URI: "amqp://guest:guest@localhost:5672"
WEBSOCKET: ENABLED: false
Global Webhook Settings
Each instance's Webhook URL and events will be requested at the time it is created
WEBHOOK:
Define a global webhook that will listen for enabled events from all instances
GLOBAL: URL: 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_BY_EVENTS: false
Automatically maps webhook paths
Set the events you want to hear
EVENTS: APPLICATION_STARTUP: false QRCODE_UPDATED: true MESSAGES_SET: true MESSAGES_UPSERT: true MESSAGES_UPDATE: true MESSAGES_DELETE: true SEND_MESSAGE: true CONTACTS_SET: true CONTACTS_UPSERT: true CONTACTS_UPDATE: true PRESENCE_UPDATE: true CHATS_SET: true CHATS_UPSERT: true CHATS_UPDATE: true CHATS_DELETE: true GROUPS_UPSERT: true GROUP_UPDATE: true GROUP_PARTICIPANTS_UPDATE: true CONNECTION_UPDATE: true CALL: true # This event fires every time a new token is requested via the refresh route NEW_JWT_TOKEN: false
CONFIG_SESSION_PHONE:
Name that will be displayed on smartphone connection
CLIENT: "Evolution API" NAME: chrome # chrome | firefox | edge | opera | safari
Set qrcode display limit
QRCODE: LIMIT: 30 COLOR: '#198754'
Defines an authentication type for the api
We recommend using the apikey because it will allow you to use a custom token,
if you use jwt, a random token will be generated and may be expired and you will have to generate a new token
AUTHENTICATION: TYPE: apikey # jwt or apikey
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: GLOBAL-API-KEY
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 SECRET: L=0YWt]b2w[WF>#>:&E`
Important:
If you do not want any of the types of LOG records to be displayed, simply insert a # character in front of the event, as in the example below, to not display the VERBOSE:
LOG:
LEVEL:
- ERROR
- WARN
- DEBUG
- INFO
- LOG
#- VERBOSE
- DARK
- WEBHOOKS
The Evolution API defaults to display all.
INITIALIZING THE EVOLUTION API
Start the API
npm run start:prod
Check if the two pieces of information appear in the LOGS listing below the lines below:
INFO [WA MODULE] [string] Module - ON LOG [SERVER] [string] HTTP - ON: 8080
The others of the VERBOSE type are only informative Press CTRL + C to end the API
Configure API initialization
pm2 start 'npm run start:prod' --name ApiEvolution pm2 startup pm2 save --force
Eventually you can complement the command with the option below, to increase the memory to be used by PM2:
pm2 start 'npm run start:prod' --name ApiEvolution -- start --node-args="--max-old-space-size=4096" --max-memory-restart 4G
In the example above, the VPS is assumed to have at least 4GB of RAM available only for the Evolution API
Test if the Evolution API is working through the address IP:PORT:
If it works, the following message will appear in your browser:
{"status":200,"message":"Welcome to the Evolution API, it is working!"}
UPDATING THE EVOLUTION API
Commands for updating the API
Warning:
ALWAYS take a snapshot of your VPS before performing the upgrade procedure. <<< ALL CHANGES AND CUSTOMIZATION MADE DIRECTLY TO THE CODE WILL BE LOST. <<< ALL LOGS WILL BE DELETED, FREEING UNNECESSARY USED SPACE. <<< Log into the API installation folder via the Bitvise Console and run the commands below:
cd ~/evolution-api/
pm2 flush pm2 stop ApiEvolution git reset --hard HEAD git pull git checkout main rm -rf node_modules npm i pm2 start ApiEvolution pm2 log ApiEvolution
TUTORIAL IN TEXT FILE (pt-BR only)
Download the Evolution API Installation Tutorial in text file.
Click on the link below, save the file on your computer and unzip it.
https://evolution-api.com/files/evolution-api-install-on-npm.zip
Nginx Installation Tested on v1.5.0
REVERSE PROXY CONFIGURATION
Login to server via Bitvise Console
Server : VPS-IP User: root Password : PASSWORD-ROOT-VPS
Install, start, enable and test the Nginx service
apt-get install -y nginx
systemctl start nginx systemctl enable nginx systemctl status nginx
If the information "Active: active (running)" appears in green, it means that Nginx is running and you can move on to the next step.
Note:
If the screen freezes with an (END) in the final line, press CTRL + C or just the Q key Test if the Nginx web server is working by visiting the address below:
If it works, a page will appear in your browser with the text:
If it works, a page will appear in your browser with the text:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com.
Thank you for using nginx.
Install the application needed to certify the Evolution API domain
snap install --classic certbot
Remove Nginx Enabled Default Website File
rm /etc/nginx/sites-enabled/default
Create a new server block file in the directory
nano /etc/nginx/conf.d/default.conf
Copy & Paste:
server { listen 80; listen [::]:80; server_name _; root /var/www/html/; index index.php index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ /index.php; }
location ~ .php$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include snippets/fastcgi-php.conf; }
A long browser cache lifetime can speed up repeat visits to your page
location ~* .(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ { access_log off; log_not_found off; expires 360d; }
disable access to hidden files
location ~ /.ht { access_log off; log_not_found off; deny all; } }
Validate Nginx configuration for subdomain
nginx -t
If it works, the following message will appear in your terminal:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
If there are no problems, restart Nginx to enable your changes.
systemctl reload nginx
Make the nginx user the owner of the web directory.
By default, it is owned by the root user.
chown www-data:www-data /usr/share/nginx/html -R
REVERSE PROXY CONFIGURATION
Create Virtual Host pointing to subdomain
cd ~ nano /etc/nginx/sites-available/api
server { server_name API-DOMAIN;
location / { proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache_bypass $http_upgrade; } }
ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled
Validate Nginx configuration for subdomain
nginx -t
If it works, the following message will appear in your terminal:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
If there are no problems, restart Nginx to enable your changes.
systemctl reload nginx
CERTIFIING THE SUBDOMAIN
Certify API subdomain
certbot --nginx -d API-DOMAIN
You will be asked if you want to enter an email to receive notifications when the certificate generated is close to the expiration date.
Enter your email address:
EMAIL-CERTBOT
Press Y on the two messages that appear.
Type the number corresponding to the subdomain you want to certify and press ENTER.
If the certification is successful, at the end of the process a line will be displayed with the following message:
"Congratulations! You have successfully enabled HTTPS..."
If this message does not appear, repeat the steps from the creation of the Virtual Host
List API subdomain certificate
ls -l /etc/letsencrypt/live/DOMINIO-DA-API/
Test if the Evolution API is working on the certified subdomain by visiting the address below:
If it works, the following message will appear in your browser:
{"status":200,"message":"Welcome to the Evolution API, it is working!","version":"{{version}}"} Important:
Confirm that the padlock appears closed on the left side of the browser address bar.
chegou a usar essa api para algum projeto? o que achou? mais simples/estavel/confiavel do que as outras que tem por ai?
Opa. Estou procurando algo assim mesmo. Sabe dizer se tem interface/sistema pra visualizar as informações? Ou é só a API mesmo?