Ejemplo n.º 1
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        # Checking if this intent should play now.
        current_intent = [GET_LOWER_INTENT, GET_HIGHER_INTENT]
        if is_next_intent_error(handler_input=handler_input,
                                current_intent=current_intent):
            return handle_next_intent_error(handler_input=handler_input)

        # Attempts to guess the user number
        alexa_guess = guess_your_number(handler_input=handler_input)

        # Checking if is possible for alexa to guess a number
        if alexa_guess is None:
            speech_text = say.unabletoguess() + say.numberthinkingof()
            next_intent = [GET_NUMBER_INTENT]
        else:
            speech_text = say.guessyournumber(alexa_guess=alexa_guess)
            next_intent = [AMAZON_YES_INTENT, AMAZON_NO_INTENT]

        # Setting the next intent.
        set_next_intent(handler_input=handler_input, next_intent=next_intent)

        # Speaking to the user
        reprompt_text = say.didnothear() + speech_text
        set_prev_msg(handler_input, msg=speech_text)
        handler_input.response_builder.speak(speech_text).ask(
            reprompt_text).set_should_end_session(False)
        return handler_input.response_builder.response
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        # Checking if this intent should play now.
        current_intent = [THIRD_ORDER_INTENT]
        if is_next_intent_error(handler_input = handler_input, current_intent = current_intent):
            return handle_next_intent_error(handler_input = handler_input)   

        speech_text = say.firstorder()
        handler_input.response_builder.speak(speech_text).set_should_end_session(False)
        return handler_input.response_builder.response
Ejemplo n.º 3
0
    def handle(self, handler_input):
        #type: (HandlerInput) -> Response

        # Checking if this intent should play now.
        if is_next_intent_error(handler_input = handler_input, current_intent = [GET_USER_GUESS_INTENT]):
            return handle_next_intent_error(handler_input = handler_input)

        # Setting the next intent.
        set_next_intent(handler_input = handler_input, 
                        next_intent = [GET_USER_GUESS_INTENT, GET_NUMBER_INTENT])

        return guess_alexa_number(handler_input)
Ejemplo n.º 4
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        # Checking if this intent should play now.
        current_intent = [
            GET_GUESS_ALEXA_NUMBER_INTENT, GET_GUESS_MY_NUMBER_INTENT
        ]
        if is_next_intent_error(handler_input=handler_input,
                                current_intent=current_intent):
            return handle_next_intent_error(handler_input=handler_input)

        # Setting game state (to game not in progress)
        handler_input.attributes_manager.session_attributes[
            'game_state'] = GAME_NOT_IN_PROGRESS

        return handler_input.response_builder.add_directive(
            DelegateRequestDirective(
                target='AMAZON.Conversations',
                period={'until':
                        DelegationPeriodUntil.EXPLICIT_RETURN})).response
