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.
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
params = {}
params['api_key'] = '1234'
params['api_secret'] = '456789'
params['from'] = 'SAPNAEDU'
params['to'] = '+1954123456789'
params['status-report-req'] = '1' #Send status report to the Delivery Recipt URL ( http://sapnaedu.in/sms_delivery.php )
params['client-ref'] = '123456|Client-rambo456' #Send status request
params['text'] = "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 \nThanks \n Sapnaedu"
#print params
param1 = urllib.urlencode(params)
base_url = "https://rest.nexmo.com/sms/json?"
url = base_url + param1
|
Step 4 : The next step is to submit the request to the Nexmo API. This can be done using the following Python code :
1
2
3
|
data_result = get_url(url) # Definition of get_url can be found in the source code
data_result = data_result.decode("utf-8")
result = json.loads(data_result)
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$result =
{
"message-count":"1",
"messages":[
{
"status":"0",
"message-id":"32242322",
"to":"1954123456789'",
"remaining-balance":"11.15",
"message-price":"0.02",
"network":"4408"
}
]
}
|
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 :
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$delivery_response =
{
"msisdn" : "1954123456789'",
"to" : "SAPNAEDU",
"network-code" : "310260",
"messageId" : "020000006FC27627",
"price" : "0.00480000",
"status" : "accepted",
"scts" : "1507270315",
"err-code" : "1",
"client-ref" : "123456|Client-rambo456",
"message-timestamp" : "2015-07-27 03:"15:"02"
}
|
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.