/
app.py
385 lines (301 loc) · 12.8 KB
/
app.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import os
import re
import sys
import json
import logging
from datetime import datetime, time, timedelta
from flask import Flask, render_template, request, redirect, url_for, flash
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.exc import IntegrityError
from raven.contrib.flask import Sentry
from twilio import TwilioClient
from forecast import ForecastClient
from geocoding import GeoCodingClient
app = Flask(__name__)
app.secret_key = os.urandom(24)
# config postgres
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['POSTGRES_URL']
db = SQLAlchemy(app)
# config sentry
app.config['SENTRY_DSN'] = os.environ['SENTRY_DSN']
sentry = Sentry(app)
# config logging
l = logging.StreamHandler(sys.stdout)
l.setLevel(logging.DEBUG)
app.logger.addHandler(l)
@app.route("/", methods=['GET', 'POST'])
def homepage():
if request.method == 'POST':
print "new signup"
print json.dumps(request.form)
data = {
"phone": re.sub("[^\d.]", "", request.form.get("phone", "")),
"location": request.form.get("location"),
"alarm_hour": request.form.get("alarm-hour"),
"alarm_minute": request.form.get("alarm-minute"),
"alarm_meridian": request.form.get("alarm-meridian"),
"time_zone": request.form.get("time-zone"),
}
for key, value in data.items():
if value is None or value == "":
flash("Whoops, don't forget to fill out %s" % key, "error")
return redirect(url_for('homepage'))
if key == "phone" and len(value) < 11:
flash("'%s' seems a bit short for a phone number. Make sure you include the country code." % value, "error")
return redirect(url_for('homepage'))
if key == "location" and len(value) < 5:
flash("Tell us more about your location. '%s' isn't much to work with." % value, "error")
return redirect(url_for('homepage'))
u = User(**data)
db.session.add(u)
try: # validate the number by sending to it
u.send_message(
"Thanks for signing up with Rooster App! Your forecasts will be delivered at {hr}:{min}{mer} Mon-Fri. Reply 'STOP' to pause messages, 'OPTIONS' to change settings.".format(
hr=u.alarm_hour,
min=u.alarm_minute,
mer=u.alarm_meridian
),
"welcome"
)
except:
flash("Hmm, we're having trouble sending to that phone number. Make sure you include your country code.", "error")
return redirect(url_for('homepage'))
try: # data has validated, now try saving to DB
db.session.commit()
flash("Cock-a-doodle-doo! You should get a confirmation text in a few seconds.", "success")
except IntegrityError:
try:
User.query.filter_by(phone=u.phone)
flash("Hey thanks! Looks like you already signed up.", "warning")
except:
flash("Uh oh! Error saving you to the database", "error")
print "unknown error..."
return redirect(url_for('homepage'))
elif request.method == 'GET':
if "herokuapp.com" in request.url:
return redirect("http://www.roosterapp.co/"), 301
else:
return render_template("homepage.html")
@app.route("/donate/", methods=['GET'])
def donate():
return render_template("donate.html")
@app.route("/message/receive/", methods=['GET', 'POST'])
def process_inbound_message():
if request.method == "GET":
return "Send me messages! http://www.twilio.com/help/faq/sms"
print "incoming message"
message_number = re.sub("[^\d.]", "", request.form.get("From", ""))
message_body = request.form.get("Body").strip()
print "from %s" % message_number
print message_body
actions_performed = []
errors_encountered = []
user = User.query.filter(User.phone == message_number).first()
if user is None:
t = TwilioClient()
message = "Couldn't find %s in our system. Go to http://www.roosterapp.co to sign up!" % (message_number)
t.send_message(to=message_number, message=message)
return message
# reactivate account
reactivate_keywords = ["start", "yes"]
for word in reactivate_keywords:
if word in message_body.lower():
user.is_active = True
actions_performed.append("reactivated your account. Welcome back!")
# deactivate account
deactivate_keywords = ["stop", "block", "cancel", "unsubscribe", "quit"]
for word in deactivate_keywords:
if word in message_body.lower():
user.is_active = False
actions_performed.append("deactivated your account. Send 'START' to reactive.")
# update location
location_index = message_body.lower().find("location:")
if location_index != -1:
location_offset = location_index + len("location:")
location = message_body[location_offset:].strip()
user.location = location
user.latitude = ""
user.longitude = ""
user.is_active = True
actions_performed.append("updated location to %s" % location)
# update wake up time
time_index = message_body.lower().find("time:")
if time_index != -1:
time_offset = time_index + len("time:")
time = message_body[time_offset:].strip()
try:
hour, minute, meridian = parse_time(time)
user.alarm_hour = hour
user.alarm_minute = minute
user.alarm_meridian = meridian
user.is_active = True
actions_performed.append("updated wake up time to {hour}:{min}{mer}".format(hour=hour, min=minute, mer=meridian))
except Exception as e:
errors_encountered.append(str(e))
# update timezone
timezone_index = message_body.lower().find("tz:")
if timezone_index != -1:
timezone_offset = timezone_index + len("tz:")
timezone = message_body[timezone_offset:].strip()
try:
assert int(timezone) in range(-11, 13)
user.time_zone = timezone
user.is_active = True
actions_performed.append("updated time zone to %s" % timezone)
except:
errors_encountered.append("timezone %s appears to be invalid" % timezone)
# see what happened
if errors_encountered:
message = "Uh oh! " + ", ".join(errors_encountered)
elif actions_performed:
db.session.add(user)
db.session.commit()
message = "Successfully " + ", ".join(actions_performed)
elif "status" in message_body.lower():
message = "Current account status:\nACTIVE: {is_active}\nLOCATION: {location}\nTIME: {hour}:{minute}{meridian}\nTZ:{timezone}".format(
is_active=user.is_active,
location=user.location,
hour=user.alarm_hour,
minute=user.alarm_minute,
meridian=user.alarm_meridian,
timezone=user.time_zone
)
else:
message = "Reply w:\n'LOCATION:' with a town or region\n'TIME:' formatted HH:MM (in 24hr format)\n'TZ:' timezone offset, ie -4\n'STOP' to stop\n'STATUS' for current acct info"
print message
user.send_message(message, "response")
return message
def parse_time(t):
# must be in format HH:MM and minute must be a multiple of 15
time = t.strip()
try:
hour, minute = time.split(":")
parsed_hour = int(hour)
parsed_minute = int(minute)
assert parsed_hour in range(0, 24)
except:
raise Exception("The time you sent (%s) appears to be invalid" % time)
try:
assert minute in ["00", "15", "30", "45"]
except:
raise Exception("The minutes must be either '00', '15', '30', '45', not %s" % minute)
if parsed_hour > 12:
meridian = "pm"
hour = parsed_hour - 12
else:
meridian = "am"
return hour, minute, meridian
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # Models # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class User(db.Model):
__tablename__ = "rooster_users"
id = db.Column(db.Integer, primary_key=True)
phone = db.Column(db.String(30), unique=True)
is_active = db.Column(db.Boolean, default=True)
alarm_hour = db.Column(db.String(2))
alarm_minute = db.Column(db.String(2))
alarm_meridian = db.Column(db.String(2))
time_zone = db.Column(db.String(3))
location = db.Column(db.String(64))
latitude = db.Column(db.String(24), default="")
longitude = db.Column(db.String(24), default="")
created_on = db.Column(db.DateTime, default=db.func.now())
def __init__(self, phone, location, alarm_hour, alarm_minute, alarm_meridian, time_zone):
self.phone = phone
self.location = location
self.alarm_hour = alarm_hour
self.alarm_minute = alarm_minute
self.alarm_meridian = alarm_meridian
self.time_zone = time_zone
def __repr__(self):
return '<User %r>' % self.phone
def needs_message_now(self):
"""
Should we text the user right now?
"""
utc_now = datetime.utcnow()
if utc_now.weekday() in [5, 6]:
return False # don't send on weekends
# figure out the hour they wanted to wake up
alarm_hour = int(self.alarm_hour)
if self.alarm_meridian.lower() == "pm":
alarm_hour += 12
# shift the hour for their timezone
time_zone = self.time_zone
if time_zone.startswith("-"):
desired_alarm_hour = alarm_hour + int(time_zone.split("-")[1])
else:
desired_alarm_hour = alarm_hour - int(time_zone)
# make sure we don't have any impossible hours
if desired_alarm_hour > 23:
desired_alarm_hour -= 24
elif desired_alarm_hour < 0:
desired_alarm_hour += 24
# build a datetime object representing their desired wakeup time in UTC
today = datetime.today()
todays_desired_alarm_time = datetime(
hour=desired_alarm_hour,
minute=int(self.alarm_minute),
year=today.year,
month=today.month,
day=today.day
)
margin_of_error = timedelta(seconds=60*8) # 8 minutes in either direction (16m window total)
if utc_now - margin_of_error < todays_desired_alarm_time < utc_now + margin_of_error:
return True
def send_message(self, message, category):
"""
An method to send a message to this user.
"""
t = TwilioClient()
t.send_message(to=self.phone, message=message)
text = Text(user=self, message=message, category=category)
db.session.add(text)
db.session.commit()
return True
def send_forecast(self):
"""
Send this user their forecast
"""
# ensure we didn't already text this person today
today = datetime.utcnow().date()
texts = Text.query.filter(Text.user_id == self.id).all()
for text in texts:
if text.category == "forecast" and text.sent.date() == today:
return False
if self.latitude == "" or self.longitude == "":
g = GeoCodingClient()
geo_info = g.lookup_location(self.location)
latitude = geo_info["results"][0]["geometry"]["location"]["lat"]
longitude = geo_info["results"][0]["geometry"]["location"]["lng"]
# save this lat/lon info so we don't need to look up each time
self.latitude = latitude
self.longitude = longitude
db.session.add(self)
db.session.commit()
else:
latitude = self.latitude
longitude = self.longitude
f = ForecastClient(time_zone=self.time_zone)
forecast = f.get_forecast(latitude, longitude)
self.send_message(forecast, "forecast")
return True
class Text(db.Model):
__tablename__ = "rooster_texts"
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('rooster_users.id'))
user = db.relationship('User', backref=db.backref('texts', lazy='dynamic'))
sent = db.Column(db.DateTime)
message = db.Column(db.String(160))
category = db.Column(db.String(32))
def __init__(self, user, message, category):
self.user = user
self.sent = datetime.utcnow()
self.message = message
self.category = category
def __repr__(self):
return '<Text %r>' % self.message
if __name__ == "__main__":
app.debug = True
app.run()