How to Send SMS Text Messages with Python

 Using Twilio

Installing Our Dependency

Our code will use a helper library to make it easier to send text messages from Python. We are going to install the helper library from PyPI into a virtualenv. First we need to create the virtualenv. In your terminal use the following command to create a new virtualenv.

virtualenv sendsms
 
Activate the virtualenv.

source sendsms/bin/activate
 
The command prompt will change after we properly activate the virtualenv to something like this:


Now install the Twilio Python helper library.

pip install twilio
 
The helper library is now installed and we can use it with the Python code we 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 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.


Enter the following code into the interpreter or into the new Python file.

# we import the Twilio client from the dependency we just installed
from twilio.rest import TwilioRestClient

# the following line needs your Twilio Account SID and Auth Token
client = TwilioRestClient("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")

# change the "from_" number to your Twilio number and the "to" number
# to the phone number you signed up for Twilio with, or upgrade your
# account to send SMS to any phone number
client.messages.create(to="+19732644152", from_="+12023358536", 
                       body="Hello from Python!") 
 
Get Your Twilio Number 

All the lines above that start with # are comments. Once you enter that code into the interpreter or run the Python script using python send_sms.py the SMS will be sent.
In a few seconds you should see a message appear on your phone. I'm on iOS 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 use your account to send and receive messages.

Using Way2SMS 


import urllib2
import cookielib
from getpass import getpass
import sys
import os
from stat import *

message = "Welcome To Python !"
number = ""

if __name__ == "__main__":   
    username = ""
    passwd = ""

    message = "+".join(message.split(' '))

 #logging into the sms site
    url ='http://site24.way2sms.com/Login1.action?'
    data = 'username='+username+'&password='+passwd+'&Submit=Sign+in'

 #For cookies

    cj= cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

 #Adding header details
    opener.addheaders=[('User-Agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120')]
    try:
        usock =opener.open(url, data)
    except IOError:
        print "error"
        #return()

    jession_id =str(cj).split('~')[1].split(' ')[0]
    send_sms_url = 'http://site24.way2sms.com/smstoss.action?'
    send_sms_data = 'ssaction=ss&Token='+jession_id+'&mobile='+number+'&message='+message+'&msgLen=136'
    opener.addheaders=[('Referer', 'http://site25.way2sms.com/sendSMS?Token='+jession_id)]
    try:
        sms_sent_page = opener.open(send_sms_url,send_sms_data)
        if sms_sent_page:
            print "Congo"
    except IOError:
        print "error"
        #return()

    print "success"
    #return ()


0 comments:

Copyright © 2013 SoftKul