How to send messages using Python

In this tutorial, we will learn to send message using Python. We can use this approach in building new Python applications.

Tools we will be needing :

  1. Python – Version 2 or 3.
  2. Virtual Environment  – To create isolated Python environments
  3. Twilio Trial Account.
  4. Twilio Python helper library.

Twilio API :

We will be using Twilio Python helper library to send messages over the phone. To use Twilio library we will need to create twilio account. Signup for a free account

Twilio   Try Twilio Free.png

Once you had signed up on twilio, you will receive verification code on your registered phone number, input this code on twilio verification page and your account is set to use twilio services.

In Trial Mode, Twilio can be only used to send messages to a validated phone number. You will have to upgrade your account in-order to send messages to any phone number.

Now login into twilio and grab twilio account test credentials by click over here.

#Note : Copy your TEST ACCOUNT SID and TEST AUTHTOKEN, we will be using this in python script.

Install Python Dependency

As mentioned earlier that we will be using twilio helper library for our Python application. The library can be install using pip command.

We will start the basic Python Project by first creating virtual environment. Virtual environment are used to isolate different Python environments.

virtualenv sendMsg

Now activate this environment using,

source sendMsg/bin/activate

As we are ready with our virtual environment, go ahead and install twilio library.

pip install twilio

Sending Message from Python Script

Create a new python file i.e sendmsg.py and enter the following code in the file.

# import the Twilio client from the dependency
from twilio.rest import Client

# put your twilio credentials here
account_sid = "{{your_account_sid}}" # get it from https://www.twilio.com/console/phone-numbers/runtime/test-credentials
auth_token = "{{your_authentication_code}}"

client = Client(account_sid, auth_token)

media_url = "https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg"

response = client.messages.create(
    to= "{{enter_your_twillio_account_phone_number}}", #https://www.twilio.com/console/phone-numbers/verified
    from_= "+15005550006", #+15005550006 will be used for trial account.
    body= "Hello, how you doing ?",
    media_url= media_url # if you need to attach multimedia to your message, else remove this parameter.
    )

Now run the python script using

python sendmsg.py

In a few seconds you should be able to see a message appear on your phone – note that messages can sometime take a little longer because we are using test credentials and it might in queued stage. Always check API response to know exact status.

That is everything needed to send messages to phone. Awesome results using few lines of Python code. This code can be added to any Python program to add message sending capabilities.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s