Sunday, July 3, 2011

android scripting using python

Android has a beautiful scripting platform 'sl4a'. You can script using lot of languages perl, python, shell etc in it. It has a good API for system calls like sending sms, making calls etc.

I've written a small script in python to send a group sms with individualised content.



import android, os, re

droid = android.Android()
docdir = "/mnt/sdcard/document"




Android calls are in android module. other modules like sys, os, re are available.



message =droid.dialogGetInput('message', ' extras: 0 name,1 mail', None).result
subject =droid.dialogGetInput('subject', '', None).result


message windows can be created using API calls.



dirfile = os.listdir (docdir)
dirfiles =[]
for f in dirfile :
 if f[-4:]==".sms":
  dirfiles.append (f)
d=droid.dialogCreateAlert("select file")
droid.dialogSetSingleChoiceItems(dirfiles)
droid.dialogSetPositiveButtonText('ok')
droid.dialogSetNegativeButtonText('cancel')
droid.dialogShow()
r=droid.dialogGetResponse().result
if r['which']== "negative":
 os._exit (1)
result = droid.dialogGetSelectedItems().result[0]
selfile = dirfiles [result]

All files in a directory ending with .sms is presented and the user is asked to select one of the files.




file = open (docdir + os.sep+ selfile, "r")
lines = file.readlines ()
contacts = []
numbers =[]
extras =[]
mail =""
for line in lines:
 f = line.split (",")
 if len (f[0])>0 :
  numbers.append (f[0])
  contacts.append (f[1])
  extras.append (f[1:])
 if len (f[1])>0 :
  mail = mail + f[2].replace(" ","").replace ("\n","") + ","

The .sms file is a comma seperated file whose first field is the contact number, second contact name, third his email, and any optional field which can be referred as $n in the message.



d=droid.dialogCreateAlert("select contacts")
n = len (contacts)
droid.dialogSetMultiChoiceItems(contacts, range (n))
droid.dialogSetPositiveButtonText('ok')
droid.dialogSetNegativeButtonText('cancel')
droid.dialogShow()
r=droid.dialogGetResponse().result
if r['which']== "negative":
 os._exit (1)
send_contacts= droid.dialogGetSelectedItems().result

for s in send_contacts :
 m= message
 for d in range(len(extras [s])):
  m = m.replace ("$"+str(d), extras[s][d])
 m=re.sub('\$\d','',m)
 droid.smsSend(numbers [s], m)
 print contacts[s] , m
droid.sendEmail(mail, subject, message, None)

The user is presented  a list of contacts to select from, to whom to send the message. The script also sends an email with the same message (without personalisation)

2 comments:

  1. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
    Digital Mobile Marketing
    SMS API
    SMS Marketing

    ReplyDelete
  2. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Mobile Marketing Service
    Mobile Marketing Companies
    Texting API
    Sms API
    Sms marketing

    ReplyDelete