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