Python

As an outcome of this guide, you will send a simple WhatsApp template message to your handset with Infobip WhatsApp API.

What is a WhatsApp template message?

As opposed to regular messages that need to be user-initiated and can only happen within the 24-hour window, template messages can be sent and delivered at any time. Check out our Product Documentation for more details.


Prerequisites

Infobip account

• Working Python 3 environment

• a message template registered with WhatsApp. For testing purposes, use the Infobip WhatsApp-registered template you can activate on our WhatsApp API landing page.

Difficulty level

This guide assumes basic knowledge of Python and basic familiarity with APIs. You should also be familiar with the WhatsApp template registration process if you want to implement this scenario IRL.

Summary of the steps

• Install the Infobip API Python SDK.
• Import WhatsApp Channel to create an instance.
• Add credentials, phone number, and WhatsApp payload to your instance.
• Send a WhatsApp template message.
• Optionally, print out the response to track the progress of your message.

Install the Infobip API Python SDK

Use your terminal or command prompt to install the Infobip Python SDK.

pip install infobip-api-python-sdk

Create a WhatsApp Channel instance

Use the WhatsApp instance to add your credentials and access all its methods. Then, use the send_template_message method to add the WhatsApp payload.

Step 1. Import WhatsAppChannel into your environment.

from infobip_channels import WhatsAppChannel

Step 2. Create WhatsAppChannel and add your base_Url and api_key. Access your credentials from your Infobip account or from the Infobip API landing page once you’re logged in.

channel = WhatsAppChannel.from_auth_params({
    "base_url": "<your_base_url>",
    "api_key": "<your_api_key>"
})

Step 3. Add the WhatsApp payload to the send_template_message method.

Key points:

• use the international number formatting when adding a number to the to field. For example, for British numbers, the value will look like this: 44754322576.
• for free trial accounts, the number used as a recipient must be the same number registered when signing up with Infobip.
• we suggest you store the send_template_message method in a variable to print out the response. That way, you’ll be able to view the message status.

response = channel.send_template_message({
  "messages": [
    {
      "from": "447860099299",
      "to": "447415774432",
      "messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
      "content": {
        "templateName": "bus",
        "templateData": {
          "body": {
                    "text": "Bus from {{1}} for {{2}} is leaving at {{3}}.",
                    "placeholders": [
"Placeholder Value 1",
"Placeholder Value 2",
"Placeholder Value 3"
]
                }
                
        },
        "language": "en"
      }
    }
  ]
})

Add multiple recipients and multiple messages

Send multiple messages or send a message to multiple recipients using one request by declaring multiple messages objects.

Keep tabs on your message

Print out the response variable to see the status of your WhatsApp message. The response will inform you if the message has successfully left the Infobip platform. If you want to know whether it’s been delivered to the recipient, use the Delivery Report feature.

print(response)

Once you run the code, you should receive a WhatsApp message on your handset and see a 200 OK response printed in your dev environment.

{
  "to": "441134960001",
  "messageCount": 1,
  "messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
  "status": {
    "groupId": 1,
    "groupName": "PENDING",
    "id": 7,
    "name": "PENDING_ENROUTE",
    "description": "Message sent to next instance"
  }
}

For troubleshooting and analytics, Use the auto-generated messageId to view the message and its details.