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. 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)
def handle(self, handler_input): # type: (HandlerInput) -> Response # Setting the next intent. set_next_intent(handler_input=handler_input, next_intent=[FIRST_ORDER_INTENT], is_launch_request=True) # Welcoming the user. speech_text = say.welcome() 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
def handle(self, handler_input): # type: (HandlerInput) -> Response # Getting session attributes. session_attr = handler_input.attributes_manager.session_attributes api_request = handler_input.request_envelope.request.api_request min = api_request.arguments['min'] max = api_request.arguments['max'] attempts = api_request.arguments['attempts'] session_attr['min'] = int(min) session_attr['max'] = int(max) session_attr['game_state'] = GAME_IN_PROGRESS if session_attr['mode'] == GUESS_ALEXA_NUMBER: # # Setting the next intent. set_next_intent(handler_input=handler_input, next_intent=[GET_NUMBER_INTENT]) session_attr["alexa_number"] = randint(int(min) - 1, int(max)) elif session_attr['mode'] == GUESS_MY_NUMBER: 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'] = [] # Converting attempts(str) to an integer. session_attr['max_attempts'] = session_attr['attempts'] = int(attempts) return { 'directives': [{ 'type': 'Dialog.DelegateRequest', 'target': 'skill', 'period': { 'until': 'EXPLICIT_RETURN' }, 'updatedRequest': { 'type': 'IntentRequest', 'intent': { 'name': 'BeginGameIntent', } } }], 'apiResponse': {} }
def handle(self, handler_input): # type: (HandlerInput) -> Response # Checking if this intent should play now. current_intent = [FIRST_ORDER_INTENT] if is_next_intent_error(handler_input=handler_input, current_intent=current_intent): return handle_next_intent_error(handler_input=handler_input) # Setting the next intent. set_next_intent(handler_input=handler_input, next_intent=[SECOND_ORDER_INTENT]) speech_text = say.firstorder() handler_input.response_builder.speak( speech_text).set_should_end_session(False) return handler_input.response_builder.response
def handle(self, handler_input): # type: (HandlerInput) -> Response # Setting the next intent. set_next_intent(handler_input=handler_input, next_intent=[ GET_GUESS_ALEXA_NUMBER_INTENT, GET_GUESS_MY_NUMBER_INTENT ], is_launch_request=True) # Welcoming the user. speech_text = say.welcome() reprompt_text = say.didnothear( ) + 'Just say, "guess alexa\'s number", if you want to guess alexa\'s number... or say "guess my number" if you want me to guess your number. ' + 'What mode would you like to play?' set_prev_msg(handler_input, msg='What mode would you like to play?') 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): # Getting session attributes. session_attr = handler_input.attributes_manager.session_attributes min = int(session_attr['min']) max = int(session_attr['max']) attempts = int(session_attr['attempts']) if min > max: min, max = max, min if session_attr['mode'] == GUESS_ALEXA_NUMBER: # Setting the next intent. set_next_intent(handler_input=handler_input, next_intent=[GET_NUMBER_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 else: # session_attr['mode'] == GUESS_MY_NUMBER: set_next_intent(handler_input=handler_input, next_intent=[AMAZON_YES_INTENT, AMAZON_NO_INTENT]) 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
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
def guess_alexa_number(handler_input): # Getting session attributes session_attr = handler_input.attributes_manager.session_attributes alexa_number = session_attr["alexa_number"] min = session_attr['min'] max = session_attr['max'] guessed_number = handler_input.request_envelope.request.intent.slots[ "number"].value if not guessed_number: speech_text = say.notnumber() 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 int(guessed_number) < min or int(guessed_number) > max: speech_text = say.outofrange(min=min, max=max) 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 int(guessed_number) == alexa_number: set_next_intent(handler_input, next_intent=[AMAZON_YES_INTENT, AMAZON_NO_INTENT]) speech_text = say.correct() + '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 attempts = session_attr['attempts'] attempts -= 1 if attempts == 0: set_next_intent(handler_input, next_intent=[AMAZON_YES_INTENT, AMAZON_NO_INTENT]) speech_text = say.outoftries( alexa_number=alexa_number) + '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 if attempts == 1: attempt_text = 'attempt' else: attempt_text = 'attempts' if int(guessed_number) < alexa_number: positon_text = ['lower', 'higher'] else: positon_text = ['higher', 'lower'] session_attr['attempts'] = attempts speech_text = say.wrong(guessed_number=guessed_number, positon=positon_text, attempt_word=attempt_text, 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
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