Python

As an outcome of this guide, you will be able to leverage Infobip WhatsApp API and share a location through a WhatsApp message delivered to your handset.


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.
• Share a location through a WhatsApp 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

The WhatsAppChannel instance allows you to add your credentials and access all its methods, one of them being the send_location_message method you will use to add the WhatsApp payload and ultumately share your location.

Step 1. Import the WhatsAppChannel.

from infobip_channels.whatsapp.channel import WhatsAppChannel

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

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

Step 3. Use the send_location_message method to add the WhatsApp location 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 phone number you’ve registered on signup.
• Optionally, use name and address to provide the location’s name and its address which will appear as part of the location message.

response = channel.send_text_message({
  "from": "447860099299",
  "to": "447415774332",
  "messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
  "content": {
    "latitude": 54.97614712105542,
    "longitude": -1.6216884576712745,
    "name": "St James' Park",
    "address": "Barrack Rd, Newcastle upon Tyne NE1 4ST"
  }
})

Add multiple recipients and multiple messages

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

Monitor the progress of your WhatsApp message

Print the response variable to see whether the message 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 handset 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.