import logging from ask_sdk.standard import StandardSkillBuilder #from ask_sdk_core.skill_builder import SkillBuilder from ask_sdk_core.utils import is_request_type, is_intent_name from ask_sdk_core.handler_input import HandlerInput from ask_sdk_model import Response from ask_sdk_model.ui import SimpleCard import time # for delayed responses - probably don't work #import urllib #import urllib.request SKILL_NAME = 'Number Guessing Game' #sb = StandardSkillBuilder(table_name="number_guessing_game", auto_create_table=True) sb = StandardSkillBuilder() logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) min0,max0 = 1,100 attr = {} # global attributes attr['min'] = min0 attr['max'] = max0 attr['guesscount'] = 0 attr['lastguess'] = -1 def makeguess(attr,min,max): # returns string response + ok to continue flag res = "You cheated! " res += " Skynet was informed of this incident. Terminators are on the way. Kiss your ass goodbye." ok = False if min<=max: guess = (min+max)//2 # res = "Is the number "+str(guess)+", or is it higher or lower" res = "My guess is "+str(guess)+", is this correct, or should it be higher or lower" attr['lastguess'] = guess if attr['guesscount']>5: res = "I hope you're not misleading me. I can tell if you're lying. "+res ok = True return (res,ok) # makeguess @sb.request_handler(can_handle_func=is_request_type("LaunchRequest")) def launch_request_handler(handler_input): global attr """Handler for Skill Launch. Get the persistence attributes, to figure out the game state. """ # type: (HandlerInput) -> Response # attr = handler_input.attributes_manager.persistent_attributes # if not attr: # attr['game_state'] = 'ENDED' # req = urllib.request('http://cs.hofstra.edu/~cscccl/csc15p/3/test.txt') # introread = req.read() # introtext = req.decode('UTF-8').rstrip() # convert to regular string attr = {} attr['min'] = min0 attr['max'] = max0 attr['guesscount'] = 0 attr['lastguess'] = -1 smin,smax = str(min0),str(max0) # handler_input.attributes_manager.session_attributes = attr speech_text = "Welcome to the Number Guessing Game by Chuck Liang. " speech_text += " Think of a number between "+smin+" and "+smax+" and I will try to guess what it is. Ready to begin?" reprompt = "Ready to begin?" handler_input.response_builder.speak(speech_text).ask(reprompt) return handler_input.response_builder.response @sb.request_handler(can_handle_func=is_intent_name("AMAZON.HelpIntent")) def help_intent_handler(handler_input): """Handler for Help Intent.""" # type: (HandlerInput) -> Response speech_text = "You can say correct, higher, or lower." reprompt = "What's your answer?" handler_input.response_builder.speak(speech_text).ask(reprompt) return handler_input.response_builder.response @sb.request_handler( can_handle_func=lambda input: is_intent_name("AMAZON.CancelIntent")(input) or is_intent_name("AMAZON.StopIntent")(input)) def cancel_and_stop_intent_handler(handler_input): """Single handler for Cancel and Stop Intent.""" # type: (HandlerInput) -> Response speech_text = "What a loser! Goodbye." handler_input.response_builder.speak( speech_text).set_should_end_session(True) return handler_input.response_builder.response @sb.request_handler(can_handle_func=is_request_type("SessionEndedRequest")) def session_ended_request_handler(handler_input): """Handler for Session End.""" # type: (HandlerInput) -> Response logger.info( "Session ended with reason: {}".format( handler_input.request_envelope.request.reason)) return handler_input.response_builder.response @sb.request_handler(can_handle_func=is_intent_name("AMAZON.YesIntent")) def yes_handler(handler_input): global attr # type: (HandlerInput) -> Response #attr = handler_input.attributes_manager.session_attributes attr['guesscount'] += 1 min,max = attr['min'],attr['max'] (res,ok) = makeguess(attr,min,max) if ok: handler_input.response_builder.speak(res).ask(res) else: handler_input.response_builder.speak(res).set_should_end_session(True) return handler_input.response_builder.response @sb.request_handler(can_handle_func=lambda input: is_intent_name("HigherIntent")(input)) def higher_handler(handler_input): global attr # type: (HandlerInput) -> Response #attr = handler_input.attributes_manager.session_attributes attr['guesscount'] += 1 attr['min'] = attr['lastguess']+1 min,max = attr['min'],attr['max'] (res,ok) = makeguess(attr,min,max) if ok: handler_input.response_builder.speak(res).ask(res) else: handler_input.response_builder.speak(res).set_should_end_session(True) return handler_input.response_builder.response @sb.request_handler(can_handle_func=lambda input: is_intent_name("LowerIntent")(input)) def lower_handler(handler_input): global attr # type: (HandlerInput) -> Response #attr = handler_input.attributes_manager.session_attributes attr['guesscount'] += 1 attr['max'] = attr['lastguess']-1 min,max = attr['min'],attr['max'] (res,ok) = makeguess(attr,min,max) if ok: handler_input.response_builder.speak(res).ask(res) else: handler_input.response_builder.speak(res).set_should_end_session(True) return handler_input.response_builder.response @sb.request_handler(can_handle_func=lambda input: is_intent_name("CorrectIntent")(input)) def correct_handler(handler_input): global attr # type: (HandlerInput) -> Response #attr = handler_input.attributes_manager.session_attributes gcx = attr['guesscount'] handler_input.response_builder.speak("I guessed your number in "+str(gcx)+" attempts because professor Liang taught me how to do binary search. Goodbye.").set_should_end_session(True) return handler_input.response_builder.response @sb.request_handler(can_handle_func=lambda input: is_intent_name("AMAZON.FallbackIntent")(input)) def fallback_handler(handler_input): """AMAZON.FallbackIntent is only available in en-US locale. This handler will not be triggered except in that locale, so it is safe to deploy on any locale. """ # type: (HandlerInput) -> Response #attr = handler_input.attributes_manager.session_attributes speech_text = "You can say correct, or that's right, or that's it, or it's lower, or higher, or larger, or smaller" handler_input.response_builder.speak(speech_text).ask("What's your response") return handler_input.response_builder.response @sb.exception_handler(can_handle_func=lambda i, e: True) def all_exception_handler(handler_input, exception): """Catch all exception handler, log exception and respond with custom message. """ # type: (HandlerInput, Exception) -> Response logger.error(exception, exc_info=True) speech = "I can't understand. Please say it again!!" handler_input.response_builder.speak(speech).ask(speech) return handler_input.response_builder.response @sb.global_response_interceptor() def log_response(handler_input, response): """Response logger.""" # type: (HandlerInput, Response) -> None logger.info("Response: {}".format(response)) lhandler = sb.lambda_handler()