示例#1
0
    def handle(self, handler_input):
        i18n = Constants.i18n
        telethon_service = TelethonService()
        sess_attrs = handler_input.attributes_manager.session_attributes
        user_is_authorized = sess_attrs.get("ACCOUNT").get("AUTHORIZED")
        account = sess_attrs.get("ACCOUNT")
        slots = handler_input.request_envelope.request.intent.slots
        reprompt = None

        if user_is_authorized:
            speech_text = i18n.ALREADY_AUTHORIZED
            handler_input.response_builder.speak(speech_text) \
                .set_should_end_session(False).ask(i18n.FALLBACK)
            return handler_input.response_builder.response
        if not account.get("PHONE_NUMBER"):
            speech_text = i18n.NO_PHONE
            should_end = True
        elif not slots.get("code").value:
            try:
                phone_code_hash = telethon_service.send_code_request()
            except TelethonException as error:
                return handle_telethon_error_response(
                    error, handler_input).response_builder.response

            sess_attrs["PHONE_CODE_HASH"] = phone_code_hash

            updated_intent = Intent("AuthorizationIntent", slots)
            elicit_directive = ElicitSlotDirective(updated_intent, "code")
            handler_input.response_builder.add_directive(elicit_directive)

            speech_text = i18n.WHAT_IS_CODE
            reprompt = i18n.WHAT_IS_CODE_REPROMPT
            should_end = False
        else:
            phone_code_hash = sess_attrs.get("PHONE_CODE_HASH")
            try:
                ok = telethon_service.sign_user_in(
                    slots.get("code").value, phone_code_hash)
            except TelethonException as error:
                return handle_telethon_error_response(
                    error, handler_input).response_builder.response

            if ok:
                sess_attrs["ACCOUNT"]["AUTHORIZED"] = True
                speech_text = i18n.AUTHORIZED
                reprompt = i18n.FALLBACK
                should_end = False
            else:
                speech_text = i18n.WRONG_CODE
                should_end = True

        handler_input.response_builder.speak(speech_text) \
            .set_should_end_session(should_end)
        if reprompt:
            handler_input.response_builder.ask(reprompt)

        return handler_input.response_builder.response
示例#2
0
    def handle(self, handler_input):
        i18n = Constants.i18n
        sess_attrs = handler_input.attributes_manager.session_attributes

        if not Constants.ACCESS_TOKEN:
            speech_text = i18n.ACCOUNT_LINKING_REQUIRED
            handler_input.response_builder.speak(
                speech_text).set_should_end_session(True)
            sess_attrs["LINK_ACCOUNT_CARD"] = True
            return handler_input.response_builder.response

        user_is_authorized = sess_attrs.get("ACCOUNT").get("AUTHORIZED")
        telethon_service = TelethonService()

        if user_is_authorized:
            try:
                user_has_telegrams = telethon_service.check_telegrams()
            except TelethonException as error:
                return handle_telethon_error_response(error, handler_input)

            if user_has_telegrams:
                speech_text = i18n.USER_HAS_TELEGRAMS
            else:
                speech_text = i18n.WELCOME
        else:
            speech_text = i18n.NOT_AUTHORIZED

        handler_input.response_builder.speak(speech_text) \
            .set_should_end_session(False).ask(i18n.FALLBACK)
        return handler_input.response_builder.response