Ejemplo n.º 5
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        # Checking if this intent should play now.
        if is_next_intent_error(handler_input = handler_input, current_intent = [GET_ATTEMPTS_INTENT]):
            return handle_next_intent_error(handler_input = handler_input) 

        # Getting the attempts from the user.
        attempts = handler_input.request_envelope.request.intent.slots['attempts'].value

         # Validing the attempts the user gives.
        if not attempts or attempts == '0':
            # Setting the next intent.
            set_next_intent(handler_input = handler_input, 
                            next_intent = [GET_ATTEMPTS_INTENT, GET_NUMBER_INTENT])

            # Reprompt user on number of attempts
            speech_text = say.notnumber() + say.getattempts()
            set_prev_msg(handler_input, msg = say.getattempts())
            handler_input.response_builder.speak(speech_text).set_should_end_session(False)
            return handler_input.response_builder.response

        # Getting session attributes.
        session_attr = handler_input.attributes_manager.session_attributes
        
        # Converting attempts(str) to an integer.
        session_attr['max_attempts'] = session_attr['attempts'] = int(attempts)

        # Setting the next intent.
        set_next_intent(handler_input = handler_input, 
                        next_intent = [GET_RANGE_INTENT])

        # Asking the user the range of the numbers in a game. 
        speech_text = say.getrange()
        reprompt_text = say.didnothear() + speech_text
        set_prev_msg(handler_input, msg = speech_text)
        handler_input.response_builder.speak(speech_text).ask(reprompt_text).set_should_end_session(False)
        return handler_input.response_builder.response
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        # Checking if this intent should play now.
        if is_next_intent_error(handler_input=handler_input,
                                current_intent=[AMAZON_NO_INTENT]):
            return handle_next_intent_error(handler_input=handler_input)

        is_prev_low_high_intent = is_prev_intent(
            handler_input, intents=[GET_LOWER_INTENT, GET_HIGHER_INTENT])
        is_prev_range_intent = is_prev_intent(handler_input,
                                              intents=[GET_RANGE_INTENT])
        is_guess_my_number_intent = is_prev_intent(
            handler_input, intents=[GET_GUESS_MY_NUMBER_INTENT])
        is_prev_yes_no_intent = is_prev_intent(
            handler_input, intents=[AMAZON_YES_INTENT, AMAZON_NO_INTENT])
        is_prev_number_intent = is_prev_intent(handler_input,
                                               intents=[GET_NUMBER_INTENT])
        is_prev_user_guess_number_intent = is_prev_intent(
            handler_input, intents=[GET_USER_GUESS_INTENT, GET_NUMBER_INTENT])

        is_game_in_progress = (
            (is_current_game_state(handler_input, state=GAME_IN_PROGRESS)
             or is_current_game_state(handler_input, state=GAME_RESTARTED))
            and is_prev_yes_no_intent)

        if is_prev_low_high_intent or is_guess_my_number_intent or is_game_in_progress:
            # Getting session attributes.
            session_attr = handler_input.attributes_manager.session_attributes

            if session_attr['attempts'] == 0:
                # Setting the next intent.
                set_next_intent(handler_input=handler_input,
                                next_intent=[GET_NUMBER_INTENT])
                speech_text = 'What number were you thinking of?'
                set_prev_msg(handler_input, msg=speech_text)
                reprompt_text = say.didnothear() + speech_text
                handler_input.response_builder.speak(speech_text).ask(
                    reprompt_text).set_should_end_session(False)
                return handler_input.response_builder.response

            # Setting the next intent.
            set_next_intent(handler_input=handler_input,
                            next_intent=[GET_HIGHER_INTENT, GET_LOWER_INTENT])

            set_game_state(handler_input, state=GAME_IN_PROGRESS)

            speech_text = 'Should I go lower or higher?'
            set_prev_msg(handler_input, msg=speech_text)
            reprompt_text = say.didnothear() + speech_text
            handler_input.response_builder.speak(speech_text).ask(
                reprompt_text).set_should_end_session(False)
            return handler_input.response_builder.response

        elif is_prev_yes_no_intent or is_prev_number_intent or is_prev_user_guess_number_intent:
            # Setting the next intent.
            set_next_intent(handler_input=handler_input,
                            next_intent=[
                                GET_GUESS_ALEXA_NUMBER_INTENT,
                                GET_GUESS_MY_NUMBER_INTENT
                            ])

            set_game_state(handler_input, state=GAME_NOT_IN_PROGRESS)

            speech_text = 'What mode would you like to play?'
            reprompt_text = say.didnothear() + speech_text
            set_prev_msg(handler_input, msg=speech_text)
            handler_input.response_builder.speak(speech_text).ask(
                reprompt_text).set_should_end_session(False)
            return handler_input.response_builder.response
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        # Checking if this intent should play now.
        if is_next_intent_error(handler_input = handler_input, current_intent = [GET_NUMBER_INTENT]):
            return handle_next_intent_error(handler_input = handler_input)
        
        # Getting session attributes.
        session_attr = handler_input.attributes_manager.session_attributes

        # Checks if the previous state exists
        is_prev_guess_alexa_my_number_intent = is_prev_intent(handler_input, intents = [GET_GUESS_ALEXA_NUMBER_INTENT, GET_GUESS_MY_NUMBER_INTENT])
        is_prev_lower_or_higher_intent = is_prev_intent(handler_input, intents = [GET_HIGHER_INTENT, GET_LOWER_INTENT])
        is_prev_user_guess_intent = is_prev_intent(handler_input, intents = [GET_USER_GUESS_INTENT, GET_NUMBER_INTENT])
        is_prev_number_intent = is_prev_intent(handler_input, intents = [GET_NUMBER_INTENT])
        is_prev_yes_no_intent = is_prev_intent(handler_input, intents = [AMAZON_YES_INTENT, AMAZON_NO_INTENT])
        is_prev_range_intent = is_prev_intent(handler_input, intents = [GET_RANGE_INTENT])

        if is_prev_guess_alexa_my_number_intent:
            # Getting the attempts from the user.
            attempts = handler_input.request_envelope.request.intent.slots["number"].value

            # Validing the attempts the user gives.
            if not attempts or attempts == '0':
                # Setting the next intent.
                set_next_intent(handler_input = handler_input, 
                                next_intent = [GET_ATTEMPTS_INTENT, GET_NUMBER_INTENT])

                # Reprompt user on number of attempts
                speech_text = say.notnumber() + say.getattempts()
                reprompt_text = say.didnothear() + speech_text
                set_prev_msg(handler_input, msg = say.getattempts())
                handler_input.response_builder.speak(speech_text).ask(reprompt_text).set_should_end_session(False)
                return handler_input.response_builder.response
            
            # Setting the next intent.
            set_next_intent(handler_input = handler_input, 
                            next_intent = [GET_RANGE_INTENT])  

            # Converting attempts(str) to an integer.
            session_attr['max_attempts'] = session_attr['attempts'] = int(attempts)

            # Asking the user the range of the numbers in a game. 
            speech_text = say.getrange()
            reprompt_text = say.didnothear() + speech_text
            set_prev_msg(handler_input, msg = speech_text)
            handler_input.response_builder.speak(speech_text).ask(reprompt_text).set_should_end_session(False)
            return handler_input.response_builder.response

        if is_prev_user_guess_intent or is_prev_range_intent:
            # Setting the next intent.
            set_next_intent(handler_input = handler_input, 
                            next_intent = [GET_USER_GUESS_INTENT, GET_NUMBER_INTENT])

            return guess_alexa_number(handler_input)

        elif is_prev_yes_no_intent or is_prev_number_intent or is_prev_lower_or_higher_intent:
            if session_attr['mode'] == GUESS_ALEXA_NUMBER:
                set_next_intent(handler_input = handler_input, 
                                next_intent = [GET_USER_GUESS_INTENT, GET_NUMBER_INTENT])
                return guess_alexa_number(handler_input)

            elif session_attr['mode'] == GUESS_MY_NUMBER:
                # Getting the attempts from the user.
                user_number = handler_input.request_envelope.request.intent.slots["number"].value
                alexa_guesses = session_attr['alexa_guesses']
                # Validing the attempts the user gives.
                if not user_number:          
                    # Setting the next intent.
                    set_next_intent(handler_input = handler_input, 
                                    next_intent = [GET_NUMBER_INTENT])      
                    speech_text = say.notnumber() + say.numberthinkingof()
                    reprompt_text = say.didnothear() + speech_text
                    set_prev_msg(handler_input, msg = speech_text)                    
                    handler_input.response_builder.speak(speech_text).ask(reprompt_text).set_should_end_session(False)
                    return handler_input.response_builder.response

                # Setting the next intent.
                set_next_intent(handler_input = handler_input, 
                                next_intent = [AMAZON_YES_INTENT, AMAZON_NO_INTENT])
                
                if user_number in alexa_guesses:
                    ordinal_number = convert_to_ordinal_number(alexa_guesses.index(user_number) + 1)
                    speech_text = ('Hey I guessed {} already on my {} attempt '.format(user_number, ordinal_number) +
                                   'Do you want a rematch?')
                    set_prev_msg(handler_input, msg = 'Do you want a rematch?')
                    reprompt_text = say.didnothear() + speech_text
                    handler_input.response_builder.speak(speech_text).ask(reprompt_text).set_should_end_session(False)                    
                    return handler_input.response_builder.response

                speech_text = ('Oh I see! I will get you next time!' + 
                               'Do you want a rematch?')
                reprompt_text = say.didnothear() + speech_text
                set_prev_msg(handler_input, msg = 'Do you want a rematch?')
                handler_input.response_builder.speak(speech_text).ask(reprompt_text).set_should_end_session(False)          
                return handler_input.response_builder.response
