Summary

Looking for another tool?

Share

Create a simple SMS delivery notification system (Python)

Reveal the hidden aspects of create a simple sms delivery notification system (python).

Logo of Callr

Customer Engagement doesn’t stop once an order is received. Some argue it might even be the beginning of it. Clients must be kept in the loop at all times and quite often, emails are not enough. Why not switch to their preferred channels instead? SMS has been boasting record-high open rates since its inception. So, here’s our goal for today’s cookbook: learn how to build a basic SMS notification system to be used for delivery notifications or anything else you might need to keep your clients informed about. To keep it simple, our goal here today is to build a status update notification system that will:

  • Given a database of the customers (we’ll create it from scratch for the example’s sake)
  • Go through this database entry by entry and send to each an SMS giving them an update and ETA on their order.

The whole thing can be done in four simple steps and should not take more than 10 minutes. We’ll detail the process step by step. If you are only looking for the code, you can find the full script here.

Sending SMS from the web

Communication Platforms as a Service (CPaaS) greatly ease the integration of telecom solutions in web and mobile apps. Indeed, CPaaS APIs such as CALLR take care of all the tedious details you need to juggle when initiating a phone call or sending an SMS.

  • You don’t have to integrate with every telecom carrier in the world. The API does that for you. You only need to say “CALLR, send this text to this number, with these options” and the API does the rest.

Do it your way

CALLR is completely language agnostic. Essentially, an API call is an HTTP request, SDKs are just here for convenience and ease of implementation. Yet, SDKs are more than welcomed as they allow you to initiate phone calls and send SMS very easily. You just parse to CALLR’s API your target number, message and options and the API does the rest. It will also return thehash ID of the call initiated / sent SMS to confirm its execution and track it. CALLR's SDK is available in 9 languages to ease its implementation:

For this tutorial, we’ll detail the steps to build an SMS notification system in Python.

1/ Install CALLR’s Python SDK

CALLR is available with pip, you can easily install it by entering the following command in your terminal:pip install callr

2/ Fetch credentials and login

Go to callr.com and create an account. You will receive your credentials shortly on the email address you specified during registration (or LinkedIn email).Once you have your credentials, you’ll be able to initialize an API instance and start using CALLR. Try it out with a simple SMS:# INITIALIZATIONimport callrapi = callr.Api("login", "password")testSMS = api.call('sms.send', 'SMS', “+336XXXXXXXX”, “Hey myself, just trying out CALLRxPython”, None)

3/ Create your sample list

To keep things simple, each customer is defined according to four data points:

  • Name: the customer name
  • Phone number: his phone number.
  • Order: item ordered
  • Dtime: expected time of delivery

NB: As you could have seen in the sample example at the previous step, numbers are written in the international format (E.164 with a + prefix). For a French mobile number, that is +33 6/7 XX.XX.XX.XX (without any spaces, slashes or dots) such as +33644630378.Hence, we define our list with Python as it follows. Two entries should be enough to try it out:# CREATING THE SAMPLE LISTrecipient_list = [ {'name' : 'Bob', 'phonenumber' : '+33628084544', 'order' : 'potatoes', 'dtime' : '4PM on 07/14/2017'}, {'name' : 'Michel', 'phonenumber' : '+33628084544', 'order' : 'bananas', 'dtime' : '8AM on 07/12/2017'}]

4/ Create a function to loop through our recipient_list and send a contextualized status update to each

The SendSMS() function does the bulk of the job, so let’s break it down:def SendSMS(recipient_list): for i, item in enumerate(recipient_list): recipient = recipient_list[i]["phonenumber"] message = "Hello %s, Your order of %s is on its way, expect delivery at %s" % (recipient_list[i]["name"], recipient_list[i]["order"], recipient_list[i]["dtime"]) optionsSMS = { 'nature' : 'ALERTING'} api.call('sms.send', 'SMS', recipient, message, optionsSMS)SendSMS(recipient_list)We define the recipient as the i “phonumber”entry of the list. We do the same for the message including three variables so everyone has a message adapted to his data. Then, we call the sms.send method to send the SMS, and since we’re notifying customers we can tag the traffic as ALERTING (SMSOption).Finally, we wrap the whole thing in a loop that will go through each entry of the list with the enumerate method. Then, we run our newly defined function, and that's all we need for our simple sms delivery notification system.

The limits of simplicity

We chose a simple use case with a straightforward scenario. It will not always be this easy. Moreover, the scenario we created is not failproof. What if your client signed up with a geographic number (01-05)? Some geographic numbers are still able to receive SMS: they are read to them with Text-to-Speech. However, only some carriers support this function so your message might literally get lost in translation. To prevent this issue, you could add a simple voice failover scenario:

  • If the phone number does no match 06.XX.XX.XX.XX or 07.XX.XX.XX.XX
  • Use the send/simple.broadcast_1 method to play your message with TTS (Python SDK link) instead of sms.send.

Besides, this script only runs locally and manually. It would be a good follow-up exercise to deploy it to a server (Heroku for instance) and have the script autorun. ;)We hope you enjoyed this little tutorial. Feel free to give feedback or ask questions if anything is not clear enough.[cta href="https://www.callr.com/p/python-sms-api/" txt="Looking for a smart and accessible Python SMS solution?" btn="Check out our API"]Here are some useful links to go further:The full code of this tutorialCALLR’s Python SDKCALLR’s websiteThe Knowledge Base

We own our network

Global coverage

Order numbers in just one click in over 220 countries, including premium, vanity or toll-numbers.

Encrypted and secure

We offer state-of-the-art data encryption with HTTPS, SIP TLS & SRTP.

Reliable network

With multiple data centers and servers around the world, we offer a robust service you can rely on.

Registered carrier

We operate our own network for better performance, localization and support.

Illustrations that represent Callr's global network