Sending SMS From Python. Fire up the Python interpreter in the terminal using the python command, or create a new file named sendsms.py. We need to grab our account credentials from the Twilio Console to connect our Python code to our Twilio account. Go to the Twilio Console and copy the Account SID and Authentication Token into your Python code. On the old Mac, open up the /Library/Messages folder. Copy that folder on to your method of choice (or drag it to AirDrop) On the new Mac, open that same location /Library/Messages. If it’s empty, drag the older Mac’s Messages folder to that location. If the new Mac’s Library/Messages folder is not empty. Copy its contents by right. Writing log messages to different locations allows you to route each message to the most appropriate place(s). TraceSource easily meets these needs. This logging library writes its messages to a listener object which then sends those messages to a predetermined location, such as the console, the Windows Event Log, or a file.
Short Message Service (SMS) text messages are ubiquitous for communicationall over the world. It is easy to send SMS text messages from aPython application using aweb application programming interface (API).Let's take a look at the tools we need to quickly add SMS capability to ourPython apps.
Tools We Need
This guide works with both Python 2 and 3, so make sure you have one ofthose two versions installed.
- Either Python 2 or 3
- pip andvirtualenv to handleapplication dependencies
- A free Twilio account to use theirSMS web API
- Open sourceTwilio Python helper library,version 6.0.0or later
If you need assistance getting pip and virtualenv installed, check out thefirst few steps of thehow to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTSguide that'll show how to install system packages for those tools.
Using a Web API
We're going to use a web API to make sending SMS easier and more reliable.Head to theTwilio website and sign up for a free trial accountawesome for more than just sending text messages!) then sign into yourexisting account.
The Twilio trial account allows you to send text messages to your ownvalidated phone number. When you want to send SMS to any phone number inyour country or other countries then you can upgrade your account to sendmessages for fractions of a cent.
After signing up, you will get a free phone number in your country. We canuse that phone number without any configuration to send outbound textmesssages. You can also receive text messages but that requires changingthe Request URL webhook in the phone number configuration screen - we'llcover that in a future blog post.
Message Tracer Library Free
Installing Our Dependency
Our code will use a helper library to make it easier to send text messagesfrom Python. We are going to install the helper library fromPyPI into a virtualenv. First we need tocreate the virtualenv. In your terminal use the following command to createa new virtualenv. If you need to install virtualenv take a look at thehow to set up Python 3, Flask and Green Unicorn on Ubuntu 16.04 LTSguide.
Message Tracer Library Login
Activate the virtualenv.
The command prompt will change after we properly activate the virtualenvto something like this:
Now install the Twilio Python helper library. We are using the 6.0.0or above library version, which is important because the syntax inthis post is backwards-incompatible with 5.x and previous Twilio helperlibrary versions.
The helper library is now installed and we can use it with the Python codewe create and execute.
Sending SMS From Python
Fire up the Python interpreter in the terminal using the python
command,or create a new file named send_sms.py
.
We need to grab our account credentials from the Twilio Console to connectour Python code to our Twilio account. Go to theTwilio Console and copy the Account SIDand Authentication Token into your Python code.
Enter the following code into the interpreter or into the new Python file.You can also copy and paste the code from theblog-code-examples Git repositoryin theFull Stack Python GitHub organization.
All the lines above that start with #
are comments. Once you enter thatcode into the interpreter or run the Python script usingpython send_sms.py
the SMS will be sent.
In a few seconds you should see a message appear on your phone. I'm oniOS so here's how the text message I received looked.
That's it! You can add this code to any Python code to send text messages.Just keep your Auth Token secret as it'll allow anyone that has it to useyour account to send and receive messages.
Questions? Contact me via Twitter@fullstackpythonor @mattmakai. I'm also on GitHub withthe username mattmakai.
See something wrong in this post? Forkthis page's source on GitHuband submit a pull request.