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()
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.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) else: latitude = self.latitude longitude = self.longitude f = ForecastClient() forecast = f.get_forecast(latitude, longitude) t = TwilioClient() t.send_message(self.phone, forecast) text = Text(user=self, message=forecast) db.session.add(text) db.session.commit() return True
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() t = TwilioClient() if user is None: if message_number.startswith("1"): # see if an american forgot to sign up w their country code user = User.query.filter(User.phone == message_number[1:]).first() if user is None: 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 %s" % time) 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) return message
def send_message(self, message): t = TwilioClient() t.send_message(to=self.phone, message=message)
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