示例#3
0
    def handle(self, handler_input):
        i18n = Constants.i18n
        sess_attrs = handler_input.attributes_manager.session_attributes
        previous_intent = sess_attrs.get("PREV_INTENT")
        user_is_authorized = sess_attrs.get("ACCOUNT").get("AUTHORIZED")

        # User answered No on question: "Welcome, do you want to hear your new Telegrams?"
        if previous_intent == "LaunchIntent" and user_is_authorized:
            speech_text = i18n.get_random_ack() + ", " + i18n.HELP_USER
            handler_input.response_builder.speak(speech_text) \
                .set_should_end_session(False).ask(i18n.FALLBACK)
            return handler_input.response_builder.response

        # User answered No on question: "Is there anything else I can help you with?
        if (previous_intent == "SendIntent"
                or previous_intent == "MessageIntent"
                or previous_intent == "SpeedIntent"
                or previous_intent == "ReplyIntent"
                or previous_intent == "SettingsIntent"
                or previous_intent == "AuthorizationIntent"
                or previous_intent == "AMAZON.YesIntent"
                or previous_intent == "AMAZON.NoIntent") \
                and not sess_attrs.get("TELEGRAMS") and not sess_attrs.get('FIRST_NAMES'):
            speech_text = i18n.get_random_ack(
            ) + ", " + i18n.get_random_goodbye()
            handler_input.response_builder.speak(
                speech_text).set_should_end_session(True)
            return handler_input.response_builder.response

        # User answered with "Yes" on "What is the telegram for Lorenz?"
        if (previous_intent == "SendIntent" or previous_intent == "SpeedIntent"
                and sess_attrs.get("TELETHON_ENTITY_ID")):
            try:
                speech_text, reprompt = send_telegram(i18n.NO, sess_attrs,
                                                      i18n)
                handler_input.response_builder.speak(speech_text) \
                    .set_should_end_session(False).ask(reprompt)
                return handler_input.response_builder.response
            except TelethonException as error:
                return handle_telethon_error_response(error, handler_input)

        if (previous_intent == "SendIntent" or previous_intent == "SpeedIntent"
                or previous_intent == "MessageIntent"
                or previous_intent == "ReplyIntent"
                or previous_intent == "AuthorizationIntent"):
            speech_text = i18n.get_random_ack(
            ) + ", " + i18n.get_random_goodbye()
            handler_input.response_builder.speak(
                speech_text).set_should_end_session(True)
            return handler_input.response_builder.response
示例#4
0
    def get_telegram(self, handler_input):
        sess_attrs = handler_input.attributes_manager.session_attributes
        i18n = Constants.i18n

        if not sess_attrs.get("TELEGRAMS"):
            try:
                conversations = self.telethon_service.get_conversations(i18n)
            except TelethonException as error:
                return handle_telethon_error_response(error, handler_input)

            if len(conversations) == 0:
                speech_text = i18n.NO_TELEGRAMS
                return speech_text

            first_names = self.get_first_names(conversations, i18n)
            contacts = [telegram.sender for telegram in conversations]
            entity_ids = [telegram.entity_id for telegram in conversations]
            spoken_telegrams = self.spoken_telegrams(conversations, i18n)

            sess_attrs["TELEGRAMS"] = spoken_telegrams
            sess_attrs["TELEGRAMS_COUNTER"] = 0
            sess_attrs["CONTACTS"] = contacts
            sess_attrs["ENTITY_IDS"] = entity_ids

            speech_text = i18n.NEW_TELEGRAMS + first_names
            speech_text = speech_text + \
                spoken_telegrams[sess_attrs["TELEGRAMS_COUNTER"]]

            if len(conversations) == 1:
                speech_text += i18n.REPLY_SEND_OR_STOP
                sess_attrs.pop("TELEGRAMS")
                sess_attrs.pop("TELEGRAMS_COUNTER")
            else:
                speech_text += i18n.REPLY_OR_NEXT_TELEGRAM
                sess_attrs["TELEGRAMS_COUNTER"] += 1

        elif sess_attrs["TELEGRAMS_COUNTER"] < len(
                sess_attrs["TELEGRAMS"]) - 1:
            speech_text = sess_attrs["TELEGRAMS"][
                sess_attrs["TELEGRAMS_COUNTER"]]
            speech_text = speech_text + i18n.REPLY_OR_NEXT_TELEGRAM
            sess_attrs["TELEGRAMS_COUNTER"] += 1
        else:
            speech_text = sess_attrs["TELEGRAMS"][
                sess_attrs["TELEGRAMS_COUNTER"]]
            speech_text += i18n.REPLY_SEND_OR_STOP
            sess_attrs.pop("TELEGRAMS")
            sess_attrs.pop("TELEGRAMS_COUNTER")
        return speech_text
