Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #22 from matheusmaiberg/main
Browse files Browse the repository at this point in the history
fix authentication api key example
  • Loading branch information
DavidsonGomes authored Mar 7, 2024
2 parents aa2ace8 + 763ea49 commit 4346d6e
Show file tree
Hide file tree
Showing 11 changed files with 2,517 additions and 818 deletions.
10 changes: 5 additions & 5 deletions docs/01-Get Started/Installation/Docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The fastest way to deploy EvolutionAPI with Docker is using `docker run` in the
```bash title="Linux Command Line Interface" live
docker run --name evolution-api --detach \
-p 8080:8080 \
-e API_KEY=YOUR_SUPER_SECURE_AUTHENTICATION_KEY \
-e AUTHENTICATION_API_KEY=YOUR_SUPER_SECURE_AUTHENTICATION_KEY \
atendai/evolution-api \
node ./dist/src/main.js
```
Expand Down Expand Up @@ -67,7 +67,7 @@ Run the following command to deploy the EvolutionAPI with the necessary volumes.
```bash title="Linux Command Line Interface" live
docker run --name evolution-api --detach \
-p 8080:8080 \
-e API_KEY=YOUR_SUPER_SECURE_AUTHENTICATION_KEY \
-e AUTHENTICATION_API_KEY=YOUR_SUPER_SECURE_AUTHENTICATION_KEY \
-v evolution_store:/evolution/store \
-v evolution_instances:/evolution/instances \
atendai/evolution-api \
Expand Down Expand Up @@ -110,7 +110,7 @@ services:
ports:
- "8080:8080"
environment:
- API_KEY=YOUR_SUPER_SECURE_AUTHENTICATION_KEY
- AUTHENTICATION_API_KEY=YOUR_SUPER_SECURE_AUTHENTICATION_KEY
volumes:
- evolution_store:/evolution/store
- evolution_instances:/evolution/instances
Expand Down Expand Up @@ -162,7 +162,7 @@ This is an example image with some of the environments variables set for fast co
Assuming that you wil have an MongoDB and a RabbitMQ container running container running. Read the optional resources section for detailed information.
:::

This example should work for most of use cases, if you want more in-depth over personalized configuration and installation check the environment variables section.
This example should work for most of use cases, if you want more in-depth over personalized configuration and installation check the [environment variables](https://doc.evolution-api.com/docs/variables) section.

```yaml title="docker-compose.yaml" showLineNumbers
version: "3.8"
Expand All @@ -175,7 +175,7 @@ x-variables:
CONFIG_SESSION_PHONE_CLIENT: "RENAME ME WITH YOUR COMPANY NAME"
# ApiKey Config for authentication High Encryption AES 256 from https://acte.ltd/utils/randomkeygen
AUTHENTICATION_TYPE: "apikey"
AUTHENTICATION_API_KEY: "YOUR_SUPER_SECURE_KEY"
AUTHENTICATION_AUTHENTICATION_API_KEY: "YOUR_SUPER_SECURE_KEY"
# Database
DATABASE_ENABLED: "true"
DATABASE_CONNECTION_URI: "mongodb://root:YOUR_SUPER_SECURE_PASSWORD@mongodb:27017/?authSource=admin&readPreference=primary&ssl=false&directConnection=true"
Expand Down
260 changes: 227 additions & 33 deletions docs/01-Get Started/Optional resources/RabbitMQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,50 @@ last_update:

## Activating RabbitMQ

To effectively utilize RabbitMQ with the Evolution API for managing WhatsApp instances, it's essential to activate RabbitMQ on each individual WhatsApp instance. This activation allows each instance to start receiving and processing AMQP (Advanced Message Queuing Protocol) queue requests that are specific to that particular WhatsApp instance.
To effectively utilize RabbitMQ with the Evolution API for managing WhatsApp instances, it's essential to activate RabbitMQ on each individual WhatsApp instance.

In other words, for each WhatsApp instance where you want to use RabbitMQ, you need to ensure that RabbitMQ integration is enabled. This setup will allow the instance to communicate with the RabbitMQ server and handle its queue of messages and requests. Enabling RabbitMQ on each instance is crucial for proper distribution and management of messaging tasks across different WhatsApp instances in your system.
This activation allows each instance to start receiving and processing AMQP (Advanced Message Queuing Protocol) queue requests that are specific to that particular WhatsApp instance.

### RabbitMQ setup for an individual instance
In other words, for each WhatsApp instance where you want to use RabbitMQ, you need to ensure that RabbitMQ integration is enabled.

This setup will allow the instance to communicate with the RabbitMQ server and handle its queue of messages and requests.

Enabling RabbitMQ on each instance is crucial for proper distribution and management of messaging tasks across different WhatsApp instances in your system.

### RabbitMQ instance setup

