-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwhatsapp.py
More file actions
183 lines (155 loc) · 5.29 KB
/
whatsapp.py
File metadata and controls
183 lines (155 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
WhatsApp Examples — All Message Types
Demonstrates every WhatsApp message type supported by the SDK:
text, media, template, interactive buttons, list, CTA URL, and location.
Usage:
1. pip install plivo_agentstack
2. Set PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN env vars
3. python whatsapp.py [text | media | template | buttons | list | cta | location]
"""
import asyncio
import sys
from plivo_agentstack import AsyncClient
from plivo_agentstack.messaging import InteractiveMessage, Location, Template
SRC = "+14155551234" # your WhatsApp Business number
DST = "+14155559876" # recipient
# --- Text message ---
async def send_text():
async with AsyncClient() as client:
response = await client.messages.create(
src=SRC,
dst=DST,
type_="whatsapp",
text="Hello from the Plivo Agent SDK!",
)
print("Text sent:", response["message_uuid"])
# --- Media message (image with caption) ---
async def send_media():
async with AsyncClient() as client:
response = await client.messages.create(
src=SRC,
dst=DST,
type_="whatsapp",
text="Here is your receipt.",
media_urls=["https://media.plivo.com/demo/image-sample.jpg"],
)
print("Media sent:", response["message_uuid"])
# --- Template message (with body params, currency, datetime, button) ---
async def send_template():
tpl = (
Template("order_confirmation", language="en")
.add_header_media("https://media.plivo.com/demo/banner.jpg")
.add_body_param("Alice")
.add_body_param("ORD-42")
.add_body_currency("$12.99", "USD", 12990)
.add_body_datetime("2026-03-07T10:30:00Z")
.add_button_param("url", 0, "https://example.com/track/ORD-42")
.build()
)
async with AsyncClient() as client:
response = await client.messages.create(
src=SRC,
dst=DST,
type_="whatsapp",
template=tpl,
)
print("Template sent:", response["message_uuid"])
# --- Interactive: quick reply buttons ---
async def send_buttons():
interactive = InteractiveMessage.button(
body_text="How would you rate your experience?",
buttons=[
{"id": "great", "title": "Great"},
{"id": "okay", "title": "Okay"},
{"id": "poor", "title": "Poor"},
],
header={"type": "text", "text": "Feedback"},
footer_text="Powered by Plivo",
)
async with AsyncClient() as client:
response = await client.messages.create(
src=SRC,
dst=DST,
type_="whatsapp",
interactive=interactive,
)
print("Buttons sent:", response["message_uuid"])
# --- Interactive: list message ---
async def send_list():
interactive = InteractiveMessage.list(
body_text="Browse our menu and pick your favorite.",
button_text="View Menu",
sections=[
{
"title": "Pizza",
"rows": [
{"id": "margherita", "title": "Margherita", "description": "$10"},
{"id": "pepperoni", "title": "Pepperoni", "description": "$12"},
],
},
{
"title": "Sides",
"rows": [
{"id": "fries", "title": "Fries", "description": "$4"},
{"id": "salad", "title": "Garden Salad", "description": "$6"},
],
},
],
header_text="Mario's Pizza",
footer_text="Prices include tax",
)
async with AsyncClient() as client:
response = await client.messages.create(
src=SRC,
dst=DST,
type_="whatsapp",
interactive=interactive,
)
print("List sent:", response["message_uuid"])
# --- Interactive: CTA URL ---
async def send_cta():
interactive = InteractiveMessage.cta_url(
body_text="Track your order in real time.",
button_title="Track Order",
url="https://example.com/track/ORD-42",
footer_text="Powered by Plivo",
)
async with AsyncClient() as client:
response = await client.messages.create(
src=SRC,
dst=DST,
type_="whatsapp",
interactive=interactive,
)
print("CTA sent:", response["message_uuid"])
# --- Location message ---
async def send_location():
location = Location.build(
37.7749,
-122.4194,
name="Plivo HQ",
address="201 Spear St, San Francisco, CA 94105",
)
async with AsyncClient() as client:
response = await client.messages.create(
src=SRC,
dst=DST,
type_="whatsapp",
location=location,
)
print("Location sent:", response["message_uuid"])
if __name__ == "__main__":
examples = {
"text": send_text,
"media": send_media,
"template": send_template,
"buttons": send_buttons,
"list": send_list,
"cta": send_cta,
"location": send_location,
}
choice = sys.argv[1] if len(sys.argv) > 1 else "text"
if choice not in examples:
print(f"Usage: python whatsapp.py [{' | '.join(examples)}]")
sys.exit(1)
asyncio.run(examples[choice]())