-
Notifications
You must be signed in to change notification settings - Fork 2
/
sms.py
139 lines (116 loc) · 4.78 KB
/
sms.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
import time
import sqlite3
import re
import json
import random
import googlevoice as gv
import datetime
class Neg(object):
call = None
def __init__(self, filename):
f = open(filename, "r")
self.choices = [line.strip() for line in f]
f.close()
Neg.call = self
def randomchoice(self):
return random.choice(self.choices)
class QuestionHandler(object):
def __init__(self, dbname):
self.connection = sqlite3.connect(dbname)
self.cursor = self.connection.cursor()
self.cursor.execute("SELECT MAX(Questions.qid) FROM Questions")
level, = self.cursor.fetchone()
self.maxlevel = int(level)
def getlevel(self, user):
self.cursor.execute("SELECT Users.qid FROM Users WHERE Users.phone=?", (user,))
currentlevel, = self.cursor.fetchone()
return int(currentlevel)
def getduration(self, user):
self.cursor.execute("SELECT Users.created, Users.finished FROM Users WHERE Users.phone=?", (user,))
created, finished, = self.cursor.fetchone()
return finished-created
def levelup(self, user):
level = self.getlevel(user)
print "Currently %d/%d level" % (level, self.maxlevel)
if level >= self.maxlevel:
return False # User is finished with the game
else:
self.cursor.execute("UPDATE Users SET qid=? WHERE phone=?", (str(level+1), user,))
self.connection.commit()
return True # User is the next level
def checkanswer(self, user, message):
question, answer = self.userinfo(user)
print "user:", user
print "sent:", message
print "correct answer:", answer
message_list = message.lower().split(' ')#re.findall(r"\w+", message)
if len(set(answer.lower().split(' ')).intersection(set(message_list))) > 0:
leveled = self.levelup(user)
if leveled:
if self.getlevel(user) == 1:
return "You may now begin at Portal #1: Entrance. Happy testing!"
question, answer = self.userinfo(user)
if self.getlevel(user) == 2:
return "Correct! You may now 'pass through' to Portal #1: Exit.\nHint: Use the Portal #1: Entrance image of the other side as a clue to finding Portal #1: Exit."
else:
return "Correct! You may now 'pass through' to Portal #%i: Exit." % self.getlevel(user)
else:
self.cursor.execute("UPDATE Users SET finished=? WHERE phone=?", (time.time(), user))
self.connection.commit()
duration = self.getduration(user)
return "Congratulations! You have finished the game with a pitiful time of %s. Oh and we lied about the cake.\n\nYou will be contacted when the testing is complete." % (datetime.timedelta(seconds=duration))
else:
if self.getlevel(user) == 0: # game has not begun
return "Text the word play to begin. You will find the first portal as well as directions in the BSOE hallway near Perk's."
else: # wrong answer
return "Sorry, %s is not the correct answer for this question. Please try again at Portal #%s: Entrance." % (message, question)
def createuser(self, user):
self.cursor.execute("INSERT INTO Users(phone, qid, created) Values(?, 0, ?)", (user, time.time()))
self.connection.commit()
def userinfo(self, user):
self.cursor.execute("SELECT Questions.question, Questions.answer FROM Questions, Users WHERE Questions.qid = Users.qid AND Users.phone=?", (user,))
try:
question, answer = self.cursor.fetchone()
return question, answer
except Exception, e:
print e
self.createuser(user)
return self.userinfo(user)
class Spawn(object):
def __init__(self, voice, db):
self.voice = voice
self.db = db
def checkmessages(self):
messages = self.voice.sms().messages
for message in messages:
if message.isRead == False:
value = self.readmessage(message)
print "check messages: ", value
if value == True:
message.mark()
message.delete()
def readmessage(self, message):
user = message.phoneNumber
text = message.messageText
try:
reply = self.db.checkanswer(user, text)
self.voice.send_sms(user, reply)
print "SENDING: %s\nTO:%s" % (reply, user)
return True
except Exception, e:
print e
return False
def main():
Neg("neg_responses.data") # init Neg class
voice = gv.Voice()
username = "USERNAME"
password = "PASSWORD"
voice.login(email=username, passwd=password)
db = QuestionHandler("portal.sqlite")
spawn = Spawn(voice, db)
while True:
spawn.checkmessages()
time.sleep(10)
if __name__ == '__main__':
main()