def send_text(msg, up, force = False): text = msg.msg if len(text) >= 160: m = "Failed to send text, too long. {} -> {!s}".format(text, up) debug(m) add_to_report(m) return False if not up.user.is_active: m = "Failed to send text, user inactive. {} -> {!s}.".format(text, up) debug(m) add_to_report(m) return False if not (force or up.is_verified): m = "Failed to send text, user not verified. {} -> {!s}.".format(text, up) debug(m) add_to_report(m) return False try: client = get_twilio_client() c = client.sms.messages.create(to = up.phone, from_ = WATTTIME_PHONE, body = text) except twilio.TwilioRestException as e: m = "Failed to send text, twilio exception '{!s}'. {} -> {!s}.".format(e, text, up) debug(m) add_to_report(m) return False sms_tools.models.TwilioSMSEvent(user = up.user, msg_type = msg.msg_type, to_number = up.phone, from_number = WATTTIME_PHONE, body = msg.msg).save() debug ("Sent text. {} -> {!s}.".format(text, up)) return True
def send_ne_texts_if_necessary(): now = _now() now_ne = _now('America/New_York') is_weekday = (now_ne.isoweekday() <= 5) is_daytime = (9 <= now_ne.hour < 17) is_evening = (17 <= now_ne.hour < 22) fuel = NE.objects.all().latest().marginal_fuel fuel_name = MARGINAL_FUELS[fuel] # 0 = coal # 1 = oil # 2 = natural gas # 3 = refuse # 4 = hydro # 5 = wood # 6 = nuclear # 7 = solar # 8 = wind # 9 = none dirty_fuel = [0, 1] clean_fuel = [8, 7, 6, 5, 4] is_dirty = fuel in dirty_fuel is_clean = fuel in clean_fuel # No more than one message in each 20 hour period last_okay_time = now - datetime.timedelta(hours = 20) ups = UserProfile.objects.all() # Dirty texts, working hours if is_weekday and is_daytime and is_dirty: last_msg = latest_by_category(LastMessageSent.NE_dirty_daytime) if last_msg is None or last_msg.date < last_okay_time: if last_msg is None: last_msg = LastMessageSent() last_msg.category = LastMessageSent.NE_dirty_daytime last_msg.date = now last_msg.save() debug("Sending NE texts for dirty, working hours {}".format(last_msg.date)) for up in ups: if up.user.is_active and up.is_verified and up.region() == newengland: if up.get_region_settings().message_frequency == 0: message = ne_message_dirty_daytime(up, fuel_name) res = send_text(message, up) msg = 'Sent text "{}" to {}'.format(message.msg, up) if not res: msg = "FAILED: " + msg debug(msg) add_to_report(msg) # Dirty texts, after hours if is_dirty and (is_evening or (is_daytime and (not is_weekday))): last_msg = latest_by_category(LastMessageSent.NE_dirty_evening) if last_msg is None or last_msg.date < last_okay_time: if last_msg is None: last_msg = LastMessageSent() last_msg.category = LastMessageSent.NE_dirty_evening last_msg.date = now last_msg.save() debug("Sending NE texts for dirty, after hours {}".format(last_msg.date)) for up in ups: if up.user.is_active and up.is_verified and up.region() == newengland: if up.get_region_settings().message_frequency == 1: message = ne_message_dirty_evening(up, fuel_name) res = send_text(message, up) msg = 'Sent text "{}" to {}'.format(message.msg, up) if not res: msg = "FAILED: " + msg debug(msg) add_to_report(msg) # Clean texts, after hours if is_clean and (is_evening or (is_daytime and (not is_weekday))): last_msg = latest_by_category(LastMessageSent.NE_clean) if last_msg is None or last_msg.date < last_okay_time: if last_msg is None: last_msg = LastMessageSent() last_msg.category = LastMessageSent.NE_clean last_msg.date = now last_msg.save() debug("Sending NE texts for clean {}".format(last_msg.date)) for up in ups: if up.user.is_active and up.is_verified and up.region() == newengland: if up.get_region_settings().message_frequency == 2: message = ne_message_clean(up, fuel_name) res = send_text(message, up) msg = 'Sent text "{}" to {}'.format(message.msg, up) if not res: msg = "FAILED: " + msg debug(msg) add_to_report(msg)