-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.py
55 lines (46 loc) · 1.44 KB
/
server.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
import os
from flask import Flask, request, redirect, g, render_template, jsonify
import twilio.twiml
import logging
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
numbers = set()
projects = [
{"name": "5C Course Recommender", "votes":0},
{"name": "5C Word War", "votes":0},
{"name": "Team Papa Bear", "votes":0},
{"name": "5Ception", "votes":0},
{"name": "Dragonfire", "votes": 0},
{"name": "Flixnext", "votes":0},
{"name": "Loki", "votes":0},
{"name": "5C Map", "votes":0},
{"name": "Team Antartica", "votes":0}
]
@app.route('/')
def index():
return render_template('index.html', projects=projects)
@app.route('/list')
def list():
return jsonify(projects=projects)
@app.route('/vote', methods=['POST'])
def vote():
from_number = request.form['From']
# number exists
resp = twilio.twiml.Response()
if from_number in numbers:
resp.sms("Thanks, but you already voted!")
else:
body = request.form['Body'].strip().upper()
letters = "ABCDEFGHIJKLMNOP"
ident = letters.find(body)
if len(body) != 1 or ident == -1 or ident >= len(projects):
resp.sms('That is an invalid vote, please try again!')
else:
projects[ident]['votes'] += 1
numbers.add(from_number)
resp.sms('Thank you for your vote!')
return str(resp)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)