Harsh Jadhav
Harsh Jadhav

Reputation: 1

How to send WhatsApp messages with dynamic content using Twilio API in Python?

I am attempting to send WhatsApp messages using the Twilio API in Python. Specifically, I want to use Twilio's support for dynamic content in WhatsApp template messages.

I've created a catalog template in Twilio and filled in the Catalog Title, Body, Subtitle, and Thumbnail Item ID. I also selected the "Specify during send time" option, so I can dynamically include item details in the message when sending it from my Python code.

However, I am struggling to understand how to properly include these dynamic variables in the API request. The Twilio documentation provides examples for basic WhatsApp messaging but doesn’t clearly explain how to pass custom values for dynamic fields in catalog templates.

Here is the Python code I have so far:

import os
from twilio.rest import Client
import json

# Hardcoded Account SID and Auth Token
account_sid = "AC29e4f65xxxxxxxxxxxxxxxxxxxxxx"
auth_token = "005d910f855463raggggggggregaer"
client = Client(account_sid, auth_token)

message = client.messages.create(
    content_sid="HXXXXXXXXX",  # Content SID of the catalog template
    to="whatsapp:+9193XXXXXXXXXXX",  # Recipient WhatsApp number
    from_="whatsapp:+1xxxxxxxxxx"   # Twilio-provided WhatsApp sender number
)

print(message.body)

These are some Products Examples that I want to add dynamically

{
  "products": [
    {"name": "Burrata", "id": "5t84ih3l2a"},
    {"name": "Grilled Salmon", "id": "g29jf8210c"},
    {"name": "Lobster Roll", "id": "p84jls9q5v"},
    {"name": "Roasted Pumpkin and Coconut Soup", "id": "k7n45x91uz"},
    {"name": "Fillet Steak", "id": "q01mz8l7xy"},
    {"name": "Steak and Lobster", "id": "f74xjp921v"},
    {"name": "T-Bone", "id": "z18uwj53mc"},
    {"name": "Farm Raised Chicken Breast", "id": "c23jv891yz"}
  ]
}

Questions: How do I specify the dynamic content (e.g., item IDs or other variables) in my Python code when sending the message? Is there a specific format for passing these dynamic variables in the client.messages.create method? Do I need to encode the dynamic content in a JSON format, or is there a specific parameter that needs to be included in the API call? Are there any examples or best practices for implementing dynamic content for catalog templates when using Twilio's Python SDK? Any help or code examples would be greatly appreciated! I’ve been through the official documentation but couldn’t find a clear guide on this. If there are any additional libraries or steps required, please let me know.

Upvotes: 0

Views: 157

Answers (1)

SamePop20
SamePop20

Reputation: 147

Just implemented few months ago using this

Here an example using node.js

await client.messages.create({
  contentSid,
  contentVariables: JSON.stringify(contentVariables),
  from: <messageServiceSid>,
  to: `whatsapp:${phone}`,
})

As you can see from has to be the message service sid and not the phone number.

Upvotes: 1

Related Questions