CTRLK

Send an interactive list with WhatsApp API

|

View as Markdown

As an outcome of this guide, you will be able to leverage Infobip WhatsApp API and send an interactive WhatsApp message delivered to your handset.

Scenario overview [#scenario-overview]

The WhatsApp sender sends a message with an image and a list of options a user can select. Once the user clicks on an option, a reply is sent back to the WhatsApp sender. That, in turn, can trigger an appropriate series of actions and responses, but this is out of scope for this guide. We will, however, discuss briefly how to intercept an incoming message with a webhook to see a user's reply.


Prerequisites [#prerequisites]

  • Infobip account. If you do not have one, create a free trial account.
  • Infobip API key with the whatsapp:message:send scope. Find out more about scopes in API Authorization.
  • Your favorite HTTP client. In this example, we use cURL. You can also choose an official Infobip SDK in your preferred programming language.
  • Sender. If in a free trial period, you can use Infobip test sender, 447860099299 and activate it. If you want to configure a custom sender, request a new number or an alphanumeric sender through your Infobip account or use the Infobip Numbers API.
  • Destination - a phone number to which the message will be sent. In a free trial period, you can only send messages to a verified phone number.

Limitations [#limitations]

With WhatsApp, you can only send a message to your customer if they have contacted you first. This opens a 24-hour window for two-way communication. Otherwise, use a template message.

Difficulty level [#difficulty-level]

This guide assumes 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 [#steps-summary]

  • Buy or configure an existing WhatsApp number capable of receiving incoming traffic.
  • Set up a webhook.
  • Add webhook details to the number you've purchased or configured.
  • Send a WhatsApp message with an interactive list. The webhook will catch the response.

Buy a number [#buy-number]

To buy a number, you'll need to log into your Infobip account.

Note

Skip this section, if using the Infobip test sender or an existing number.

  1. Expand the Channels and Numbers icon on the left-hand side menu, and select Channels.
  2. Select WhatsApp and click Register Sender.
  3. From the Register a WhatsApp sender screen, select the option you wish to choose for sender registration.
  4. Set up your number preferences, like a country, number type, etc.

You're all set for now. Once a webhook is configured, you'll come back to the number config to add its URL.

Set up a webhook [#set-up-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-with-git]

Sign into HookRelay with your GitHub or GitLab credentials.

Step 2. Create a webhook [#create-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 providing 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 webhook URL to a WhatsApp sender [#add-webhook-to-sender]

Head back to your Infobip account and access your WhatsApp sender configuration page (Channels and Numbers > WhatsApp).

  1. Select Senders and click the kebab (three-dot) menu on the WhatsApp sender's tile to select Edit configuration.
  2. Under Keywords add a new one or edit the existing default one.
  3. Under the Forwarding action section, select Forward to HTTP and add the webhook's URL to the URL field.
  4. Click Save.

That's about it for the webhook configuration. Let's now move to creating a WhatsApp message with an interactive list.

Send a WhatsApp message

We are going to send a message with an interactive list consisting of 4 options. We'll use the Send WhatsApp interactive list message endpoint.

To make it easier to visualize, the image below indicates which parts of the WhatsApp interactive list are driven from which fields in our WhatsApp API.

WhatsApp message with an interactive list with header and footer WhatsApp message with an interactive list body

Key points:

  • The to field must include a number in the international format, e.g. 447415074332.
  • If using a free trial account, the phone number you use must be the same phone number you've registered on signup.
curl
1curl -L 'https://{baseUrl}/whatsapp/1/message/interactive/list' \
2-H 'Authorization: {authorization}' \
3-H 'Content-Type: application/json' \
4-H 'Accept: application/json' \
5-d '{
6 "from": "447860099299",
7 "to": "447415774332",
8 "content": {
9 "body": {
10 "text": "Dear Developer, select a ticket for our annual Shift conference. Tickets include access to sessions, the event app, and talk recordings."
11 },
12 "action": {
13 "title": "See Tickets",
14 "sections": [
15 {
16 "title": "Choose one ticket.",
17 "rows": [
18 {
19 "id": "1",
20 "title": "Regular Student",
21 "description": "32€ + VAT"
22 },
23 {
24 "id": "2",
25 "title": "Regular Standard",
26 "description": "192€ + VAT"
27 },
28 {
29 "id": "3",
30 "title": "Regular All Access",
31 "description": "216€ + VAT"
32 },
33 {
34 "id": "4",
35 "title": "Regular Virtual",
36 "description": "56€ + VAT"
37 }
38 ]
39 }
40 ]
41 },
42 "header": {
43 "type": "TEXT",
44 "text": "Get Your Tickets!"
45 },
46 "footer": {
47 "text": "Join us at Shift!"
48 }
49 }
50}'

Add multiple recipients and multiple messages

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

Track message delivery

Once you send the request, you should receive a WhatsApp message on the handset you've specified in the to field and see a 200 OK response.

json
1{
2 "to": "441134960001",
3 "messageCount": 1,
4 "messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
5 "status": {
6 "groupId": 1,
7 "groupName": "PENDING",
8 "id": 7,
9 "name": "PENDING_ENROUTE",
10 "description": "Message sent to next instance"
11 }
12}

The response informs you that the message has successfully been queued within the Infobip platform and will be then sent to Meta depending on the throughput.

If you want to know whether it's been delivered to the recipient, use the Delivery Report feature.

Troubleshoot and analyze your message

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

Catch the answer

Once a user selects an option from the list, your webhook that's listening to that event will catch the reply. The message object will tell you which option has been selected and sent as an answer.

json
1{
2 "results": [
3 {
4 "from": "447415774332",
5 "to": "447860099299",
6 "integrationType": "WHATSAPP",
7 "receivedAt": "2022-08-18T09:30:52.516+0000",
8 "messageId": "ABEGRHQVd0QyAhCEOHQDx2_nQGlWh5eTJdht",
9 "pairedMessageId": "e5ee7f2a-5d43-46e1-9e9c-08c83e1ae935",
10 "callbackData": null,
11 "message": {
12 "id": "2",
13 "title": "Regular Standard",
14 "description": "192€ + VAT",
15 "context": {
16 "from": "447860099299",
17 "id": "e5ee7f2a-5d43-46e1-9e9c-08c83e1ae935"
18 },
19 "type": "INTERACTIVE_LIST_REPLY"
20 },
21 "contact": {
22 "name": "Joanna Suau"
23 },
24 "price": {
25 "pricePerMessage": 0.0,
26 "currency": "EUR"
27 }
28 }
29 ],
30 "messageCount": 1,
31 "pendingMessageCount": 0
32}