How to Send SMS through Nexmo in Python?


This is a follow-up article on sending/receiving SMS through Nexmo. In the previous article here, we discussed how to send/receive SMS through Nexmo in Python. In this article, we discuss about sending/receive SMS through Python.

In this article and the subsequent articles, I will be discussing specifically about Nexmo API. I will be discussing about sending / receiving SMS through Python. There are a number of SMS Gateways like Twilio, BulkSMS and so on available in the market. However, we found Nexmo to be very reliable as well as cheaper.
When I discuss about Nexmo, I believe, you already have an account with Nexmo. If you donot have an account already, you can create an account for free here. They provide you with $2 Account balance which you can use it to test all the features. Some of the features include :

  • Send / Receive SMS
  • Send Automated Voice Message
  • Voice Telephony


In this article, we will discuss about Sending SMS through Nexmo. For demonstration, I will be using Python. The complete source for sending SMS can be downloaded from here.

Example : Let’s say, you want to send an SMS to your client in United States reminding about the payment. So, you would send an SMS to your client saying,

Hey John, This is a reminder to make a payment for the month of August,2015 subscription. Please make a payment to avoid cancellation of your subscription
Thanks
Sapnaedu

Steps

Step 1: : Login to your Nexmo Dashboard here  and obtain the API key and a secret key associated with your account. You can also set the URL to notify the delivery of the message to your client.

Nexmo-API-Settings

Step 2 : Now, we have the following information to send the SMS to our client :

  • Nexmo API Key : 1234
  • Nexmo API Secret : 456789
  • Delivery Receipt URL : http://sapnaedu.in/sms_delivery.php
  • Client Mobile Number( Including Country Code ) : +1954123456789
  • Message :  Try to keep the length of the text to less than 160 characters. If the message is longer than 160 characters, Nexmo automatically send multiple text messages in parts each having 160 characters.

 

Step 3 : Build the argument list to be submitted to the base URL. https://rest.nexmo.com/sms/json?. This can be done using the following Python code

 

Step 4 : The next step is to submit the request to the Nexmo API. This can be done using the following Python code :

 

Step 5 : Handling the response

As we have submitted the request to https://rest.nexmo.com/sms/json which is a JSON end point, the response will be a JSON object.  A typical response would look like the following:

The response with status=0, indicate that the message was sent successfully.  Even though the SMS was sent successfully, it does not necessarily guarantee that SMS was delivered to the recipient.The delivery status is confirmed to the Delivery Receipt URL : http://sapnaedu.in/sms_delivery.php via GET method ( as configured in Step 2 ).

Step 6 : Check successful receipt of the SMS

Once the SMS was sent, Nexmo API notifies the Delivery receipt URL http://sapnaedu.in/sms_delivery.php via GET method. A typical delivery response would look similar to this :

You need to check the status field. If the SMS is delivered, the status field would be set to “accepted” or “delivered”.  If the SMS is not delivered for variety of reason which include :

  • The client phone is not reachable/switched off
  • Network Error
  • The client has barred all incoming SMS, etc

the status is set to “failure” or “failed”.

 

Download

The complete Python source can be downloaded from here. Please note, that the code is tested using Python 2.7 . If you are using Python 3.3, you might have to do some minor changes in the code.

I hope, you find this article very useful. If you have any doubts or have any comments concerning this tutorial, Please leave a comment or contact me. I would be glad to help.

Kiran Chandrashekhar

Hey, Thanks for dropping by. My name is Kiran Chandrashekhar. I am a full-time software freelancer. I love Maths and Mathematical Shortcuts. Numbers fascinate me. I will be posting articles on Mathematical Shortcuts, Software Tips, Programming Tips in this website. I love teaching students preparing for various competitive examinations. Read my complete story.

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected by WP Anti Spam