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