Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
0
def send_telegram(message, sess_attrs, i18n):
    entity_id = sess_attrs.get("TELETHON_ENTITY_ID")
    TelethonService().send_telegram(entity_id, message)
    speech_text = i18n.get_random_anyting_else()
    reprompt = i18n.FALLBACK
    sess_attrs.clear()
    return speech_text, reprompt
Ejemplo n.º 4
0
def handle_speed_dial_number_input(number, sess_attrs, i18n):
    dt_service = DailyTelegramsService()
    telethon_service = TelethonService()

    slot_to_elicit = "message"
    first_name = dt_service.get_firstname_for_speed_dial_number(number)
    if first_name:
        contacts = telethon_service.get_potential_contacts(first_name)

        if len(contacts) > 1:
            error_message = i18n.MULTIPLE_TELEGRAM_CONTACTS_FOR_SPEED_DIAL
            raise SpeedDialException(error_message)

        c = contacts[0]
        speech_text = i18n.MESSAGE.format(c.first_name)
        reprompt = i18n.MESSAGE.format(c.first_name)

        sess_attrs["FIRST_NAMES"] = [c.first_name]
        sess_attrs["TELETHON_ENTITY_ID"] = c.entity_id

        return (speech_text, reprompt, slot_to_elicit)
    else:
        error_message = i18n.NO_SPEED_DIAL_CONTACT
        raise SpeedDialException(error_message)
Ejemplo n.º 5
0
    def handle(self, handler_input):
        self.telethon_service = TelethonService()
        sess_attrs = handler_input.attributes_manager.session_attributes
        i18n = Constants.i18n
        slots = handler_input.request_envelope.request.intent.slots

        if not sess_attrs.get("ENTITY_IDS"):
            speech_text = i18n.NO_TELETHON_ID
            return handler_input.response_builder.speak(speech_text) \
            .set_should_end_session(False).ask(i18n.FALLBACK).response

        if slots.get("message").value:
            if sess_attrs.get("TELEGRAMS_COUNTER") is None:
                # User is replying on last unread dialog
                index = len(sess_attrs.get("CONTACTS")) - 1
            else:
                # User is replying to some other dialog
                index = sess_attrs.get("TELEGRAMS_COUNTER") - 1

            contact = sess_attrs.get("CONTACTS")[index]
            entity_id = sess_attrs.get("ENTITY_IDS")[index]
            self.telethon_service \
                .send_telegram(entity_id, slots.get("message").value)

            next_telegram = MessageIntentHandler().get_telegram(handler_input)
            speech_text = i18n.TELEGRAM_SENT.format(contact) + next_telegram
            reprompt = i18n.FALLBACK
        else:
            speech_text = i18n.get_random_acceptance_ack(
            ) + ", " + i18n.MESSAGE_2
            reprompt = i18n.get_random_dont_understand(
            ) + ", " + i18n.MESSAGE_2
            updated_intent = Intent("ReplyIntent", slots)
            elicit_directive = ElicitSlotDirective(updated_intent, "message")
            handler_input.response_builder.add_directive(elicit_directive)

        handler_input.response_builder.speak(speech_text) \
            .set_should_end_session(False).ask(reprompt)
        return handler_input.response_builder.response
Ejemplo n.º 6
0
 def __init__(self):
     self.telethon_service = TelethonService()
Ejemplo n.º 7
0
class MessageIntentHandler(AbstractRequestHandler):
    def __init__(self):
        self.telethon_service = TelethonService()

    def can_handle(self, handler_input):
        return is_intent_name("MessageIntent")(handler_input)

    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

        i18n = Constants.i18n
        speech_text = self.get_telegram(handler_input)
        handler_input.response_builder.speak(speech_text) \
            .set_should_end_session(False).ask(i18n.FALLBACK)
        return handler_input.response_builder.response

    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

    def get_first_names(self, conversations, i18n):
        first_names = []

        # Don't loop over last, because we add an 'and' for the voice output
        if len(conversations) > 1:
            for telegram in conversations[:-1]:
                first_names.append(telegram.sender)
            first_names = ", ".join(first_names)
            first_names += i18n.AND + \
                conversations[-1].sender + ". " + i18n.BREAK_BETWEEN_NAMES
        else:
            first_names = conversations[0].sender + \
                ". " + i18n.BREAK_BETWEEN_NAMES

        # Constructs a string like: "Tom, Paul, and Julia"
        return first_names

    def spoken_telegrams(self, conversations, i18n):
        texts = []

        for conversation in conversations:
            if conversation.is_group:
                speech_text = i18n.GROUP_INTRO.format(conversation.sender)
            else:
                speech_text = i18n.PERSONAL_CHAT_INTRO.format(
                    conversation.sender)

            telegrams = " ".join(conversation.telegrams)
            speech_text += telegrams

            texts.append(speech_text)

        return texts
Ejemplo n.º 8
0
class SendIntentHandler(AbstractRequestHandler):
    def __init__(self):
        self.telethon_service = TelethonService()

    def can_handle(self, handler_input):
        if is_intent_name("SendIntent")(handler_input):
            slots = handler_input.request_envelope.request.intent.slots
            if slots.get('message').value and not slots.get('first_name').value:
                return False
            return True

    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

    def provide_choices(self, contacts, i18n):
        name_1 = contacts[0].first_name
        name_2 = contacts[1].first_name

        if len(contacts) == 3:
            name_3 = contacts[2].first_name
            speech_text = i18n.NO_CONTACT.format(name_1, name_2, name_3)
            reprompt = i18n.NO_CONTACT_REPROMPT.format(name_1, name_2, name_3)
        else:
            speech_text = i18n.NO_CONTACT_2.format(name_1, name_2)
            reprompt = i18n.NO_CONTACT_REPROMPT_2.format(name_1, name_2)
        return speech_text, reprompt

    def get_users_choice(self, one_two_or_three, i18n, sess_attrs):
        index = int(one_two_or_three.value) - 1
        telethon_id = sess_attrs.get("TELETHON_IDS")[index]
        sess_attrs["TELETHON_ENTITY_ID"] = telethon_id

        name = sess_attrs["FIRST_NAMES"][index]
        speech_text = i18n.MESSAGE.format(name)
        reprompt = i18n.MESSAGE_REPROMPT.format(name)
        return speech_text, reprompt

    def handle_first_name_input(self, first_name, sess_attrs, i18n):
        contacts = self.telethon_service.get_potential_contacts(first_name)

        if len(contacts) == 1:
            c = contacts[0]
            slot_to_elicit = "message"
            sess_attrs["TELETHON_ENTITY_ID"] = c.entity_id
            speech_text = i18n.MESSAGE.format(c.first_name)
            reprompt = i18n.MESSAGE_REPROMPT.format(c.first_name)
        else:
            slot_to_elicit = "one_two_or_three"
            speech_text, reprompt = self.provide_choices(contacts, i18n)
            sess_attrs["FIRST_NAMES"] = [c.first_name for c in contacts]
            sess_attrs["TELETHON_IDS"] = [c.entity_id for c in contacts]

        return (speech_text, reprompt, slot_to_elicit)
Ejemplo n.º 9
0
 def __init__(self):
     self.telethon_service = TelethonService()
     self.daily_telegrams_service = DailyTelegramsService()