Saturday, August 22, 2009

Google Voice Automation

We've been using Google Voice since it was Grand Central. The one number for life really is a great convenience. I like having it ring all the phones I have when I get a call - so if my cell coverage is weak or I'm running out of minutes, I can just answer a landline. Realistically though, I only want it to ring all the phones that I can answer based on where I am. Recently, I found pygooglevoice, which exposes the Google Voice API to Python. This is exactly what I need - now I can write a script to choose which phones to forward calls to - based on where I am. No sense on forwarding calls there if nobody's there. Also, if my wife's at home but I'm at work, there's no need to bother her by having my calls forwarded home. With our Bluetracker software, the HA system can tell if we're at one of our homes and execute a Python script to forward to the appropriate phones.

A sample script follows, based heavily on the gvoice script included in pygooglevoice:

import sys
from atexit import register
from optparse import OptionParser
from googlevoice import Voice,util
from googlevoice.util import LoginError,input,print_

parser = OptionParser(usage='''gvoice [-e -p ] commands
Where commands are

home
work
home2
out''')
parser.add_option("-e", "--email", dest="email", default=None,
help="Google Voice Account Email")
parser.add_option("-p", "--password", dest='passwd', default=None,
help='Your account password (prompted if blank)')
options, args = parser.parse_args()

def login(email=options.email, passwd=options.passwd):
global voice
try:
voice.login(options.email,options.passwd)
except LoginError:
if input('Login failed. Retry?[Y/n] ').lower() in ('', 'y'):
login(None, None)
else:
sys.exit(0)

try:
action,args = args[0],args[1:]
except IndexError:
print "you need to give a command"
sys.exit(2)

voice = Voice()
login()

register(voice.logout)

if action == "home":
voice.phones[0].disable() # work
voice.phones[1].enable() # mobile
voice.phones[2].enable() # home
voice.phones[3].disable() # home 2
elif action == "work":
voice.phones[0].enable()
voice.phones[1].disable()
voice.phones[2].enable()
voice.phones[3].disable()
elif action == "home2":
voice.phones[0].disable()
voice.phones[1].disable()
voice.phones[2].enable()
voice.phones[3].enable()
elif action == "out":
voice.phones[0].disable()
voice.phones[1].disable()
voice.phones[2].enable()
voice.phones[3].disable()


You give it three options - your email address, your password and the command (in this case home, home2, work and out):

gvforward.py -e ABCdefGHI9876543210@gmail.com -p password work

No comments:

Post a Comment