You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/channel.md
+46-88Lines changed: 46 additions & 88 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,40 +6,26 @@ This document explains how to send messages to WhatsApp Channels using the Wasen
6
6
7
7
## Overview
8
8
9
-
Sending a message to a WhatsApp Channel utilizes the existing generic `client.send()` method from the Wasender Python SDK. The key differences compared to sending a message to a regular user or group are:
9
+
Sending a message to a WhatsApp Channel is straightforward with the Wasender Python SDK. You will typically use the `client.send_text()` helper method.
10
10
11
-
1.**Recipient (`to` field):** The `to` field in the message payload must be the unique **Channel ID** (also known as Channel JID). This typically looks like `12345678901234567890@newsletter`.
12
-
2.**Message Type Restriction:** Currently, the Wasender API (and thus the SDK) generally **only supports sending text messages** to channels. Attempting to send other message types (images, videos, documents, etc.) to channels via the standard `send()` method may not be supported or could result in an API error. Always refer to the latest official Wasender API documentation for channel messaging capabilities.
11
+
1.**Recipient (`to` field):** The `to` field in the `send_text()` method must be the unique **Channel ID** (also known as Channel JID). This typically looks like `12345678901234567890@newsletter`.
12
+
2.**Message Type Restriction:** Currently, the Wasender API (and thus the SDK) generally **only supports sending text messages** to channels using the standard helper methods. Attempting to send other message types might not be supported. Always refer to the latest official Wasender API documentation for channel messaging capabilities.
13
13
14
14
## Prerequisites
15
15
16
-
1.**Obtain a Channel ID:** You need the specific ID of the channel you want to send a message to. One way to obtain a Channel ID is by listening for webhook events (e.g., `message.upsert` or `message.created` if your provider supports it for channels), as this event data for messages originating from a channel will include the channel's JID.
17
-
2.**SDK Initialization:** Ensure the Wasender Python SDK is correctly initialized in your project. This involves:
16
+
1.**Obtain a Channel ID:** You need the specific ID of the channel you want to send a message to.
17
+
2.**SDK Initialization:** Ensure the Wasender Python SDK is correctly initialized.
18
18
* Installing the SDK: `pip install wasenderapi`
19
-
* Setting the environment variable `WASENDER_API_KEY`. This API key should correspond to the specific WhatsApp session you intend to use for sending channel messages.
20
-
* Creating an instance of `WasenderClient`.
21
-
(Refer to `README.md` or `docs/messages.md` for detailed SDK initialization examples.)
19
+
* Setting the environment variable `WASENDER_API_KEY`.
20
+
* Creating an instance of `WasenderAsyncClient` (or `WasenderSyncClient`).
22
21
23
22
## How to Send a Message to a Channel
24
23
25
-
You will use the `client.send()` method with a `TextPayload` Pydantic model. The Python SDK also provides a specific type alias `wasenderapi.models.channel.ChannelTextMessage` which is an alias for `TextPayload`, intended for conceptual clarity when working with channels.
26
-
27
-
### Python Model for Channel Messages
28
-
29
-
The relevant Pydantic model from `wasenderapi.models.channel` is:
30
-
31
-
```python
32
-
# From wasenderapi/models/channel.py
33
-
# (Conceptual - actual import would be from wasenderapi.models)
34
-
from ..models.message import TextPayload # Relative import path for illustration
35
-
36
-
ChannelTextMessage = TextPayload
37
-
```
38
-
This means you can directly use `TextPayload` from `wasenderapi.models` when constructing your message for a channel.
24
+
Use the `client.send_text()` method.
39
25
40
26
### Code Example
41
27
42
-
Here's how you can send a text message to a WhatsApp Channel in Python:
28
+
Here's how you can send a text message to a WhatsApp Channel in Python using the async client:
43
29
44
30
```python
45
31
# examples/send_channel_message.py
@@ -49,117 +35,89 @@ import logging
49
35
from datetime import datetime
50
36
from typing import Optional
51
37
52
-
from wasenderapi import WasenderClient
38
+
# Corrected imports
39
+
from wasenderapi import create_async_wasender, WasenderAsyncClient # For type hinting
53
40
from wasenderapi.errors import WasenderAPIError
54
41
from wasenderapi.models import (
55
-
TextPayload, # Equivalent to ChannelTextMessage
56
-
WasenderSendResult,
42
+
WasenderSendResult, # Result from send_text
57
43
RateLimitInfo
44
+
# RetryConfig could be imported here if configuring retries
58
45
)
59
46
60
47
# Configure basic logging
61
48
logging.basicConfig(level=logging.INFO)
62
49
logger = logging.getLogger(__name__)
63
50
64
51
# --- SDK Initialization (Minimal for this example) ---
65
-
api_key = os.getenv("WASENDER_API_KEY")# API Key for the session sending the message
52
+
api_key = os.getenv("WASENDER_API_KEY")
66
53
67
54
ifnot api_key:
68
-
logger.error("Error: WASENDER_API_KEY environment variable is not set.")
69
-
exit(1)
70
-
71
-
# For sending messages via an existing session, typically only the session's API key is needed.
72
-
client = WasenderClient(api_key=api_key)
55
+
logger.error("Error: WASENDER_API_KEY environment variable not set.")
56
+
api_key ="YOUR_API_KEY_PLACEHOLDER"# Use a placeholder for docs
73
57
74
-
# The personal_access_token is generally used for account-level session management (create, list, delete sessions),
75
-
# not for sending messages through an already established session.
# # client = WasenderClient(api_key=api_key, personal_access_token=personal_access_token) # If needed for specific advanced scenarios or different auth models
# await send_message_to_channel_example(another_channel_jid, "Another important update!")
148
-
149
110
if__name__=="__main__":
150
-
# Before running, ensure WASENDER_API_KEY is set in your environment.
151
-
# Also, replace target_channel_jid in main() with a valid Channel ID.
152
111
logger.info("Starting channel message example. Ensure JID and API Key are set.")
153
112
asyncio.run(main())
154
113
155
114
```
156
115
157
116
### Key Points from the Example:
158
117
159
-
-**`to`**: Set to the `target_channel_jid`.
160
-
-**`TextPayload`**: Used to construct the message. The `message_type` attribute within `TextPayload` defaults to `"text"`, which is what channels typically require. You can explicitly set `message_type="text"` for clarity if desired.
161
-
-**`text`**: Contains the content of your message.
162
-
- The example includes minimal SDK initialization, error handling, and rate limit logging.
118
+
-**`create_async_wasender`**: Used to initialize the client.
119
+
-**`client.send_text(to=channel_jid, text_body=message_text)`**: The recommended way to send text to a channel.
120
+
- The example includes minimal SDK initialization and basic error logging.
0 commit comments