/ tuesdaytooling

Tooling Tuesday - Nexmo API and Python

This time we take a look at an API that enables us to send and receive SMS and calls via many different languages.

If you like what you read...

Would you like to buy me a cup of coffee? It helps me to pay for hosting this blog, and to buy stuff to hack from Poundshops / Dollar Stores which are used in free projects on this blog. Thanks!

So what is it?

Nexmo is a platform which offers telephony and SMS (text messages) for customer service businesses. Ever had a text message about your dental appointment, car MOT, or a reminder to collect your item from a store? Chances are that Nexmo provided that service to the business that served you.

So why should I be interested with it?

I get it, customer service texts are handy, but not that interesting. But we can use Nexmo to provide alerts for things in our life, and with a Python API we can mash our own cool projects together! Perhaps a high altitude balloon that sends you an SMS with its GPS position? An alarm system which alerts you when a door is left open? All possible with very little code!

So how do I use it?

Sign up for a free account and you shall receive free credit which can be used to send SMS messages. Note that you will not be able to receive a message, only send.

To install the Nexmo Python client via pip, open a terminal and type.

Windows

pip3.exe install nexmo

Linux & Mac

sudo pip3 install nexmo

To send a message with Python I obtained my API key and API secret via the Nexmo dashboard, and then I modified the provided code to make my own Python app to send an SMS.

#!/usr/bin/env python3
import nexmo, api

First I told the code where to find the Python interpreter. Then I imported the Nexmo library and an api library (api.py stored in the same directory as this code) that I created to store my api.key and api.secret.

client = nexmo.Client(key=api.key, secret=api.secret)

I then created a connection to my Nexmo account and used my api key and secret to tell Nexmo who I was.

who = input("Who is the message from?: ")
number = input("Receiving number?: ")
msg = input("What is the message?: ")

Capturing the answer to three questions I get the details of who the message is from, yes you can send the message as anyone, then I enter the number which I want to text (use the full international dialing code for this). Lastly I then capture the message that I wish to send.

Don't be an idiot and abuse the ability to send a message as anyone, it isn't funny for those that receive the message, and you can still be traced!

responseData = client.send_message(
    {
        "from": who,
        "to": number,
        "text": msg,
    }
)

I then send the message and await a response from the Nexmo servers.

if responseData["messages"][0]["status"] == "0":
    print("Message sent successfully.")
else:
    print(f"Message failed with error: {responseData['messages'][0]['error-text']}")

An if...else conditional test then reports on the success of sending the message.

I save the code and then run it.

python3 send-sms.py

Then give it the details (name, number and message) and it will send the SMS!

If you would like to run the code without typing in python3 send-sms.py then we need to first ensure that we have #!/usr/bin/env python3 at the start of the code. Then we need to make the file executable by typing chmod +x send-sms.py. We can then run the code by typing ./send-sms.py.

You will have noticed that with your free credit, a message is added to the end of your SMS. If you buy credit then this message disappears.
Like this

Anything else?

Yeah! As well as Python I enjoy tinkering with node-RED and after watching a talk at Barcamp London last May where Sam Machin showed how he could quickly prototype Nexmo flows to create elaborate interfaces over phone and SMS. Fancy controlling Neopixels by texting your Nexmo number with an RGB value, or calling your Pi and telling it the values over DTMF? Well yes you can!

So I wondered if I could make a quick proof of concept in node-RED and so I made a button, a real physical button on a breadboard, that when pressed, would send an SMS to my mobile. And it worked! With a little help from Sam.

I learnt about mustache templates in node-RED, enabling me to pass through the msg.payloaddata to the debug console. Handy!

So where can I learn more?

Screenshot-from-2019-10-28-18-38-14
Head over to the Nexmo Developer Tutorials to get a flavour of what it can do!