Python

As an outcome of this guide, you will receive a Delivery Report of a sent SMS using Infobip SMS API and an online free webhook tool. Once configured, for every SMS sent, you will get a delivery notification sent in real-time to an endpoint you have specified for that SMS.


Prerequisites

Infobip account

• Working Python 3 environment

• a webhook that will listen to incoming events

Difficulty level

This guide assumes very basic knowledge of Python and basic familiarity with APIs. It is important, however, that you are familiar with the webhook technology and know how to use it for this project. 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.
• Send an SMS with the URL to your webhook specified in the payload.
• Receive a Delivery Report in real time on to the previously provided URL.

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. Once logged in, you’ll access a dashboard where you can start creating webhooks.

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 SMS Delivery Reports.

Once you click the Create hook button, you’ll be able to view and copy its URL that you will need to provide in your SMS payload, so the Infobip platform knows where to send its delivery reports. You’ll do that for each SMS you wish to receive delivery reports for.

Send an SMS

Send an SMS as you would normally do, but additionally use the notifyUrl field to include the URL for your inbound webhook. Check out our Send an SMS guide for more details, if need be.

from infobip_channels.sms.channel import SMSChannel

from credentials import api_key

channel = SMSChannel.from_auth_params(
    {
        "base_url": "jdzkyv.api.infobip.com",
        "api_key": api_key
    }
)

sms_response = channel.send_sms_message({
    "messages": [{
        "destinations": [{
            "to": "447415774432"
            }],
        "from": "InfobipSMS",
        "text": "Hi! I'm your first Infobip message. Have a lovely day!",
        "notifyUrl": "https://api.hookrelay.dev/hooks/jogep17l25r0hejdnkwzx830/72e082cb9edc40bc7952a939"
            }
        ]
    }
)

print(sms_response)

Key points:

• If for some reason your webhook was not available to receive traffic, the Infobip platform will make several attempts to communicate with it.

Receive a Delivery Report

Once your message has been sent, you should get an event on your webhook. Typically, this would be a message saying that your SMS has been delivered to a handset. See the Response Status and Error Codes documentation to view other possible message statuses or errors that will help you troubleshoot any potential issue you may encounter.

{
  "results": [
    {
      "price": {
        "pricePerMessage": 0.0,
        "currency": "EUR"
      },
      "status": {
        "id": 5,
        "groupId": 3,
        "groupName": "DELIVERED",
        "name": "DELIVERED_TO_HANDSET",
        "description": "Message delivered to handset"
      },
      "error": {
        "id": 0,
        "name": "NO_ERROR",
        "description": "No Error",
        "groupId": 0,
        "groupName": "OK",
        "permanent": false
      },
      "messageId": "35770713228903571979",
      "doneAt": "2022-07-13T10:12:22.906+0000",
      "smsCount": 1,
      "sentAt": "2022-07-13T10:12:12.295+0000",
      "to": "447415774432"
    }
  ]
}