Send WhatsApp Templated Messages Using Python

Overview

This guide shows how to send an WhatsApp text message to any phone number. Businesses send text messages to notify customers about recent information, send alerts, and provide delivery status updates, among other use cases.

You can start sending SMS messages either by using our PHLO visual workflow builder or our APIs. Follow the instructions in one of the tabs below.

Prerequisites

To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. If this is your first time using Plivo APIs, follow our instructions to set up a Python development environment.

Once you have a Plivo account, follow our WhatsApp guide to onboard your WhatsApp account, register a number against your WABA and have a template in approved state.

If you phone number is in connected state and template is in approved state, you can send your first message.

Create the send WhatsApp application

Create a file called send_whatsapp.py and paste into it this code.

import plivo
from plivo.utils.template import Template

client = plivo.RestClient('<auth_id>','<auth_token>')

template=Template(**{ 
            ""name"": ""sample_purchase_feedback"",
            ""language"": ""en_US"",
            ""components"": [
                {
                    ""type"": ""header"",
                    ""parameters"": [
                        {
                            ""type"": ""media"",
                            ""media"": ""https://plivo.com/s3/img1.jpg""
                        }
                    ]
                },
                {
                    ""type"": ""body"",
                    ""parameters"": [
                        {
                            ""type"": ""text"",
                            ""text"": ""ABC Water Purifier""
                        }
                    ]
                }
            ]
          }
       )
response = client.messages.create(   
        src=""+14151112221"",
        dst=""+14151112222"",
        type_=""whatsapp"",
        template=template,
        url=""https://foo.com/sms_status/""
    )
print(response)
#prints only the message_uuid
print(response.message_uuid)

Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).  src would be your phone number registered against your WhatsApp business account.  dst  would be the destination WhatsApp number that would receive the message. 

WhatsApp templates support 4 components:  headerbodyfooterbuttons. At the point of sending messages, the template object you see in the code acts as a way to pass the dynamic parameters.  header  can accomodate text or media (images, video, documents) content.  body  can accomodate text content.  footer  cannot have any dynamic variables. Plivo does not support sending dynamic parameters in button yet. 

Note: We recommend that you store your credentials in the auth_id and auth_token environment variables to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables. You can use os module(os.environ) to store environment variables and fetch them when initializing the client.

Test

Save the file and run it.

$ python send_whatsapp.py
Note: If you’re using a Plivo Trial account, you can make calls only to phone numbers that have been verified with Plivo. You can verify (sandbox) a number by going to the console’s Phone Numbers > Sandbox Numbers page.