Twilio Imessage



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

Application of the Week – Twilio SMS Extension for Zoho CRM. Posted on April 15, 2020 April 14, 2020. Send SMS messages using Twilio from an iMessage style. Server sends a Twilio SMS message to everyone in the group, using their phone number. Anyone in that group who replies vis SMS to that message, sends to the entire group. I want my listener out of the picture at this point. From here on, it's just a pure group text, phone to phone (s).

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.

Twilio Message Length

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.

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.

Activate the virtualenv.

The command prompt will change after we properly activate the virtualenvto something like this:

Computer laptops & desktops driver download. 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.

ImessageMessageTwilio imessage login

Twilio Imessage Login

Twilio Imessage

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.

Twilio Message

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. Arcam sound cards & media devices driver download.