This guide walks you through the process of sending a WhatsApp message with media using Infobip WhatsApp API. As an outcome, you will send a WhatsApp message with an image to your handset. Some steps will additionally show you which parts of code you can substitute to use other media types, such as:
• document
• audio
• video
• sticker
Prerequisites
• 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 a correct WhatsApp payload to your instance.
• Send a WhatsApp message with a media attachment, such as image.
• Optionally, print out the response to view the outcome of your send message request.
Install the Infobip API Python SDK
Use a terminal or command prompt to install the Infobip Python SDK.
pip install infobip-api-python-sdk
Create a WhatsApp Channel instance
This section will show you, step by step, how to create a WhatsAppChannel
instance. You’ll then use it to add credentials and access all its methods, one of them being the method that allows you to send a WhatsApp message with an image.
Step 1. Import WhatsAppChannel
.
from infobip_channels.whatsapp.channel import WhatsAppChannel
Step 2. Create a WhatsAppChannel
instance 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_image_message
method to add the WhatsApp payload. Alternatively, here’s a list of methods you can use for different media types:
Media Type | Method |
---|---|
document | send_document_message |
audio | send_audio_message |
video | send_video_message |
sticker | send_sticker_message |
response = channel.send_image_message({
"from": "447860099299",
"to": "447415774432",
"messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
"content": {
"mediaUrl": "https://seekvectorlogo.com/wp-content/uploads/2019/06/infobip-vector-logo-small.png",
"caption": "Check out our logo!"
}
})
Key points about the payload:
• 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.
• Documentation for the mediaUrl
field lists supported file formats you can use.
• mediaUrl
is the only required attribute of the content
object, regardless of the media type. If you’re using a different media type than image, make sure you navigate to the relevant endpoint for details.
• Optional attributes of the content
object differ depending on the media type you wish to send:
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 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 you run the code, you should receive a WhatsApp message on your handset and see a 200 OK
response.
{
"to": "447415774432",
"messageCount": 1,
"messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
"status": {
"groupId": 1,
"groupName": "PENDING",
"id": 7,
"name": "PENDING_ENROUTE",
"description": "Message sent to next instance",
"action": null
}
}
For troubleshooting and analytics, Use the auto-generated messageId
to fetch the message and its details.