Python

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


Prerequisites

Infobip account

• Working Python 3 environment

Difficulty level

This guide assumes basic knowledge of Python and basic familiarity with APIs.

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 text 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_text_message method to add the WhatsApp payload.

Step 1. Import the WhatsAppChannel.

from infobip_channels import WhatsAppChannel

Step 2. Create WhatsAppChannel and add your base_Url and api_key that you can access either from your Infobip account or from the Infobip API landing page once you log in.

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

Step 3. Use the send_text_message method to add the WhatsApp payload.

Key points:

• The to field must include a number in the international format, e.g. 447415774332.
• If using a free trial account, the phone number you use must be the same one you’ve registered on signup.
• It’s a good practice to have the send_text_message method in a variable so you can easily print out the response.

response = channel.send_text_message({
     "from": "<Infobip_WhatsApp_sender>",
      "to": "<Number_to_receive_WhatsApp_message>",
      "messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
      "content": {
        "text": "Hello, this is your first WhatsApp message."
      }
    })

Add multiple recipients and multiple messages

If you want to send multiple messages to multiple recipients in one request, use a template WhatsApp message.

Monitor the progress of your WhatsApp message

Print the response variable to see what’s happening with your WhatsApp message. The response will only tell you whether it has successfully left the Infobip platform. If you wish to know whether it’s been delivered to the recipient, you’ll need to set up a Delivery Report.

print(response)

Once executed, you should receive a WhatsApp message on your headset and see a 200 OK response.

{
  "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.