bitfroster
bitfroster

Reputation: 23

How to specify Twilio Whatsapp template from the python client?

I would like to use Twilio Whatsapp messages with custom templates. I'm trying to send messages using a Python client. We have different types of messages and we would like to manage them from Twilio backend. We created several templates, but I have no idea how to specify a template in the code.

In the official documentation there is no mention of this. I looked into the source code of the client, here is a code from class MessageList, that I use for message creation:

    def create(self, to, status_callback=values.unset, application_sid=values.unset,
           max_price=values.unset, provide_feedback=values.unset,
           attempt=values.unset, validity_period=values.unset,
           force_delivery=values.unset, content_retention=values.unset,
           address_retention=values.unset, smart_encoded=values.unset,
           persistent_action=values.unset, from_=values.unset,
           messaging_service_sid=values.unset, body=values.unset,
           media_url=values.unset):

There is nothing similar to the template name in function parameters.

Is it possible at all to specify the template programmatically?

Upvotes: 2

Views: 1200

Answers (2)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

When you want to send a message using a template, all you have to do is ensure that the body of the message you are sending matches the template. For example, if your templates is:

Your login code for {{1}} is {{2}}.

Then sending the message:

Your login code for Twilio is 12345.

matches that template and will send successfully. So you don't specify the template programmatically, just with your message content.

You can see more about sending template messages with the Twilio API for WhatsApp here.

Upvotes: 2

hyperledger_fabric
hyperledger_fabric

Reputation: 1

from twilio.rest import Client

client = Client() # this is the Twilio sandbox testing number

from_whatsapp_number = 'whatsapp:+14155238886'

to_whatsapp_number = 'whatsapp:+15005550006'
client.messages.create(body='Ahoy,world!', from_=from_whatsapp_number, to=to_whatsapp_number)

Upvotes: 0

Related Questions