-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
186 lines (144 loc) · 5.93 KB
/
application.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from flask import redirect, request, render_template, Response, session
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql.expression import func
from functools import wraps
from sms_swap import create_app
from sms_swap.config import CONFIG_VARS
from sms_swap.models import Answer
from sms_swap.database import db
import twilio.twiml
from twilio.rest import TwilioRestClient
application = create_app()
@application.route("/")
def index():
# TODO: grab some recordings to show
return render_template('index.html')
@application.route("/respond", methods=['GET', 'POST'])
def respond():
"""Respond to incoming texts"""
resp = twilio.twiml.Response()
incoming_msg = request.values.get('Body', '')
# this clears all cookies
if(incoming_msg=='clear'):
session['seen_prompt'] = False
session['gave_rec'] = False
session['extra_msg'] = False
resp.sms("erasing my memory of our conversation")
return str(resp)
# if exchange has been completed
if session.get('extra_msg',False):
session['extra_msg'] = False
if incoming_msg.lower().startswith('y'):
session['seen_prompt'] = False
session['gave_rec'] = False
elif incoming_msg.lower().startswith('n'):
# TODO: ask for feedback?
resp.sms('ok, later!')
return str(resp)
if not session.get('seen_prompt',False):
resp.sms("🎵give music, get music🎵\n\nreply and tell me about any song! say, something you've had on repeat, or a hidden gem you wish more people knew about, or just the last thing you listened to.")
session['seen_prompt'] = True
elif not session.get('gave_rec',False):
# saving rec here
if incoming_msg: # only if there is a msg
new_rec = Answer(request.values.get('SmsSid'), request.values.get('From'), incoming_msg)
db.session.add(new_rec)
db.session.commit()
# grabbing a rec from a stranger
random_rec = Answer.query.filter_by(is_approved=True)\
.filter((Answer.from_number!=request.values.get('From'))|(Answer.from_number == None))\
.order_by(func.rand())\
.first()
if random_rec:
resp.sms("thanks! I'll pass it on. here's a song from a stranger:\n\n%s" %random_rec.answer_text)
if incoming_msg: # only if there is a msg
random_rec.view_count = random_rec.view_count+1
db.session.add(random_rec)
db.session.commit()
# alerting rec-giver when rec has been seen for the first time
if random_rec.from_number and random_rec.view_count==1:
client = TwilioRestClient(
CONFIG_VARS['TWILIO_ACCOUNT_SID'],
CONFIG_VARS['TWILIO_AUTH_TOKEN']
)
msg = "your rec (%s) was just delivered to a stranger" %random_rec.answer_text
message = client.messages.create(
to=random_rec.from_number,
from_=CONFIG_VARS['TWILIO_PHONE_NO'],
body=msg
)
else:
# TODO: deal with this
resp.sms("thanks! unfortunately I don't have any songs to show you b/c I haven't collected enough")
session['gave_rec'] = True
else:
# TODO: a more elegant way of handling additional messages?
session['extra_msg'] = True
resp.sms("do you want to start over & exchange another song? \n(y/n)")
return str(resp)
@application.route("/rollback", methods=['GET', 'POST'])
def rollback():
# TODO: figure out what's going on???
db.session.rollback()
return redirect('/respond')
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == CONFIG_VARS['ADMIN_USER'] and password == CONFIG_VARS['ADMIN_PASS']
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your credentials for that url', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
@application.route('/review')
@requires_auth
def review():
review_queue = Answer.query.filter_by(is_approved=None).all()
approved = Answer.query.filter_by(is_approved=True).all()
return render_template('review.html', review_queue = review_queue, approved=approved)
@application.route('/reviewtrash')
@requires_auth
def reviewtrash():
disapproved = Answer.query.filter_by(is_approved=False).all()
return render_template('reviewtrash.html', disapproved=disapproved)
@application.route('/approve/<ans_id>')
@requires_auth
def approve(ans_id):
ans = Answer.query.get(ans_id)
ans.is_approved = True
db.session.commit()
return redirect('/review')
@application.route('/disapprove/<ans_id>')
@requires_auth
def disapprove(ans_id):
ans = Answer.query.get(ans_id)
ans.is_approved = False
db.session.commit()
return redirect('/review')
@application.route('/add', methods=['GET', 'POST'])
@requires_auth
def add():
# manually adding songs
if request.method == 'POST':
new_ans = Answer(None, None, request.form['song'])
new_ans.is_approved = True
db.session.add(new_ans)
db.session.commit()
return render_template('add.html')
@application.route('/initialize')
@requires_auth
def initialize():
# TODO: only do this if tables don't exist?
db.create_all()
return redirect('/')
if __name__ == "__main__":
application.run(host='0.0.0.0')