-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMSTrivia.py
executable file
·73 lines (60 loc) · 2.25 KB
/
SMSTrivia.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
#!/usr/bin/env python
from twilio.rest import TwilioRestClient
from flask import Flask, request, redirect, session
import twilio.twiml
import os
import threading
from TriviaWindow import TriviaWindow
from PySide.QtGui import QApplication, QIcon
import sys
import requests
from subprocess import Popen,DEVNULL
app = Flask(__name__)
app.config.from_object(__name__)
app.secret_key = os.urandom(24)
def runFlask():
app.run(debug=True, use_reloader=False)
@app.route("/", methods=['GET', 'POST'])
def respond():
resp = twilio.twiml.Response()
state = int(session.get('state',0))
rx = request.values.get('Body')
sender = request.values.get('From')
if(window.centralWidget().registrationActive):
if(state == 0):
if(rx.lower().strip() == 'join'):
tx = "You have now joined Dragon Brain Buster!\nGood luck!"
state = 1
window.centralWidget().registerUser(sender)
else:
tx = "Welcome to Dragon Brain Buster!\nReply JOIN to join today's trivia challenge."
elif(state == 1):
tx = "You have already joined Dragon Brain Buster!\nPlease wait for the registration period to finish."
session['state'] = state
elif(window.centralWidget().questionActive):
if(state == 0):
tx = "Welcome to Dragon Brain Buster!\nRegistration is currently closed, please try again next time."
elif(state == 1):
answer = rx
window.centralWidget().putAnswer(sender, answer)
tx = "You selected answer: " + answer + ".\nPlease check that this is your desired answer. Feel free to change your answer before time runs out."
else:
tx = "Sorry, there is no current active session"
resp.sms(tx)
return str(resp)
@app.route('/exit', methods=['POST'])
def exit():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
return "Shutting down..."
flaskThread = threading.Thread(target=runFlask)
flaskThread.start()
qapp = QApplication(sys.argv)
qapp.setWindowIcon(QIcon("./icon.png"))
window = TriviaWindow()
qapp.exec_()
requests.post('http://localhost:5000/exit')
flaskThread.join()
sys.exit()