Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ClimenteA
Last active May 10, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClimenteA/4ee3bfec196c621341fd1fb0b4b0da08 to your computer and use it in GitHub Desktop.
Save ClimenteA/4ee3bfec196c621341fd1fb0b4b0da08 to your computer and use it in GitHub Desktop.
Auto respond to new incoming messages on your android phone using qpython3 and sl4a library
try:
import sl4a
droid = sl4a.Android()
except:
print("Can't initialize sl4a module!")
pass
import re
import random
import time
import itertools
li1 = ['msg1','msg2','msg3']
li2 = ['msg1','msg2','msg3']
#etc lists with random messages
testrandom = ['testok', 'testok1']
unreadtest = [0, 1]
def checklastmsg():
#Get all messages from inbox
try:
smsuri = (droid.smsGetMessages(False))
msgli = (smsuri[1])
#print(msgli[0]['body'])
lastmsg = msgli[0]['body']
countmsg = (droid.smsGetMessageCount(True).result)
#print(lastmsg, countmsg)
except:
lastmsg = random.choice(testrandom)
countmsg = random.choice(unreadtest)
return lastmsg, countmsg
#checklastmsg()
def choosemsg(lastmsg):
#Choose a response based on the last message received
if re.search('ms', lastmsg):
msgres = random.choice(li1)
elif re.search('another str', lastmsg):
msgres = random.choice(li2)
# elif re.search('', lastmsg):
# msgres = random.choice(lin)
#etc nr of choises
return msgres
#choosemsg('test string')
sphone = "Put here the phone number"
def sendmsg(phone, message):
print('Sending to: {} Message: {}'.format(phone, message))
#droid.smsSend(phone, message) #uncomment this to send message
def run(waitsec = 5):
#Compare previous value with current value
sectowait = list(range(waitsec))
lastmsg, unreadmsg = checklastmsg()
print('New init lastmsg: ',lastmsg, unreadmsg)
#input()
for i in itertools.count():
currmsg, currunreadmsg = checklastmsg()
#if currunreadmsg != 0:
if lastmsg == currmsg:
#print('No msg received yet. LastMsg:{}, CurMsg:{}'.format(lastmsg, currmsg))
time.sleep(waitsec)
else:
try:
print('New message! LastMsg:{}, CurMsg:{}'.format(lastmsg, currmsg))
msgtosend = choosemsg(currmsg)
time.sleep(random.choice(sectowait)) # wait random sec until send
sendmsg(sphone, msgtosend)
run() # some recurrsion here to store the last message received
except Exception as e:
print('Got: ', e)
break
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment