def received_twilio_sms(twilio_sms): from_phone = normalize_phone_number(twilio_sms.from_number) print str(from_phone) chat_line_phone = normalize_phone_number(twilio_sms.to_number) print str(chat_line_phone) body = twilio_sms.body track_sms_received(from_phone, twilio_sms.body) if ChatLine.objects.filter(phone=chat_line_phone).exists(): chat_line = ChatLine.objects.get(phone=chat_line_phone) from_user = User.objects.get(username=from_phone) if SmsConversation.objects.filter( chat_line=chat_line, active=True).filter( Q(user_one=from_user) | Q(user_two=from_user)).exists(): print "Found SMS Conversation" if SmsConversation.objects.filter(user_one=from_user, chat_line=chat_line, active=True).exists(): sms_conversation = SmsConversation.objects.get( user_one=from_user, chat_line=chat_line, active=True) to_phone = sms_conversation.user_two.username print "User One" send_chat_sms(body, to_phone, chat_line_phone) elif SmsConversation.objects.filter(user_two=from_user, chat_line=chat_line, active=True).exists(): sms_conversation = SmsConversation.objects.get( user_two=from_user, chat_line=chat_line, active=True) to_phone = sms_conversation.user_one.username print "User Two" send_chat_sms(body, to_phone, chat_line_phone)
def check_to_send_final_reminder_staffer_notifications(): try: min_dt = datetime.datetime.now() max_dt = datetime.datetime.now() + datetime.timedelta(hours=1) if SponsoredEvent.objects.filter(start__gte=min_dt, start__lte=max_dt).exists(): events = SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)) count = 0 for event in events: staffers = EventStaffer.objects.filter( event=event, status=STAFFER_STATUS.PRIMARY) for staffer in staffers: text = "You should be on your way to the event at {0}. The address is {1}. Please confirm when you arrive by responding 'HERE'".format( staffer.event.place.name, staffer.event.place.street_address) send_chat_sms( text, normalize_phone_number(staffer.manager.phone_number), event_manager_number) notification = StafferNotification( event=staffer.event, manager=staffer.manager, notification_type=STAFFER_NOTIFICATION.FIRST_REMINDER) notification.save() count = count + 1 gm_sms = "{0} final reminders were sent for events today".format( count) send_chat_sms(gm_sms, "5413359388", event_manager_number) except: html = "Sending final staffer notifications failed" traceback.print_exc() html = html + str(traceback.format_exc()) send_error_email(html)
def send_backup_confirmation_to_staffer(event_staffer): manager = event_staffer.manager event = event_staffer.event time = get_event_time(event) date = get_event_date(event) url = get_shortened_staffer_url(event_staffer) body = "You're confirmed as a back-up for the event at {0} on {1} at {2}.\n\nEvent Link: {3}".format( event.place.name, date, time, url) send_chat_sms(body, manager.phone_number, event_manager_number)
def check_to_send_initial_staffer_notifications(): try: upcoming_events = SponsoredEvent.objects.filter( start__gte=datetime.datetime.now()) for event in upcoming_events: has_primary_staffer = EventStaffer.objects.filter( event=event).filter(status=STAFFER_STATUS.PRIMARY).exists() has_backup_staffer = EventStaffer.objects.filter( event=event).filter(status=STAFFER_STATUS.BACKUP).exists() if not (has_primary_staffer or has_backup_staffer): market = get_market_from_lat_lng(event.place.latitude, event.place.longitude) texted_managers = StafferNotification.objects.filter( event=event, notification_type=STAFFER_NOTIFICATION.INITIAL ).values_list('manager', flat=True) managers_to_text = EventManager.objects.filter( market=market).exclude(pk__in=texted_managers).exclude( status=MANAGER_STATUS.INACTIVE) if managers_to_text: time = get_event_time(event) date = get_event_date(event) if not has_primary_staffer: body = "Hotspot here! Would you be able to staff the event at {0} on {1} at {2}?\n\nPlease respond either 'Yes' or 'No' as soon as you can".format( event.place.name, date, time) for manager in managers_to_text: send_chat_sms(body, manager.phone_number, event_manager_number) create_initial_staffer_notification_tracker( event, manager) elif not has_backup_staffer: body = "Hotspot here! Would you be able to be the back-up for the event at {0} on {1} at {2}?\n\nPlease respond either 'Yes' or 'No' as soon as you can".format( event.place.name, date, time) for manager in managers_to_text: send_chat_sms(body, manager.phone_number, event_manager_number) create_initial_staffer_notification_tracker( event, manager) # else: # if (not has_primary_staffer): # body = "{0} doesn't have a primary staffer for the event at {1} and no more managers to text".format( # market.name, event.place.name) # send_chat_sms(body, "5413359388", event_manager_number) # if (not has_backup_staffer): # body = "{0} doesn't have a backup staffer for the event at {1} and no more managers to text".format( # market.name, event.place.name) # send_chat_sms(body, "5413359388", event_manager_number) except: html = "Sending initial staffer notifications failed" traceback.print_exc() html = html + str(traceback.format_exc()) send_error_email(html)
def send_day_before_reminder(): tomorrow = datetime.datetime.now() + datetime.timedelta(days=1) min_dt = datetime.datetime.combine(tomorrow, datetime.time.min) max_dt = datetime.datetime.combine(tomorrow, datetime.time.max) if SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)).exists(): events = SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)) for event in events: staffers = EventStaffer.objects.filter(event=event) for staffer in staffers: url = get_shortened_staffer_url(staffer) time = get_event_time(staffer.event) text = "You're scheduled to staff the event tomorrow at {0}. Make sure you arrive at {1} by {2}.\n " \ "Event Dashboard Link: {3}".format(staffer.event.place.name, staffer.event.place.street_address, time, url) send_chat_sms( text, [normalize_phone_number(staffer.manager.phone_number)])
def send_post_save_notifications_to_staffer(event): try: market = get_market_from_lat_lng(event.place.latitude, event.place.longitude) managers_to_text = EventManager.objects.filter( market=market, status=MANAGER_STATUS.PRIMARY) time = get_event_time(event) date = get_event_date(event) body = "Hotspot here! Tickets just went live for an event at {0} on {1} at {2}. Would you be able to staff it?\n\nPlease respond either 'Yes' or 'No' as soon as you can".format( event.place.name, date, time) for manager in managers_to_text: create_initial_staffer_notification_tracker(event, manager) send_chat_sms(body, manager.phone_number, event_manager_number) except: html = "Sending post save notifications to staffer failed" traceback.print_exc() send_error_email(html)
def check_to_send_first_reminder_staffer_notifications(): try: day_after_tomorrow = datetime.datetime.now() + datetime.timedelta( days=2) min_dt = datetime.datetime.combine(day_after_tomorrow, datetime.time.min) max_dt = datetime.datetime.combine(day_after_tomorrow, datetime.time.max) if SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)).exists(): events = SponsoredEvent.objects.filter(start__range=(min_dt, max_dt)) count = 0 for event in events: staffers = EventStaffer.objects.filter( event=event, status=STAFFER_STATUS.PRIMARY) for staffer in staffers: url = get_shortened_staffer_url(staffer) time = get_event_time(staffer.event) text = "You're scheduled to staff the event day after tomorrow at {0}. Make sure you arrive at {1} by {2}. Please confirm this by responding 'Yes'".format( staffer.event.place.name, staffer.event.place.street_address, time) send_chat_sms( text, normalize_phone_number(staffer.manager.phone_number), event_manager_number) notification = StafferNotification( event=staffer.event, manager=staffer.manager, notification_type=STAFFER_NOTIFICATION.FIRST_REMINDER) notification.save() count = count + 1 gm_sms = "{0} first reminders were sent for events in two days".format( count) send_chat_sms(gm_sms, "5413359388", event_manager_number) except: html = "Sending first reminder staffer notifications failed" traceback.print_exc() html = html + str(traceback.format_exc()) send_error_email(html)
def received_twilio_sms_from_staffer(twilio_sms): from_phone = normalize_phone_number(twilio_sms.from_number) body = twilio_sms.body manager = EventManager.objects.get(phone_number=from_phone) if StafferNotification.objects.filter(manager=manager).exists(): last_notification = StafferNotification.objects.filter( manager=manager).latest('date_created') print from_phone print body if last_notification.notification_type == STAFFER_NOTIFICATION.INITIAL: if ("yes" in body.lower()) and (last_notification.response is None): event = last_notification.event last_notification.response = STAFFER_RESPONSE.YES last_notification.save() if not EventStaffer.objects.filter(event=event).filter( status=STAFFER_STATUS.PRIMARY).exists(): event_staffer = EventStaffer(event=event, manager=manager, status=STAFFER_STATUS.PRIMARY) event_staffer.save() else: event_staffer = EventStaffer(event=event, manager=manager, status=STAFFER_STATUS.BACKUP) event_staffer.save() elif ("no" in body.lower()) and (last_notification.response is None): last_notification.response = STAFFER_RESPONSE.NO last_notification.save() else: text_for_gm = "{0} in {1} ({2}) sent this: {3}".format( manager.name, manager.market, manager.phone_number, body) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) elif last_notification.notification_type == STAFFER_NOTIFICATION.FIRST_REMINDER: if ("yes" in body.lower()) and (last_notification.response is None): last_notification.response = STAFFER_RESPONSE.YES last_notification.save() staffer = EventStaffer.objects.get( event=last_notification.event, manager=last_notification.manager) url = get_shortened_staffer_url(staffer) text_to_staffer = "Awesome!\n\nEvent Link: {0}".format(url) send_chat_sms(text_to_staffer, last_notification.manager.phone_number, event_manager_phone) text_to_gm = "{0} just checked in for event at {1}".format( last_notification.manager.name, last_notification.event.place.name) send_chat_sms(text_to_gm, "5413359388", event_manager_phone) else: text_for_gm = "{0} in {1} ({2}) sent this: {3}".format( manager.name, manager.market, manager.phone_number, body) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) elif last_notification.notification_type == STAFFER_NOTIFICATION.SECOND_REMINDER: if ("yes" in body.lower()) and (last_notification.response is None): last_notification.response = STAFFER_RESPONSE.YES last_notification.save() staffer = EventStaffer.objects.get( event=last_notification.event, manager=last_notification.manager) url = get_shortened_staffer_url(staffer) text_to_staffer = "Awesome!\n\nEvent Link: {0}".format(url) send_chat_sms(text_to_staffer, last_notification.manager.phone_number, event_manager_phone) text_to_gm = "{0} just checked in for event at {1}".format( last_notification.manager.name, last_notification.event.place.name) send_chat_sms(text_to_gm, "5413359388", event_manager_phone) else: text_for_gm = "{0} in {1} ({2}) sent this: {3}".format( manager.name, manager.market, manager.phone_number, body) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) elif last_notification.notification_type == STAFFER_NOTIFICATION.THIRD_REMINDER: if ("here" in body.lower()) and (last_notification.response is None): last_notification.response = STAFFER_RESPONSE.YES last_notification.save() staffer = EventStaffer.objects.get( event=last_notification.event, manager=last_notification.manager) url = get_shortened_staffer_url(staffer) text_to_staffer = "Awesome!\n\nEvent Link: {0}".format(url) send_chat_sms(text_to_staffer, last_notification.manager.phone_number, event_manager_phone) text_to_gm = "{0} just checked in for event at {1}".format( last_notification.manager.name, last_notification.event.place.name) send_chat_sms(text_to_gm, "5413359388", event_manager_phone) else: text_for_gm = "{0} in {1} ({2}) sent this: {3}".format( manager.name, manager.market, manager.phone_number, body) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) else: text_for_gm = "Error: {0}".format(manager.name) send_chat_sms(text_for_gm, "5413359388", event_manager_phone) else: text_for_gm = "Error: {0}".format(manager.name) send_chat_sms(text_for_gm, "5413359388", event_manager_phone)