For **developers** who wants to use in their applications AMQP messaging system, you could use RabbitMQ for queue your instances actions.

To configure RabbitMQ for individual WhatsApp instances in the Evolution API, you can use the following endpoint:

```plaintext title="POST"
[baseUrl]/rabbitmq/set/[instance_name]
```

This endpoint allows you to enable RabbitMQ and specify which events each WhatsApp instance should subscribe to in the AMQP queue. Below is an example of the JSON body for this endpoint:

```json title="body" showLineNumbers
```json title="POST /rabbitmq/set/{instance_name}" showLineNumbers
{
"enabled": true,
"events": [
// List of events to subscribe to. Uncomment the events you need.
"APPLICATION_STARTUP",
"QRCODE_UPDATED",
"MESSAGES_SET",
"MESSAGES_UPSERT",
"MESSAGES_UPDATE",
"MESSAGES_DELETE",
"SEND_MESSAGE",
"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",
"CALL",
"NEW_JWT_TOKEN"
]
"enabled": true,
"events": [
"APPLICATION_STARTUP",
"QRCODE_UPDATED",
"MESSAGES_SET",
"MESSAGES_UPSERT",
"MESSAGES_UPDATE",
"MESSAGES_DELETE",
"SEND_MESSAGE",
"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",
"CALL",
"NEW_JWT_TOKEN"
]
}
```

Expand All @@ -82,3 +83,196 @@ Now you can send to your application the messages and consume them in RabbitMQ.
If you want more in-depth over personalized configuration and installation check the environment variables section.

:::

## Consuming RabbitMQ messages

There is many ways to consume the queue of the application, for users who do do not know how to code, n8n a low code automation tool could be used to consume the queue.

### n8n

Use the RabbitMQ webhook node to consume the queue.

### NodeJs

First install the `amqp.node` package

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs>
<TabItem value="npm" label="npm">
```bash
npm i amqplib @types/amqplib --save
```
</TabItem>

<TabItem value="pnpm" label="pnpm">
```bash
pnpm i amqplib @types/amqplib --save
```
</TabItem>

<TabItem value="bun" label="bun">
```bash
bun i amqplib @types/amqplib --save
```
</TabItem>
</Tabs>

Now, lets consume RabbitMQ events using the `amqp` lib consumer.

```javascript
import amqp from 'amqplib';

class RabbitMQConsumer {
constructor(instanceName) {
this.instanceName = instanceName;
this.connection = null;
this.channel = null;
}

async init() {
try {
this.connection = await amqp.connect('amqp://admin:admin@rabbitmq:5672');
this.channel = await this.connection.createChannel();

await this.setupExchangesAndQueues();
} catch (error) {
console.log(`Error initializing RabbitMQConsumer: ${error}`);
}
}

async setupExchangesAndQueues() {
let queueBindings = this.getQueueBindings();

for (let { queueName, bindingKey } of queueBindings) {
await this.createQueue(queueName, bindingKey);
this.consumeQueue(queueName);
}
}

getQueueBindings() {
return [
{
queueName: ${instanceName}.state.instance,
bindingKey: "state.instance"
},
{
queueName: ${instanceName}.connection.update,
bindingKey: "state.instance"
},
{
queueName: ${instanceName}.qrcode.update,
bindingKey: "qrcode.update"
},
{
queueName: ${instanceName}.chats.upsert,
bindingKey: "chats.upsert"
},
{
queueName: ${instanceName}.contacts.upsert,
bindingKey: "contacts.upsert"
},
{
queueName: ${instanceName}.contacts.update,
bindingKey: "contacts.update"
},
{
queueName: ${instanceName}.chats.set,
bindingKey: "chats.set"
},
{
queueName: ${instanceName}.messages.set,
bindingKey: "messages.set"
},
{
queueName: ${instanceName}.messages.upsert,
bindingKey: "messages.upsert"
},
{
queueName: ${instanceName}.messages.update,
bindingKey: "messages.update"
},
{
queueName: ${instanceName}.groups.upsert,
bindingKey: "groups.upsert"
}, {
queueName: ${instanceName}.qrcode.updated,
bindingKey: "qrcode.updated"
},
{
queueName: ${instanceName}.connection.update,
bindingKey: "connection.update"
},
{
queueName: ${instanceName}.messages.upsert,
bindingKey: "messages.upsert"
},
{
queueName: ${instanceName}.send.message,
bindingKey: "send.message"
},
{
queueName: ${instanceName}.messages.update,
bindingKey: "messages.update"
},
{
queueName: ${instanceName}.messages.delete,
bindingKey: "messages.delete"
}
];
}

async createQueue(queueName, bindingKey) {
await this.channel.assertQueue(queueName, { durable: true });
await this.channel.bindQueue(queueName, exchangeName, bindingKey);
}

consumeQueue(queueName) {
this.channel.consume(queueName, message => {
if (message !== null) {
// Here you can change the logic for your application
console.log(`Received message from ${queueName}: ${message.content.toString()}`);
this.channel.ack(message);
}
});
}
}

