#!/usr/bin/env python import simplejson import urllib import urllib2 import pprint # debug pp = pprint.PrettyPrinter(indent=4) def getAccountsForBank(bankId): # send request to json-rpc server to get accounts. # post code taken from http://www.voidspace.org.uk/python/articles/urllib2.shtml#fetching-urls url = 'http://macbook.lan.nurey.com:8080/Finance/Bank/JSONFactory' cardNumber = 'XXX' password = 'XXX' cachePath = '/Users/ilia/.byob/cache' values = { 'params': [ { 'bank_id': bankId, 'card_number': cardNumber, 'password': password, 'cache_path': cachePath } ], 'method': 'accounts', 'id': 'jsonRpcPyClient' } valuesJson = simplejson.dumps(values) data = valuesJson #data = urllib.urlencode(valuesJson) #pp.pprint(data) #debug req = urllib2.Request(url, data) response = urllib2.urlopen(req) # get the response object responseJson = response.read() #pp.pprint(response_json) #debug responseDict = simplejson.loads(responseJson) return responseDict['result'] def ReplyTest(message): if message['Number'] == '999': # No reply to this number return None return 'Reply to %s' % message['Text'] # Reply function, first element is matching string, second can be: # - string = fixed string will be sent as reply # - function = function will be called with SMS data and it's result will be sent # - None = no reply replies = [ ('1/1 www:', 'This is test'), ('1/2 www:', ReplyTest), ('2/2 www:', None), ] def Callback(sm, type, data): if verbose: print 'Received incoming event type %s, data:' % type if type != 'SMS': print 'Unsupported event!' if not data.has_key('Number'): data = sm.GetSMS(data['Folder'], data['Location'])[0] if verbose: print data accounts = [] for bankId in ['pc', 'cibc-nurey', 'cibc-ilia']: bankAccounts = getAccountsForBank(bankId) accounts.extend(bankAccounts) for reply in replies: if reply[0] == data['Text'][:len(reply[0])]: if callable(reply[1]): response = reply[1](data) else: response = reply[1] if response is not None: message = {'Text': response, 'SMSC': {'Location': 1}, 'Number': data['Number']} if verbose: print message sm.SendSMS(message) else: if verbose: print 'No reply!' break sm = gammu.StateMachine() sm.ReadConfig() sm.Init() sm.SetIncomingCallback(Callback) try: sm.SetIncomingSMS() except gammu.ERR_NOTSUPPORTED: print 'Your phone does not support incoming SMS notifications!' # We need to keep communication with phone to get notifications print 'Press Ctrl+C to interrupt' while 1: time.sleep(1) status = sm.GetBatteryCharge() print 'Battery is at %d%%' % status['BatteryPercent']