def invite_phone_number( self, inviter, phone_number_str, nickname, date_invited, sms_message_formatter, sms_analytics_event_name, sms_analytics_event_properties, ): """ phone_number_str: Must be formatted as international E164 Returns the user """ try: phone_number = PhoneNumber.objects.get(phone_number=phone_number_str) if phone_number.verified: return phone_number.user except PhoneNumber.DoesNotExist: new_user = User.objects.create_user(nickname=nickname) phone_number = PhoneNumber.objects.create( phone_number=phone_number_str, user=new_user, date_created=date_invited, verified=False ) new_user.pick_anonymous_avatar(phone_number_str) new_user.save() user_avatar_changed.send(sender=self, user=new_user) link_code_object, created = PhoneNumberLinkCode.objects.get_or_create( phone_number=phone_number, defaults={ "invite_code": PhoneNumberLinkCode.generate_invite_code(), "inviting_user": inviter, "date_created": date_invited, "was_visited": False, }, ) inviter_phone = inviter.get_primary_phone_number() if inviter_phone: sender_phone = inviter_phone.phone_number else: sender_phone = None message = sms_message_formatter(link_code_object) invite_url_prefix = "https://i.useglance.com" send_sms( phone_number.phone_number, message + "\n" + link_code_object.get_invite_page(invite_url_prefix), sender_phone, ) track_event(link_code_object.phone_number.user, sms_analytics_event_name, sms_analytics_event_properties) return link_code_object.phone_number.user
def process_scheduled_invites(current_time): num_sent = 0 for scheduled_message in ScheduledSMSInviteMessage.objects.get_scheduled_till(current_time): if not scheduled_message.link_code.was_visited: send_immediate_invite(scheduled_message.link_code, scheduled_message.message_template, scheduled_message.sms_sender_phone_override) track_event(scheduled_message.link_code.phone_number.user, 'New User SMS Invite Sent', { 'inviter': scheduled_message.link_code.inviting_user.id, 'time_delay_hours': scheduled_message.time_delay_hours}) num_sent += 1 scheduled_message.delete() return num_sent
def send_invite(inviter, phone_number, current_time): link_code, created = PhoneNumberLinkCode.objects.get_or_create(phone_number=phone_number, defaults={ 'invite_code': PhoneNumberLinkCode.generate_invite_code(), 'inviting_user': inviter, 'date_created': current_time, 'was_visited': False }) if not created: # If the target user was previously invited, update his link_code # object so that the inviter reflects the user who most recently # invited him link_code.inviting_user = inviter link_code.save(update_fields=['inviting_user']) # Delete any scheduled invites that the target user may already # have. New scheduled events will be set for the current invite ScheduledSMSInviteMessage.objects.filter(link_code=link_code).delete() destination_country_calling_code = phonenumbers.parse(phone_number.phone_number).country_code if SMSInviteMessage.objects.country_calling_code_use_default(destination_country_calling_code): immediate_message_obj = SMSInviteMessage.objects.get(country_calling_code=SMSInviteMessage.COUNTRY_DEFAULT_VALUE, time_delay_hours=0) delayed_message_objs = SMSInviteMessage.objects.filter(country_calling_code=SMSInviteMessage.COUNTRY_DEFAULT_VALUE, time_delay_hours__gt=0) else: immediate_message_obj = SMSInviteMessage.objects.get(country_calling_code=destination_country_calling_code, time_delay_hours=0) delayed_message_objs = SMSInviteMessage.objects.filter(country_calling_code=destination_country_calling_code, time_delay_hours__gt=0) for delayed_message in delayed_message_objs: ScheduledSMSInviteMessage.objects.create( invite_sent_time = current_time, scheduled_delivery_time = current_time + datetime.timedelta(hours=delayed_message.time_delay_hours), link_code = link_code, message_template = delayed_message.message_template, time_delay_hours = delayed_message.time_delay_hours, sms_sender_phone_override = None) send_immediate_invite(link_code, immediate_message_obj.message_template, None) track_event(link_code.phone_number.user, 'New User SMS Invite Sent', { 'inviter': link_code.inviting_user.id, 'time_delay_hours': 0}) return link_code