pythontwiliowhatsappwhatsapp-cloud-api

How to access a WhatsApp template on Twilio using Python?


I have created multiple WhatsApp templates, but I am unsure of how to access them from the Twilio backend. Is there a way to access these templates?


Solution

  • You can send pre-approved template with the following code:

    # Download the helper library from https://www.twilio.com/docs/python/install
    import os
    from twilio.rest import Client
    import json
    
    
    # Find your Account SID and Auth Token at twilio.com/console
    # and set the environment variables. See http://twil.io/secure
    account_sid = os.environ['TWILIO_ACCOUNT_SID']
    auth_token = os.environ['TWILIO_AUTH_TOKEN']
    client = Client(account_sid, auth_token)
    
    message = client.messages.create(
                                  content_sid='HXXXXXXXXX',
                                  from_='MGXXXXXXXX',
                                  content_variables=json.dumps({
                                      '1': 'Name'
                                  }),
                                  to='whatsapp:+18551234567'
                              )
    
    print(message.sid)
    

    Go here for the full docs.