示例#5
0
    def handle(self, handler_input):
        sess_attrs = handler_input.attributes_manager.session_attributes
        slots = handler_input.request_envelope.request.intent.slots
        updated_intent = Intent("SpeedIntent", slots)
        i18n = Constants.i18n

        for slot_name in slots:
            slot = slots[slot_name]
            if slot_name == "speed_dial_number" and slots.get(
                    "message").value is None:
                if slot.value is None:
                    slot_to_elicit = "speed_dial_number"
                    speech_text = i18n.SPEED_DIAL
                    reprompt = i18n.SPEED_DIAL_REPROMPT
                else:
                    num = int(slot.value)
                    try:
                        speech_text, reprompt, slot_to_elicit = handle_speed_dial_number_input(
                            num, sess_attrs, i18n)
                    except SpeedDialException as error:
                        return handler_input.response_builder \
                            .speak(error.args[0]).set_should_end_session(True).response

                directive = ElicitSlotDirective(updated_intent, slot_to_elicit)
                handler_input.response_builder.add_directive(directive)

            if slot.name == "message" and slot.value:
                try:
                    m = slot.value
                    speech_text, reprompt = send_telegram(m, sess_attrs, i18n)
                except TelethonException as error:
                    return handle_telethon_error_response(error, handler_input)

        handler_input.response_builder.speak(speech_text) \
            .set_should_end_session(False).ask(reprompt)
        return handler_input.response_builder.response
示例#6
0
    def handle(self, handler_input):
        try:
            check_for_account(handler_input)
        except AccountException as error:
            return handler_input.response_builder \
                .speak(error.args[0]).set_should_end_session(True).response

        slots = handler_input.request_envelope.request.intent.slots
        updated_intent = Intent("SendIntent", slots)
        sess_attrs = handler_input.attributes_manager.session_attributes
        locale = handler_input.request_envelope.request.locale
        i18n = Constants.i18n

        for slot_name in slots:
            slot = slots[slot_name]
            if slot_name == "first_name" and slots["message"].value is None:
                if slot.value is None:
                    slot_to_elicit = "first_name"
                    speech_text = i18n.FIRST_NAME
                    reprompt = i18n.FIRST_NAME_REPROMPT
                else:
                    if not sess_attrs.get("FIRST_NAMES"):
                        if locale == 'de-DE':
                            # On German Alexa we get "vier" if user says a speed dial number
                            # On English Alexa we get "4"... --> need that also for German
                            slot.value = parse_spoken_numbers_to_integers(slot.value)

                        is_speed_dial = slot.value.isdigit()

                        if is_speed_dial:
                            try:
                                num = int(slot.value)
                                speech_text, reprompt, slot_to_elicit = \
                                    handle_speed_dial_number_input(
                                        num, sess_attrs, i18n)
                            except SpeedDialException as error:
                                return handler_input.response_builder \
                                    .speak(error.args[0]).set_should_end_session(True).response
                        else:
                            speech_text, reprompt, slot_to_elicit = self \
                                .handle_first_name_input(slot.value, sess_attrs, i18n)
                    else:
                        one_two_or_three = slots.get("one_two_or_three")

                        if one_two_or_three.value not in ["1", "2", "3"]:
                            speech_text = i18n.MAX_NO_CONTACT
                            handler_input.response_builder\
                                .speak(speech_text).set_should_end_session(True)
                            return handler_input.response_builder.response

                        speech_text, reprompt = self \
                            .get_users_choice(one_two_or_three, i18n, sess_attrs)
                        slot_to_elicit = "message"

                directive = ElicitSlotDirective(updated_intent, slot_to_elicit)
                handler_input.response_builder.add_directive(directive)

            if slot.name == "message" and slot.value:
                try:
                    m = slot.value
                    speech_text, reprompt = send_telegram(m, sess_attrs, i18n)
                except TelethonException as error:
                    return handle_telethon_error_response(error, handler_input)

        handler_input.response_builder.speak(speech_text) \
            .set_should_end_session(False).ask(reprompt)

        return handler_input.response_builder.response