Beispiel #1
0
def admin_reset_user_pin(user: User):
    pin_reset_token = user.encode_single_use_JWS('R')
    user.save_pin_reset_token(pin_reset_token)
    user.failed_pin_attempts = 0

    pin_reset_message = i18n_for(user, "general_sms.pin_reset")
    send_message(user.phone, pin_reset_message)
Beispiel #2
0
def send_onboarding_sms_messages(user):

    # First send the intro message
    organisation = getattr(g, 'active_organisation',
                           None) or user.default_organisation

    intro_message = i18n_for(
        user,
        "general_sms.welcome.{}".format(organisation.custom_welcome_message_key
                                        or 'generic'),
        first_name=user.first_name,
        balance=rounded_dollars(user.transfer_account.balance),
        token=user.transfer_account.token.name)

    send_message(user.phone, intro_message)

    send_terms_message_if_required(user)
 def send_sms(self, message_key, **kwargs):
     # if we use directory listing similarly for other countries later, can generalize country to init
     message = i18n_for(self.recipient, "ussd.kenya.{}".format(message_key),
                        **kwargs)
     message_processor.send_message(self.recipient.phone, message)
Beispiel #4
0
def menu_display_text_in_lang(current_menu: UssdMenu,
                              user: Optional[User]) -> str:
    return i18n_for(user, current_menu.display_key)
 def send_sms(user, message_key, **kwargs):
     # if we use token processor similarly for other countries later, can generalize country to init
     message = i18n_for(user, "ussd.kenya.{}".format(message_key), **kwargs)
     message_processor.send_message(user.phone, message)
 def send_sms(self, phone, message_key, **kwargs):
     message = i18n_for(self.user, "ussd.kenya.{}".format(message_key), **kwargs)
     message_processor.send_message(phone, message)
    def custom_display_text(menu: UssdMenu, ussd_session: UssdSession) -> str:
        """
        Many USSD responses include user-specific data that is stored inside the USSD session. This function
        extracts the appropriate session data based on the current menu name and then inserts them as keywords in the
        i18n function.
        :param menu: The USSD menu to create a text response for
        :param ussd_session: The ussd session containing user data
        :return: raw ussd menu text string
        """

        user = ussd_session.user

        if menu.name == 'about_me':
            bio = next(filter(lambda x: x.name == 'bio', user.custom_attributes), None)
            first_name = user.first_name
            last_name = user.last_name
            gender = next(filter(lambda x: x.name == 'gender', user.custom_attributes), None)
            location = user.location

            if bio and gender:
                bio_text = bio.value.strip('"')
                gender_text = gender.value.strip('"')
            else:
                bio_text = None
                gender_text = None

            # translations
            absent_value_placeholder = "missing"
            if user.preferred_language == "sw":
                absent_value_placeholder = 'hakuna'
                if gender_text == 'male':
                    gender_text = 'mwanaume'
                elif gender_text == 'female':
                    gender_text = 'mwanamke'

            if first_name == 'Unknown first name':
                first_name = None

            if last_name == 'Unknown last name':
                last_name = None

            if bio_text == 'Unknown business':
                bio_text = None

            if gender_text == 'Unknown gender':
                gender_text = None

            if location == 'Unknown location':
                location = None

            # define final values to show in menu
            first_name = first_name or absent_value_placeholder
            last_name = last_name or absent_value_placeholder
            bio_text = bio_text or absent_value_placeholder
            gender_text = gender_text or absent_value_placeholder
            location = location or absent_value_placeholder

            full_name = "{} {}".format(first_name, last_name)
            if first_name == absent_value_placeholder and last_name == absent_value_placeholder:
                full_name = absent_value_placeholder

            return i18n_for(user, "{}.profile".format(menu.display_key),
                            full_name=full_name,
                            gender=gender_text,
                            location=location, user_bio=bio_text)

        if menu.name == 'send_token_pin_authorization':
            recipient = get_user_by_phone(ussd_session.get_data('recipient_phone'), 'KE', True)
            other_user_details = recipient.user_details()
            user_details = user.user_details()
            token = default_token(user)
            transaction_amount = ussd_session.get_data('transaction_amount')
            if user.failed_pin_attempts > 0:
                return i18n_for(
                    user=user,
                    key="{}.{}".format(menu.display_key, 'retry'),
                    remaining_attempts=3 - user.failed_pin_attempts
                )
            else:
                return i18n_for(
                    user=user,
                    key="{}.{}".format(menu.display_key, 'first'),
                    transaction_amount=cents_to_dollars(transaction_amount),
                    token_name=token.symbol,
                    other_user_details=other_user_details,
                    user_details=user_details

                )

        if menu.name == 'exit_successful_send_token':
            recipient = get_user_by_phone(ussd_session.get_data('recipient_phone'), 'KE', True)
            other_user_details = recipient.user_details()
            user_details = user.user_details()
            token = default_token(user)
            transaction_amount = ussd_session.get_data('transaction_amount')
            return i18n_for(
                user=user,
                key="{}".format(menu.display_key, 'exit_successful_send_token'),
                transaction_amount=cents_to_dollars(transaction_amount),
                token_name=token.symbol,
                other_user_details=other_user_details,
                user_details=user_details

            )

        if menu.name == 'exchange_token_confirmation':
            agent = get_user_by_phone(ussd_session.get_data('agent_phone'), 'KE', True)
            agent_phone = agent.user_details()
            token = default_token(user)
            exchange_amount = ussd_session.get_data('exchange_amount')
            return i18n_for(
                user, menu.display_key,
                agent_phone=agent_phone,
                token_name=token.symbol,
                exchange_amount=cents_to_dollars(exchange_amount)
            )

        # in matching is scary since it might pick up unintentional ones
        if 'exit' in menu.name or 'help' == menu.name:
            return i18n_for(
                user, menu.display_key,
                support_phone='+254757628885'
            )

        # in matching is scary since it might pick up unintentional ones
        if 'pin_authorization' in menu.name or 'current_pin' == menu.name:
            if user.failed_pin_attempts is not None and user.failed_pin_attempts > 0:
                return i18n_for(
                    user, "{}.retry".format(menu.display_key),
                    remaining_attempts=3 - user.failed_pin_attempts
                )
            else:
                return i18n_for(user, "{}.first".format(menu.display_key))

        if menu.name == 'directory_listing' or menu.name == 'send_token_reason':

            blank_template = i18n_for(
                user, menu.display_key, options=''
            )

            blank_len = len(blank_template)

            most_relevant_usages = ussd_session.get_data('transfer_usage_mapping')

            options = KenyaUssdProcessor.fit_usages(
                ussd_session,
                most_relevant_usages,
                blank_len,
                user,
                0,
                [0]
            )

            # current_usages = most_relevant_usages[:ITEMS_PER_MENU]
            return i18n_for(
                user, menu.display_key,
                options=options
            )

        if menu.name == 'directory_listing_other' or menu.name == 'send_token_reason_other':

            most_relevant_usages = ussd_session.get_data('transfer_usage_mapping')
            usage_menu_nr = ussd_session.get_data('usage_menu')
            usage_stack = ussd_session.get_data('usage_index_stack') or [0]

            start_of_list = usage_stack[usage_menu_nr]

            total_usages = len(most_relevant_usages)

            # First see if we can fit remaining usages onto the one page
            if start_of_list + ITEMS_PER_MENU > total_usages:
                part = 'first' if start_of_list == 0 else 'last'
                current_usages = most_relevant_usages[start_of_list:total_usages]
                menu_options = KenyaUssdProcessor.create_usages_list(current_usages, user)

                translated_menu = i18n_for(
                    user, "{}.{}".format(menu.display_key, part),
                    other_options=menu_options
                )

                if len(translated_menu) <= USSD_MAX_LENGTH:
                    return translated_menu

            # Oh well, guess we just have to fit as many as possible then

            part = 'first' if start_of_list == 0 else 'middle'

            blank_template = i18n_for(
                user, "{}.{}".format(menu.display_key, part),
                other_options=''
            )

            blank_len = len(blank_template)

            options = KenyaUssdProcessor.fit_usages(
                ussd_session,
                most_relevant_usages,
                blank_len,
                user,
                start_of_list,
                usage_stack)

            # current_usages = most_relevant_usages[:ITEMS_PER_MENU]
            return i18n_for(
                user, "{}.{}".format(menu.display_key, part),
                other_options=options
            )

        return i18n_for(user, menu.display_key)
Beispiel #8
0
def send_sms(user, message_key):
    message = i18n_for(user, "user.{}".format(message_key))
    send_message(user.phone, message)
Beispiel #9
0
def send_terms_message_if_required(user):

    if not user.seen_latest_terms:
        terms_message = i18n_for(user, "general_sms.terms")
        send_message(user.phone, terms_message)
        user.seen_latest_terms = True