Send free SMS from commandline

Disclaimer: My intention of writing this post and publishing the code that uses a proprietary service is not to provide a derivative work based on way2sms.com, but for educational purpose only. The code given below clearly illustrates how python like intuitive programming language can be used to create interesting interactive web based applications. Use the following code for educational purpose only.

Last few days I had negative balance on my phone and I was very lazy to go out for a recharge. I couldn’t send any sms due to underbalance. So I had to rely on Way2SMS.com for free sms. The web interface with lots of ads and stuff irritated me. I thought it would be great if I have some utility/command that can perform the sms sending task for me :D

So I wrote a python script for sending free sms from commandline. I am enjoying it.

Here it is. Have a try :)

Instructions: 1. Go to www.way2sms.com, Create an account 2. Create a text file ~/.smsconf and write following lines in it

username=your_mob_no_as_user
password=password_you_registered
  1. Copy the following code to sms_send.py
$ chmod a+x sms_send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python

#Author: Sarath Lakshman ( sarathlakshman [at] slynux.com )


import urllib2,urllib,re,os

opener=urllib2.build_opener(urllib2.HTTPCookieProcessor())

customer_id=""
first_time=True
configs={}

config_handle = file(os.path.expanduser('~/.smsconf'),"r+")
config_lines = config_handle.readlines()
if len(config_lines) > 2:
    first_time=False

for line in config_lines:
    splits = line.strip().split('=')
    configs[splits[0]]=splits[1]


customer_id_regex = re.compile("custfrom[0-9]*")
success_re = re.compile("Message has been submitted successfully")

print "Logging in"

login=urllib.urlencode({'username':configs["username"], 'password':configs["password"],'login':'Login'})

opener.open("http://wwwo.way2sms.com//auth.cl", login)

if first_time:

    print "First run: Pulling customer identity..."
    handle=opener.open("http://wwwo.way2sms.com//jsp/InstantSMS.jsp?val=0")
    data=handle.read()
    customer_id=customer_id_regex.findall(data)[0]
    config_handle.write("custid=%s" %customer_id)

    
else:
    customer_id=configs["custid"]

config_handle.close()

print
mobno=raw_input("Enter mobile no: ")
message=raw_input("Enter message: ")
print

print "Sending.."


send_msg=urllib.urlencode({'HiddenAction':'instantsms', 'Action':customer_id,'MobNo':mobno,'textArea':message})
handle=opener.open("http://wwwo.way2sms.com//FirstServletsms?custid=",send_msg)


if len(success_re.findall(handle.read())) == 1:
    print "Message has been submitted successfully"
else:
    print "Could not complete your request"
print 
  1. Try it out.
$ ./sms_send.py 
Logging in

Enter mobile no: xxxxxxxxxx
Enter message: cool

Sending..
Message has been submitted successfully

Looking forward to your comments :)

TODO: Here is your task. Write a GTK/Qt frontend for this.