As an outcome of this guide, you will get an interactive WhatsApp message delivered to your handset using Infobip WhatsApp API and an online free webhook tool that will catch the reply sent from the same handset.
Scenario overview
The WhatsApp sender sends a message with a product image and a description, which are loaded from their existing Meta catalog. The recipient can then click on the image and choose between two options, either message the sender about the product or add the product to the cart and send it to the business. The business determines the next steps, whether it’s a delivery or payment integration, which is out of scope for this guide. The end user’s reply will be intercepted by a webhook so that the WhatsApp sender can view the reply and react accordingly.
Prerequisites
• Working Python 3 environment
• a webhook that will intercept a user response
• WhatsApp Business Account. For testing purposes, use the Infobip test sender.
• Facebook Business Account. For testing purposes, use the Infobip test catalog and product retailer ID provided in a code sample below.
Difficulty level
This guide assumes basic knowledge of Python and basic familiarity with APIs. It is important, however, that you are familiar with the webhook technology if you wish to catch a user response. For ease of replicating the scenario, we have used a free online webhook tool, HookRelay. You may replace HookRelay with your own hook server if you need to process your events programmatically.
Summary of the steps
• Set up a webhook and add its URL to the WhatsApp sender number.
• Install the Infobip API Python SDK.
• Import WhatsApp Channel
to create an instance.
• Add credentials, phone number, and WhatsApp payload to your instance.
• Send an interactive product WhatsApp message.
• Use a webhook to catch a user response.
Set up a webhook
For the purpose of this guide, we will use a simple online webhook tool, HookRelay. This section will show you how to create an account and configure the webhook with HookRelay.
Step 1. Sign in with git credentials
Sign in to HookRelay with your GitHub or GitLab credentials.
Step 2. Create a webhook
To create an inbound webhook, from your dashboard, click the New Hook button on the top right. That will take you to the configuration screen where all you need to provide is a descriptive name for your webhook, e.g. Infobip WhatsApp List Response.
Once you click the Create hook button, you’ll be able to view and copy its URL that you will need to add to your WhatsApp sender.
Add a webhook URL to a WhatsApp sender
Head back to your Infobip account and access your WhatsApp sender configuration page (Channels and Numbers > WhatsApp).
- Select Senders and click the kebab (three-dot) menu on the WhatsApp sender’s tile to select Edit configuration.
- Enable the Forwarding to URL toggle to expand more options.
- Add the URL of the webhook to the URL field.
- Click Save.
That’s about it for webhook configuration. Let’s now move to creating a WhatsApp message using Infobip Python SDK.
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, such as the send_interactive_product_message
method to which we’re going to add the WhatsApp interactive product payload.
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_interactive_product_message
method to add the WhatsApp interactive product 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.
• If you just want to test out the solution, use the catalogId
and productRetailerId
provided in the code sample below.
product_message = channel.send_interactive_product_message({
"from": "447860099299",
"to": "447415774332",
"messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
"content": {
"action": {
"catalogId": "1634406623431555",
"productRetailerId": "tennis-12366"
}
}
})
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(list_message)
Once you run the code, 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.
Catch the answer
Once the user replies, the Infobip platform will send you details on the URL you have previously specified.
{
"results": [
{
"from": "447415774332",
"to": "447860099299",
"integrationType": "WHATSAPP",
"receivedAt": "2022-09-05T08:27:43.211+0000",
"messageId": "ABEGRHQVd0QyAhCBxs0yCyJSa8s4pXro5xeI",
"pairedMessageId": null,
"callbackData": null,
"message": {
"catalogId": "1634406623431551",
"productItems": [
{
"currency": "USD",
"itemPrice": 2,
"productRetailerId": "tennis-12345",
"quantity": 1
}
],
"type": "ORDER"
},
"contact": {
"name": "Joanna Suau"
},
"price": {
"pricePerMessage": 0.0,
"currency": "EUR"
}
}
],
"messageCount": 1,
"pendingMessageCount": 0
}