// Uso
const rabbitMQConsumer = new RabbitMQConsumer('instanceName');
rabbitMQConsumer.init();

```

### Binding keys

Once connected, you have the capability to listen for various events emitted by the server. Each event may carry data that is pertinent to the context of the event. For example, if you are subscribed to message updates, you might receive data containing the content and metadata of the updated message.

The table below outlines the key binding keys available and the type of information or notification each represents:


<!-- prettier-ignore -->
**bindingKey** | **Description**
--- | ---
application.startup | Notifies you when an application startup.
qrcode.updated | Sends the base64 of the qrcode for reading
connection.update | Informs the status of the connection with WhatsApp
message.set | Sends a list of all your messages uploaded on WhatsApp. This event occurs only once
message.upsert | Notifies you when a message is received
message.update | Tells you when a message is updated
message.delete | Tells you when a message is deleted
send.message | Notifies when a message is sent
contacts.set | Performs initial loading of all contacts.This event occurs only once
contacts.upsert | Reloads all contacts with additional information.This event occurs only once
contacts.update | Informs you when the chat is updated
presence.update | Informs if the user is online, if he is performing some action like writing or recording and his last seen: 'unavailable', 'available', 'composing', 'recording', 'paused'
chats.set | Send a list of all loaded chats
chats.update | Informs you when the chat is updated
chats.upsert | Sends any new chat information
groups.upsert | Notifies when a group is created
groups.update | Notifies when a group has its information updated
group-participants.update | Notifies when an action occurs involving a participant: 'add', 'remove', 'promote', 'demote'
new.jwt | Notifies when the token (jwt) is updated

This structure provides a clear and organized understanding of the events you can listen for through the API, facilitating the implementation of specific logic in response to each type of notification or data received.
43 changes: 21 additions & 22 deletions docs/01-Get Started/Optional resources/Websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,29 +223,28 @@ In this example, replace event_name with the specific event you want to listen t

Once connected, you can listen for various events emitted by the server. Each event may carry data relevant to the event's context. For instance, if you're listening for message updates, you might receive data containing the updated message content and metadata.


<!-- prettier-ignore -->
**Event** | **Description**
--- | ---
application.startup | Notifies you when an application startup.
qrcode.updated| Sends the base64 of the qrcode for reading
connection.update | Informs the status of the connection with WhatsApp
message.set | Sends a list of all your messages uploaded on WhatsApp. This event occurs only once
message.upsert | Notifies you when a message is received
message.update | Tells you when a message is updated
message.delete | Tells you when a message is deleted
send.message | Notifies when a message is sent
contacts.set | Performs initial loading of all contacts.This event occurs only once
contacts.upsert | Reloads all contacts with additional information.This event occurs only once
contacts.update | Informs you when the chat is updated
presence.update | Informs if the user is online, if he is performing some action like writing or recording and his last seen: 'unavailable', 'available', 'composing', 'recording', 'paused'
chats.set | Send a list of all loaded chats
chats.update | Informs you when the chat is updated
chats.upsert | Sends any new chat information
groups.upsert | Notifies when a group is created
groups.update | Notifies when a group has its information updated
group-participants.update | Notifies when an action occurs involving a participant: 'add', 'remove', 'promote', 'demote'
new.jwt | Notifies when the token (jwt) is updated
**Event** | **Description**
--- | ---
application.startup | Notifies you when an application startup.
qrcode.updated | Sends the base64 of the qrcode for reading
connection.update | Informs the status of the connection with WhatsApp
messages.set | Sends a list of all your messages uploaded on WhatsApp. This event occurs only once
messages.upsert | Notifies you when a message is received
messages.update | Tells you when a message is updated
messages.delete | Tells you when a message is deleted
send.message | Notifies when a message is sent
contacts.set | Performs initial loading of all contacts.This event occurs only once
contacts.upsert | Reloads all contacts with additional information.This event occurs only once
contacts.update | Informs you when the chat is updated
presence.update | Informs if the user is online, if he is performing some action like writing or recording and his last seen: 'unavailable', 'available', 'composing', 'recording', 'paused'
chats.set | Send a list of all loaded chats
chats.update | Informs you when the chat is updated
chats.upsert | Sends any new chat information
groups.upsert | Notifies when a group is created
groups.update | Notifies when a group has its information updated
group-participants.update | Notifies when an action occurs involving a participant: 'add', 'remove', 'promote', 'demote'
new.jwt | Notifies when the token (jwt) is updated

### Sending Messages

Expand Down
Loading

0 comments on commit 4346d6e

Please sign in to comment.