This tutorial is originally meant to be a guide to go along with a talk for PyLadies (but also for anyone else who finds it useful) on how to create a simple Twilio SMS application that sends you a notification for an event.
Here is what you will need:
pip install twilioor
To get a number:
and it will offer you a number. If you don’t like this one, you can click on “Search for a different number”. In any way, make sure it has SMS listed in the capabilities (should be the case pretty much anywhere in the world). You should be greeted with a message similar to this:
Congratulations, you how have a programmable number that has infinite use cases from notifying you when your train is late, telling you when there was movement detected by a camera, or sending your grandmother preset signs of affection that get sent out automatically at specific times so that she doesn’t feel lonely! Or make your dog send selfies to send them over MMS, your choice:
You will also need the ACCOUNT_SID and AUTH_TOKEN to send a SMS. Keep in mind that the AUTH_TOKEN is equivalent to a password, so keep it secure accordingly. In order to get it, click on the “Show API credentials link”:
or simply navigate to https://www.twilio.com/console/sms/getting-started/basics and click on the eye to see your token:
The account sid always starts with AC and it’s an equivalent to a user name. Write both of these down, you will need it later.
Ok, now we get to the fun part, coding. Sending a SMS is as simple as filling in the ACCOUNT_SID and AUTH_TOKEN variables with correct values and adjusting the message. Leave the quotes, replace just the text between these. Save it to a file, let’s say sms.py.
from twilio.rest import TwilioRestClient # put your own credentials here ACCOUNT_SID = 'replace with your account sid' AUTH_TOKEN = 'replace with your account token' client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) client.messages.create( to = 'replace with the number you want to send a sms to', from_ = 'replace with your Twilio number you just created', body = 'replace with the message', )
Let's try it out! Execute
python sms.py from a shell and make sure that you either are in the directory where the file is or give it a full path. If it was successful, it has no output, you just get a new prompt line and you should receive a message with the contents you entered, padded with a free trial account message, like this:
Congratulations, you have created your first Twilio SMS app! Now you can reuse this for any other application to send a notification for any sort of an event. Your imagination what to integrate it with is your playground. You could also execute the Python script from crontab periodically to check for any condition (let's say a Twitter post for a specific user or a hashtag, stock price change and so on), and if it's met, it will let you know by SMS.
Note that you are not limited to SMS, you could also send MMS, make a call or use any of the other products available. https://www.twilio.com/docs/quickstart/python/sms and https://www.twilio.com/docs/tutorials are good places to explore next with great tutorials.