docs: Update README documentation with WebSocket examples and configuration improvements

- Add detailed WebSocket configuration examples
- Include instructions for creating a WebSocket using the client method
- Expand the events section with clearer explanations
- Add notes on security and best practices for API tokens
This commit is contained in:
Davidson Gomes 2025-03-06 14:33:26 -03:00
parent 15e229cfb4
commit 043a7f9b3c

119
README.md
View File

@ -24,11 +24,13 @@ client = EvolutionClient(
### Instance Management ### Instance Management
#### List Instances #### List Instances
```python ```python
instances = client.instances.fetch_instances() instances = client.instances.fetch_instances()
``` ```
#### Create New Instance #### Create New Instance
```python ```python
from evolutionapi.models.instance import InstanceConfig from evolutionapi.models.instance import InstanceConfig
@ -59,6 +61,7 @@ new_instance = client.instances.create_instance(config)
``` ```
#### Configure Webhook #### Configure Webhook
```python ```python
from evolutionapi.models.instance import WebhookConfig from evolutionapi.models.instance import WebhookConfig
@ -92,6 +95,7 @@ response = client.instances.set_webhook(instance_id, config, instance_token)
``` ```
#### Configure Events #### Configure Events
```python ```python
from evolutionapi.models.instance import EventsConfig from evolutionapi.models.instance import EventsConfig
@ -120,6 +124,7 @@ response = client.instances.set_events(instance_id, config, instance_token)
``` ```
#### Configure Chatwoot Integration #### Configure Chatwoot Integration
```python ```python
from evolutionapi.models.instance import ChatwootConfig from evolutionapi.models.instance import ChatwootConfig
@ -143,31 +148,37 @@ response = client.instances.set_chatwoot(instance_id, config, instance_token)
``` ```
#### Delete Instance #### Delete Instance
```python ```python
response = client.instances.delete_instance(instance_id, instance_token) response = client.instances.delete_instance(instance_id, instance_token)
``` ```
#### Get Instance Info #### Get Instance Info
```python ```python
response = client.instances.get_instance_info(instance_id, instance_token) response = client.instances.get_instance_info(instance_id, instance_token)
``` ```
#### Get Instance QR Code #### Get Instance QR Code
```python ```python
response = client.instances.get_instance_qrcode(instance_id, instance_token) response = client.instances.get_instance_qrcode(instance_id, instance_token)
``` ```
#### Get Instance Status #### Get Instance Status
```python ```python
response = client.instances.get_instance_status(instance_id, instance_token) response = client.instances.get_instance_status(instance_id, instance_token)
``` ```
#### Logout Instance #### Logout Instance
```python ```python
response = client.instances.logout_instance(instance_id, instance_token) response = client.instances.logout_instance(instance_id, instance_token)
``` ```
#### Restart Instance #### Restart Instance
```python ```python
response = client.instances.restart_instance(instance_id, instance_token) response = client.instances.restart_instance(instance_id, instance_token)
``` ```
@ -175,16 +186,19 @@ response = client.instances.restart_instance(instance_id, instance_token)
### Instance Operations ### Instance Operations
#### Connect Instance #### Connect Instance
```python ```python
state = client.instance_operations.connect(instance_id, instance_token) state = client.instance_operations.connect(instance_id, instance_token)
``` ```
#### Check Connection State #### Check Connection State
```python ```python
state = client.instance_operations.get_connection_state(instance_id, instance_token) state = client.instance_operations.get_connection_state(instance_id, instance_token)
``` ```
#### Set Presence #### Set Presence
```python ```python
from evolutionapi.models.presence import PresenceConfig, PresenceStatus from evolutionapi.models.presence import PresenceConfig, PresenceStatus
@ -204,6 +218,7 @@ response = client.instance_operations.set_presence(instance_id, config, instance
### Sending Messages ### Sending Messages
#### Text Message #### Text Message
```python ```python
from evolutionapi.models.message import TextMessage, QuotedMessage from evolutionapi.models.message import TextMessage, QuotedMessage
@ -250,6 +265,7 @@ response = client.messages.send_text(instance_id, message, instance_token)
``` ```
#### Media Message #### Media Message
```python ```python
from evolutionapi.models.message import MediaMessage, MediaType, QuotedMessage from evolutionapi.models.message import MediaMessage, MediaType, QuotedMessage
@ -299,6 +315,7 @@ response = client.messages.send_media(instance_id, message, instance_token)
``` ```
#### Status Message #### Status Message
```python ```python
from evolutionapi.models.message import StatusMessage, StatusType, FontType from evolutionapi.models.message import StatusMessage, StatusType, FontType
@ -337,6 +354,7 @@ response = client.messages.send_status(instance_id, message, instance_token)
``` ```
#### Location Message #### Location Message
```python ```python
from evolutionapi.models.message import LocationMessage from evolutionapi.models.message import LocationMessage
@ -353,6 +371,7 @@ response = client.messages.send_location(instance_id, message, instance_token)
``` ```
#### Contact Message #### Contact Message
```python ```python
from evolutionapi.models.message import ContactMessage, Contact from evolutionapi.models.message import ContactMessage, Contact
@ -374,6 +393,7 @@ response = client.messages.send_contact(instance_id, message, instance_token)
``` ```
#### Reaction Message #### Reaction Message
```python ```python
from evolutionapi.models.message import ReactionMessage from evolutionapi.models.message import ReactionMessage
@ -392,6 +412,7 @@ response = client.messages.send_reaction(instance_id, message, instance_token)
``` ```
#### Poll Message #### Poll Message
```python ```python
from evolutionapi.models.message import PollMessage from evolutionapi.models.message import PollMessage
@ -407,6 +428,7 @@ response = client.messages.send_poll(instance_id, message, instance_token)
``` ```
#### Button Message #### Button Message
```python ```python
from evolutionapi.models.message import ButtonMessage, Button from evolutionapi.models.message import ButtonMessage, Button
@ -464,6 +486,7 @@ response = client.messages.send_buttons(instance_id, message, instance_token)
``` ```
#### List Message #### List Message
```python ```python
from evolutionapi.models.message import ListMessage, ListSection, ListRow from evolutionapi.models.message import ListMessage, ListSection, ListRow
@ -501,6 +524,7 @@ response = client.messages.send_list(instance_id, message, instance_token)
### Group Management ### Group Management
#### Create Group #### Create Group
```python ```python
from evolutionapi.models.group import CreateGroup from evolutionapi.models.group import CreateGroup
@ -514,6 +538,7 @@ response = client.group.create_group(instance_id, config, instance_token)
``` ```
#### Update Group Picture #### Update Group Picture
```python ```python
from evolutionapi.models.group import GroupPicture from evolutionapi.models.group import GroupPicture
@ -525,6 +550,7 @@ response = client.group.update_group_picture(instance_id, "group_jid", config, i
``` ```
#### Update Group Subject #### Update Group Subject
```python ```python
from evolutionapi.models.group import GroupSubject from evolutionapi.models.group import GroupSubject
@ -536,6 +562,7 @@ response = client.group.update_group_subject(instance_id, "group_jid", config, i
``` ```
#### Update Group Description #### Update Group Description
```python ```python
from evolutionapi.models.group import GroupDescription from evolutionapi.models.group import GroupDescription
@ -547,6 +574,7 @@ response = client.group.update_group_description(instance_id, "group_jid", confi
``` ```
#### Send Group Invite #### Send Group Invite
```python ```python
from evolutionapi.models.group import GroupInvite from evolutionapi.models.group import GroupInvite
@ -560,6 +588,7 @@ response = client.group.send_group_invite(instance_id, config, instance_token)
``` ```
#### Manage Participants #### Manage Participants
```python ```python
from evolutionapi.models.group import UpdateParticipant from evolutionapi.models.group import UpdateParticipant
@ -591,6 +620,7 @@ response = client.group.update_participant(instance_id, "group_jid", config, ins
``` ```
#### Update Group Settings #### Update Group Settings
```python ```python
from evolutionapi.models.group import UpdateSetting from evolutionapi.models.group import UpdateSetting
@ -618,6 +648,7 @@ response = client.group.update_setting(instance_id, "group_jid", config, instanc
``` ```
#### Toggle Ephemeral Messages #### Toggle Ephemeral Messages
```python ```python
from evolutionapi.models.group import ToggleEphemeral from evolutionapi.models.group import ToggleEphemeral
@ -631,6 +662,7 @@ response = client.group.toggle_ephemeral(instance_id, "group_jid", config, insta
### Profile Management ### Profile Management
#### Fetch Profile #### Fetch Profile
```python ```python
from evolutionapi.models.profile import FetchProfile from evolutionapi.models.profile import FetchProfile
@ -642,6 +674,7 @@ response = client.profile.fetch_profile(instance_id, config, instance_token)
``` ```
#### Update Profile Name #### Update Profile Name
```python ```python
from evolutionapi.models.profile import ProfileName from evolutionapi.models.profile import ProfileName
@ -653,6 +686,7 @@ response = client.profile.update_profile_name(instance_id, config, instance_toke
``` ```
#### Update Status #### Update Status
```python ```python
from evolutionapi.models.profile import ProfileStatus from evolutionapi.models.profile import ProfileStatus
@ -664,6 +698,7 @@ response = client.profile.update_profile_status(instance_id, config, instance_to
``` ```
#### Update Profile Picture #### Update Profile Picture
```python ```python
from evolutionapi.models.profile import ProfilePicture from evolutionapi.models.profile import ProfilePicture
@ -675,6 +710,7 @@ response = client.profile.update_profile_picture(instance_id, config, instance_t
``` ```
#### Configure Privacy Settings #### Configure Privacy Settings
```python ```python
from evolutionapi.models.profile import PrivacySettings from evolutionapi.models.profile import PrivacySettings
@ -693,6 +729,7 @@ response = client.profile.update_privacy_settings(instance_id, config, instance_
### Chat Operations ### Chat Operations
#### Check WhatsApp Numbers #### Check WhatsApp Numbers
```python ```python
from evolutionapi.models.chat import CheckIsWhatsappNumber from evolutionapi.models.chat import CheckIsWhatsappNumber
@ -704,6 +741,7 @@ response = client.chat.check_is_whatsapp_numbers(instance_id, config, instance_t
``` ```
#### Mark Message as Read #### Mark Message as Read
```python ```python
from evolutionapi.models.chat import ReadMessage from evolutionapi.models.chat import ReadMessage
@ -717,6 +755,7 @@ response = client.chat.mark_message_as_read(instance_id, [message], instance_tok
``` ```
#### Archive Chat #### Archive Chat
```python ```python
from evolutionapi.models.chat import ArchiveChat from evolutionapi.models.chat import ArchiveChat
@ -740,6 +779,7 @@ response = client.chat.archive_chat(instance_id, config, instance_token)
``` ```
#### Mark Chat as Unread #### Mark Chat as Unread
```python ```python
from evolutionapi.models.chat import UnreadChat from evolutionapi.models.chat import UnreadChat
@ -762,6 +802,7 @@ response = client.chat.unread_chat(instance_id, config, instance_token)
``` ```
#### Get Chat Profile Picture #### Get Chat Profile Picture
```python ```python
from evolutionapi.models.chat import ProfilePicture from evolutionapi.models.chat import ProfilePicture
@ -773,6 +814,7 @@ response = client.chat.get_chat_profile_picture(instance_id, config, instance_to
``` ```
#### Download Media Message #### Download Media Message
```python ```python
from evolutionapi.models.chat import MediaMessage from evolutionapi.models.chat import MediaMessage
@ -797,6 +839,7 @@ response = client.chat.download_media_message(instance_id, config, instance_toke
``` ```
#### Update Message #### Update Message
```python ```python
from evolutionapi.models.chat import UpdateMessage from evolutionapi.models.chat import UpdateMessage
@ -815,6 +858,7 @@ response = client.chat.update_message(instance_id, config, instance_token)
``` ```
#### Set Presence #### Set Presence
```python ```python
from evolutionapi.models.chat import Presence from evolutionapi.models.chat import Presence
@ -830,6 +874,7 @@ response = client.chat.set_presence(instance_id, config, instance_token)
### Calls ### Calls
#### Simulate Call #### Simulate Call
```python ```python
from evolutionapi.models.call import FakeCall from evolutionapi.models.call import FakeCall
@ -853,6 +898,7 @@ response = client.calls.fake_call(instance_id, config, instance_token)
### Labels ### Labels
#### Manage Labels #### Manage Labels
```python ```python
from evolutionapi.models.label import HandleLabel from evolutionapi.models.label import HandleLabel
@ -882,11 +928,13 @@ The Evolution API client supports WebSocket connection to receive real-time even
Before using WebSocket, you need to: Before using WebSocket, you need to:
1. Enable WebSocket in your Evolution API by setting the environment variable: 1. Enable WebSocket in your Evolution API by setting the environment variable:
```bash ```bash
WEBSOCKET_ENABLED=true WEBSOCKET_ENABLED=true
``` ```
2. Configure WebSocket events for your instance using the WebSocket service: 2. Configure WebSocket events for your instance using the WebSocket service:
```python ```python
from evolutionapi.models.websocket import WebSocketConfig from evolutionapi.models.websocket import WebSocketConfig
@ -934,19 +982,22 @@ print(f"Configured events: {websocket_info.events}")
There are two ways to create a WebSocket manager: There are two ways to create a WebSocket manager:
1. Using the client's helper method: 1. Using the client's helper method (recommended):
```python ```python
# Create WebSocket manager using the client # Create WebSocket manager using the client
websocket = client.create_websocket( websocket = client.create_websocket(
instance_id="test", instance_id="test",
api_token="your_api_token",
max_retries=5, # Maximum number of reconnection attempts max_retries=5, # Maximum number of reconnection attempts
retry_delay=1.0 # Initial delay between attempts in seconds retry_delay=1.0 # Initial delay between attempts in seconds
) )
``` ```
2. Creating the manager directly: 2. Creating the manager directly:
```python ```python
from evolutionapi.services.websocket import WebSocketManager from evolutionapi.client import EvolutionClient
import logging import logging
# Logging configuration # Logging configuration
@ -955,11 +1006,18 @@ logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
) )
# Create WebSocket manager directly # Initialize client
websocket = WebSocketManager( client = EvolutionClient(
base_url="http://localhost:8081", # Your Evolution API URL base_url="http://localhost:8081",
instance_id="test", # Your instance ID api_token="your-api-token"
api_token="your-api-token" # Your API token )
# Create WebSocket manager
websocket = client.create_websocket(
instance_id="test",
api_token="your_api_token",
max_retries=5,
retry_delay=1.0
) )
``` ```
@ -984,6 +1042,7 @@ websocket.on("qrcode.updated", handle_qrcode)
The available events are: The available events are:
#### Instance Events #### Instance Events
- `application.startup`: Triggered when the application starts - `application.startup`: Triggered when the application starts
- `instance.create`: Triggered when a new instance is created - `instance.create`: Triggered when a new instance is created
- `instance.delete`: Triggered when an instance is deleted - `instance.delete`: Triggered when an instance is deleted
@ -991,12 +1050,14 @@ The available events are:
- `logout.instance`: Triggered when an instance logs out - `logout.instance`: Triggered when an instance logs out
#### Connection and QR Code Events #### Connection and QR Code Events
- `qrcode.updated`: Triggered when the QR Code is updated - `qrcode.updated`: Triggered when the QR Code is updated
- `connection.update`: Triggered when connection status changes - `connection.update`: Triggered when connection status changes
- `status.instance`: Triggered when instance status changes - `status.instance`: Triggered when instance status changes
- `creds.update`: Triggered when credentials are updated - `creds.update`: Triggered when credentials are updated
#### Message Events #### Message Events
- `messages.set`: Triggered when messages are set - `messages.set`: Triggered when messages are set
- `messages.upsert`: Triggered when new messages are received - `messages.upsert`: Triggered when new messages are received
- `messages.edited`: Triggered when messages are edited - `messages.edited`: Triggered when messages are edited
@ -1006,32 +1067,39 @@ The available events are:
- `messaging-history.set`: Triggered when messaging history is set - `messaging-history.set`: Triggered when messaging history is set
#### Contact Events #### Contact Events
- `contacts.set`: Triggered when contacts are set - `contacts.set`: Triggered when contacts are set
- `contacts.upsert`: Triggered when new contacts are added - `contacts.upsert`: Triggered when new contacts are added
- `contacts.update`: Triggered when contacts are updated - `contacts.update`: Triggered when contacts are updated
#### Chat Events #### Chat Events
- `chats.set`: Triggered when chats are set - `chats.set`: Triggered when chats are set
- `chats.update`: Triggered when chats are updated - `chats.update`: Triggered when chats are updated
- `chats.upsert`: Triggered when new chats are added - `chats.upsert`: Triggered when new chats are added
- `chats.delete`: Triggered when chats are deleted - `chats.delete`: Triggered when chats are deleted
#### Group Events #### Group Events
- `groups.upsert`: Triggered when groups are created/updated - `groups.upsert`: Triggered when groups are created/updated
- `groups.update`: Triggered when groups are updated - `groups.update`: Triggered when groups are updated
- `group-participants.update`: Triggered when group participants are updated - `group-participants.update`: Triggered when group participants are updated
#### Presence Events #### Presence Events
- `presence.update`: Triggered when presence status is updated - `presence.update`: Triggered when presence status is updated
#### Call Events #### Call Events
- `call`: Triggered when there's a call - `call`: Triggered when there's a call
#### Typebot Events #### Typebot Events
- `typebot.start`: Triggered when a typebot starts - `typebot.start`: Triggered when a typebot starts
- `typebot.change-status`: Triggered when typebot status changes - `typebot.change-status`: Triggered when typebot status changes
#### Label Events #### Label Events
- `labels.edit`: Triggered when labels are edited - `labels.edit`: Triggered when labels are edited
- `labels.association`: Triggered when labels are associated/disassociated - `labels.association`: Triggered when labels are associated/disassociated
@ -1060,7 +1128,8 @@ websocket.on("presence.update", handle_presence)
### Complete Example ### Complete Example
```python ```python
from evolutionapi.services.websocket import WebSocketManager from evolutionapi.client import EvolutionClient
from evolutionapi.models.websocket import WebSocketConfig
import logging import logging
import time import time
@ -1081,13 +1150,33 @@ def handle_connection(data):
logger.info(f"Connection status: {data}") logger.info(f"Connection status: {data}")
def main(): def main():
# Initialize WebSocket # Initialize client
websocket = WebSocketManager( client = EvolutionClient(
base_url="http://localhost:8081", base_url="http://localhost:8081",
instance_id="test",
api_token="your-api-token" api_token="your-api-token"
) )
# Configure WebSocket
websocket_config = WebSocketConfig(
enabled=True,
events=[
"MESSAGES_UPSERT",
"QRCODE_UPDATED",
"CONNECTION_UPDATE"
]
)
# Set WebSocket configuration
client.websocket.set_websocket("instance_id", websocket_config, "instance_token")
# Create WebSocket manager
websocket = client.create_websocket(
instance_id="instance_id",
api_token="your_api_token",
max_retries=5,
retry_delay=1.0
)
# Register handlers # Register handlers
websocket.on("messages.upsert", handle_message) websocket.on("messages.upsert", handle_message)
websocket.on("qrcode.updated", handle_qrcode) websocket.on("qrcode.updated", handle_qrcode)
@ -1120,10 +1209,9 @@ if __name__ == "__main__":
The WebSocket Manager has automatic reconnection with exponential backoff: The WebSocket Manager has automatic reconnection with exponential backoff:
```python ```python
websocket = WebSocketManager( websocket = client.create_websocket(
base_url="http://localhost:8081",
instance_id="test", instance_id="test",
api_token="your-api-token", api_token="your_api_token",
max_retries=5, # Maximum number of reconnection attempts max_retries=5, # Maximum number of reconnection attempts
retry_delay=1.0 # Initial delay between attempts in seconds retry_delay=1.0 # Initial delay between attempts in seconds
) )
@ -1153,4 +1241,5 @@ The WebSocket Manager has robust error handling:
2. Implement handlers for all events you need to monitor 2. Implement handlers for all events you need to monitor
3. Use logging for debugging and monitoring 3. Use logging for debugging and monitoring
4. Consider implementing a heartbeat mechanism if needed 4. Consider implementing a heartbeat mechanism if needed
5. Keep your API token secure and don't expose it in logs 5. Keep your API token secure and don't expose it in logs
6. Keep your API token secure and don't expose it in logs