Python

As an outcome of this guide, you will be able to schedule an SMS and receive it within the specified time window on your handset using Infobip SMS API.


Prerequisites

Infobip account

• Working Python 3 environment

Difficulty level

This guide assumes very basic knowledge of Python and basic familiarity with APIs.

Summary of the steps

• Install the Infobip API Python SDK.
• Import the SMS Channel and create an SMS Channel instance.
• Add credentials, phone number, and SMS payload that includes scheduling details.
• Send an SMS and print the response to track its progress. The SMS will arrive at your handset within the time window specified in the SMS payload.

Install the Infobip API Python SDK

pip install infobip-api-python-sdk

Create an SMS Channel instance

Create an SMS instance to input your credentials and access its methods. In this case, we will use the send_sms_message method to add the SMS payload. Your payload will include all scheduling details.

Step 1. Import the SMSChannel.

from infobip_channels.sms.channel import SMSChannel

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

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

Step 3. Use the send_sms_message method to add the SMS payload that will additionally include scheduling details.

Key points about scheduling:

• Use the following format: yyyy-MM-dd'T'HH:mm:ss.SSSZ within the sendAt field to pass the specific date and time you want your message to be sent.
• Your message cannot be scheduled later than 180 days in advance.
• Add abulkId field to pass your custom bulk ID which you will then use to manage your scheduled messages. Otherwise, the Infobip platform will autogenerate it for you and return it in a response.

sms_response = channel.send_sms_message({
  "messages": [
    {
      "destinations": [
        {
          "to": "447415774432"
        },
      ],
      "from": "InfoSMS",
      "text": "This is a sample message",
      "sendAt": "2022-07-18T14:27:00.000+0000"
    }
  ]
})

Step 4. Print the response to monitor the progress of your SMS that will arrive at your handset around the time and date you have specified in the sendAt field. You’ll also be able to view your bulk ID.

print(sms_response)

Once you run the code, you should see a 200 OK response and receive an SMS at the specified date and time. The response contains an autogenerated bulkId that you can use to manage your scheduling, e.g. reschedule your message, view/update its status or cancel it completely. You can also use it for analytics and troubleshooting.

{
  "bulkId": "35823398891103573113",
  "messages": [{
      "messageId": "35841637745505282710",
      "status": {
        "description": "Message sent to next instance",
        "groupId": 1,
        "groupName": "PENDING",
        "id": 26,
        "name": "MESSAGE_ACCEPTED"
      },
      "to": "2250be2d4219-3af1-78856-aabe-1362af1edfd2"
    }]
}

Reschedule an SMS

To reschedule your SMS, use bulkId to identify which message you want to reschedule and pass it as a query parameter. The body should only contain the sendAt field with a new date and time you want to use. Note that, just like with scheduling, the new date cannot exceed 180 days into the future.

rescheduled_sms = channel.reschedule_sms_messages(
    {"bulkId": "35823398891103573113"},
    {
        "sendAt": "2022-08-25T16:00:00"
    })

print(rescheduled_sms)

If successful, you’ll get a 200 OK response that returns the new date and confirms the bulk ID.

{
  "bulkId": "35823398891103573113",
  "sendAt": "2022-08-25T16:00:00.000+0000"
}

View the status of your scheduled messages

To view the status of your scheduled message, you’ll again need to know your bulk ID and pass it in as a query parameter with the get_scheduled_sms_messages_status method.

sms_status = channel.get_scheduled_sms_messages_status(
    {"bulkId": "35823398891103573113"}
)

print(sms_status)

The 200 OK response will contain the confirmation of the bulk ID and the current status of your message.

{
  "bulkId": "35823398891103573113",
  "status": "PENDING"
}

Cancel a scheduled SMS or change its status

You can cancel a scheduled SMS or update its status by calling the update_scheduled_sms_messages_status method and passing in your (you guessed it!) bulk ID as a query parameter and the status you wish to update it within the request body.

canceled_sms = channel.update_scheduled_sms_messages_status(
    {"bulkId": "35823398891103573113"},
    {"status": "CANCELED"}
)

print(canceled_sms)

Your 200 OK response will confirm the bulk ID and the new status of the message. In the case of a CANCELED status, the sending of the message will also get canceled.

{
  "bulkId": "35823398891103573113",
  "status": "CANCELED"
}