Ejemplo n.º 8
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        if is_next_intent_error(handler_input=handler_input,
                                current_intent=[GET_RANGE_INTENT]):
            return handle_next_intent_error(handler_input=handler_input)

        min = handler_input.request_envelope.request.intent.slots["min"].value
        max = handler_input.request_envelope.request.intent.slots["max"].value

        if not min or not max:
            set_next_intent(handler_input=handler_input,
                            next_intent=[GET_RANGE_INTENT])
            speech_text = "You said an invalid range. An example of a valid range is 1 to 10. Please try again. " + say.getattempts(
            )
            reprompt_text = say.didnothear() + speech_text
            set_prev_msg(handler_input, msg=say.getrange())
            handler_input.response_builder.speak(speech_text).ask(
                reprompt_text).set_should_end_session(False)
            return handler_input.response_builder.response

        if min > max:
            min, max = max, min

        session_attr = handler_input.attributes_manager.session_attributes
        session_attr['min'] = int(min)
        session_attr['max'] = int(max)
        session_attr['game_state'] = GAME_IN_PROGRESS
        attempts = session_attr['max_attempts']

        if session_attr['mode'] == GUESS_ALEXA_NUMBER:
            # Setting the next intent.
            set_next_intent(
                handler_input=handler_input,
                next_intent=[GET_NUMBER_INTENT, GET_USER_GUESS_INTENT])
            session_attr["alexa_number"] = randint(int(min), int(max))

            speech_text = say.guessalexanumber(min=min,
                                               max=max,
                                               attempts=attempts)
            reprompt_text = say.didnothear() + speech_text
            set_prev_msg(handler_input, msg=speech_text)
            handler_input.response_builder.speak(speech_text).ask(
                reprompt_text).set_should_end_session(False)
            return handler_input.response_builder.response

        elif session_attr['mode'] == GUESS_MY_NUMBER:
            # Setting the next intent.
            set_next_intent(handler_input=handler_input,
                            next_intent=[AMAZON_YES_INTENT, AMAZON_NO_INTENT])
            session_attr['guessing_range'] = []
            session_attr['position'] = ''
            session_attr['alexa_guesses'] = []

            alexa_guess = guess_your_number(handler_input=handler_input)

            speech_text = (say.guessyournumberprompt(
                attempts=attempts, min=min, max=max) +
                           say.guessyournumber(alexa_guess=alexa_guess))

            reprompt_text = say.didnothear() + speech_text
            set_prev_msg(handler_input, msg=speech_text)
            handler_input.response_builder.speak(speech_text).ask(
                reprompt_text).set_should_end_session(False)
            return handler_input.response_builder.response