def process_pre_registration(msg): """ Returns True if this message was part of the SMS pre-registration workflow (see corehq.apps.sms.models.SelfRegistrationInvitation). Returns False if it's not part of the pre-registration workflow or if the workflow has already been completed. """ invitation = SelfRegistrationInvitation.by_phone(msg.phone_number) if not invitation: return False if any_migrations_in_progress(invitation.domain): raise DelayProcessing() domain_obj = Domain.get_by_name(invitation.domain, strict=True) if not domain_obj.sms_mobile_worker_registration_enabled: return False text = msg.text.strip() if is_registration_text(text): # Return False to let the message be processed through the SMS # registration workflow return False elif invitation.phone_type: # If the user has already indicated what kind of phone they have, # but is still replying with sms, then just resend them the # appropriate registration instructions if invitation.phone_type == SelfRegistrationInvitation.PHONE_TYPE_ANDROID: invitation.send_step2_android_sms() elif invitation.phone_type == SelfRegistrationInvitation.PHONE_TYPE_OTHER: invitation.send_step2_java_sms() return True elif text == '1': invitation.phone_type = SelfRegistrationInvitation.PHONE_TYPE_ANDROID invitation.save() invitation.send_step2_android_sms() return True elif text == '2': invitation.phone_type = SelfRegistrationInvitation.PHONE_TYPE_OTHER invitation.save() invitation.send_step2_java_sms() return True else: invitation.send_step1_sms() return True
def process_sms_registration(msg): """ This method handles registration via sms. Returns True if a contact was registered, False if not. To have a case register itself, do the following: 1) Select "Enable Case Registration Via SMS" in project settings, and fill in the associated Case Registration settings. 2) Text in "join <domain>", where <domain> is the domain to join. If the sending number does not exist in the system, a case will be registered tied to that number. The "join" keyword can be any keyword in REGISTRATION_KEYWORDS. This is meant to support multiple translations. To have a mobile worker register itself, do the following: 1) Select "Enable Mobile Worker Registration via SMS" in project settings. 2) Text in "join <domain> worker <username>", where <domain> is the domain to join and <username> is the requested username. If the username doesn't exist it will be created, otherwise the registration will error. If the username argument is not specified, the username will be the mobile number The "join" and "worker" keywords can be any keyword in REGISTRATION_KEYWORDS and REGISTRATION_MOBILE_WORKER_KEYWORDS, respectively. This is meant to support multiple translations. """ registration_processed = False text_words = msg.text.upper().split() keyword1 = text_words[0] if len(text_words) > 0 else "" keyword2 = text_words[1].lower() if len(text_words) > 1 else "" keyword3 = text_words[2] if len(text_words) > 2 else "" keyword4 = text_words[3] if len(text_words) > 3 else "" cleaned_phone_number = strip_plus(msg.phone_number) if is_registration_text(msg.text) and keyword2 != "": domain_name = keyword2 if any_migrations_in_progress(domain_name): raise DelayProcessing() domain_obj = Domain.get_by_name(domain_name, strict=True) if domain_obj is not None: if domain_has_privilege(domain_obj, privileges.INBOUND_SMS): if (keyword3 in REGISTRATION_MOBILE_WORKER_KEYWORDS and domain_obj.sms_mobile_worker_registration_enabled): if keyword4 != '': username = keyword4 else: username = cleaned_phone_number try: user_data = {} invitation = SelfRegistrationInvitation.by_phone( msg.phone_number) if invitation: invitation.completed() user_data = invitation.custom_user_data username = process_username(username, domain_obj) password = random_password() new_user = CommCareUser.create(domain_obj.name, username, password, user_data=user_data) new_user.add_phone_number(cleaned_phone_number) new_user.save() entry = new_user.get_or_create_phone_entry( cleaned_phone_number) entry.set_two_way() entry.set_verified() entry.save() registration_processed = True if domain_obj.enable_registration_welcome_sms_for_mobile_worker: send_sms( domain_obj.name, None, cleaned_phone_number, get_message( MSG_REGISTRATION_WELCOME_MOBILE_WORKER, domain=domain_obj.name)) except ValidationError as e: send_sms(domain_obj.name, None, cleaned_phone_number, e.messages[0]) elif domain_obj.sms_case_registration_enabled: register_sms_contact( domain=domain_obj.name, case_type=domain_obj.sms_case_registration_type, case_name="unknown", user_id=domain_obj.sms_case_registration_user_id, contact_phone_number=cleaned_phone_number, contact_phone_number_is_verified="1", owner_id=domain_obj.sms_case_registration_owner_id, ) registration_processed = True if domain_obj.enable_registration_welcome_sms_for_case: send_sms( domain_obj.name, None, cleaned_phone_number, get_message(MSG_REGISTRATION_WELCOME_CASE, domain=domain_obj.name)) msg.domain = domain_obj.name msg.save() return registration_processed
def process_sms_registration(msg): """ This method handles registration via sms. Returns True if a contact was registered, False if not. To have a case register itself, do the following: 1) Select "Enable Case Registration Via SMS" in project settings, and fill in the associated Case Registration settings. 2) Text in "join <domain>", where <domain> is the domain to join. If the sending number does not exist in the system, a case will be registered tied to that number. The "join" keyword can be any keyword in REGISTRATION_KEYWORDS. This is meant to support multiple translations. To have a mobile worker register itself, do the following: 1) Select "Enable Mobile Worker Registration via SMS" in project settings. 2) Text in "join <domain> worker <username>", where <domain> is the domain to join and <username> is the requested username. If the username doesn't exist it will be created, otherwise the registration will error. If the username argument is not specified, the username will be the mobile number The "join" and "worker" keywords can be any keyword in REGISTRATION_KEYWORDS and REGISTRATION_MOBILE_WORKER_KEYWORDS, respectively. This is meant to support multiple translations. """ registration_processed = False text_words = msg.text.upper().split() keyword1 = text_words[0] if len(text_words) > 0 else "" keyword2 = text_words[1].lower() if len(text_words) > 1 else "" keyword3 = text_words[2] if len(text_words) > 2 else "" keyword4 = text_words[3] if len(text_words) > 3 else "" cleaned_phone_number = strip_plus(msg.phone_number) if is_registration_text(msg.text) and keyword2 != "": domain = Domain.get_by_name(keyword2, strict=True) if domain is not None: if domain_has_privilege(domain, privileges.INBOUND_SMS): if keyword3 in REGISTRATION_MOBILE_WORKER_KEYWORDS and domain.sms_mobile_worker_registration_enabled: if keyword4 != '': username = keyword4 else: username = cleaned_phone_number try: user_data = {} invitation = SelfRegistrationInvitation.by_phone(msg.phone_number) if invitation: invitation.completed() user_data = invitation.custom_user_data username = process_username(username, domain) password = random_password() new_user = CommCareUser.create(domain.name, username, password, user_data=user_data) new_user.add_phone_number(cleaned_phone_number) new_user.save_verified_number(domain.name, cleaned_phone_number, True, None) new_user.save() registration_processed = True if domain.enable_registration_welcome_sms_for_mobile_worker: send_sms(domain.name, None, cleaned_phone_number, get_message(MSG_REGISTRATION_WELCOME_MOBILE_WORKER, domain=domain.name)) except ValidationError as e: send_sms(domain.name, None, cleaned_phone_number, e.messages[0]) elif domain.sms_case_registration_enabled: register_sms_contact( domain=domain.name, case_type=domain.sms_case_registration_type, case_name="unknown", user_id=domain.sms_case_registration_user_id, contact_phone_number=cleaned_phone_number, contact_phone_number_is_verified="1", owner_id=domain.sms_case_registration_owner_id, ) registration_processed = True if domain.enable_registration_welcome_sms_for_case: send_sms(domain.name, None, cleaned_phone_number, get_message(MSG_REGISTRATION_WELCOME_CASE, domain=domain.name)) msg.domain = domain.name msg.save() return registration_processed