## Alexa skill for Hofstra University facts import logging from ask_sdk_core.skill_builder import SkillBuilder from ask_sdk_core.dispatch_components import AbstractRequestHandler 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 from ask_sdk_core.dispatch_components import AbstractExceptionHandler from random import randint, random # add other modules you need to import sb = SkillBuilder() logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) ## Because the program is run on AWS servers, to trace your program you need # to use the looger: # logger.info("the program got to here without crashing, var x is "+str(x)) class HofstraFacts: Facts = [ "Hofstra University is located in Hempstead, New York", "The students at Hofstra are so nice and quiet, they're like farm animals", "Hofstra is a non-profit, private University", "There are approximately 10000 students, 1000 faculty and 30000 administrators at Hofstra", "The food is expensive at Hofstra but students don't protest", "Hofstra has no football team. Go Hofstra, not!", "Hofstra's best students major in computer science", "The best part of Hofstra is the Engineering school" ] def __init__(self): self.counter = 0 # counts through facts # self def NextFact(self): f = HofstraFacts.Facts[self.counter] n = len(HofstraFacts.Facts) self.counter = (self.counter+1) % n # wrap around facts return f # nextfact def RandomFact(self): n = len(HofstraFacts.Facts) return HofstraFacts.Facts[randint(0,n-1)] # RandomFact ### define permutefacts... def reset(self): self.counter = 0 # reset def GetFact(self,i): # get i'th fact if i>=0 and i bool return is_request_type("LaunchRequest")(handler_input) def handle(self, handler_input): logger.info("launch handler started") # to log info speech_text = "Do you want to know something about Hofstra University?" #hamgame.greeting reprompt = "Ready to begin?" handler_input.response_builder.speak(speech_text).ask(reprompt) return handler_input.response_builder.response #launchrequest handler #### "yes" intent handler - using built-in Amazon intent class YesIntentHdlr(AbstractRequestHandler): def can_handle(self, handler_input): return is_intent_name("AMAZON.YesIntent")(handler_input) def handle(self, handler_input): speech_text = HF.NextFact() speech_text += ". Do you want to hear another fact?" handler_input.response_builder.speak(speech_text).set_card(SimpleCard("Hofstra Facts", speech_text)).set_should_end_session(False) return handler_input.response_builder.response # yes intent handler # invoked when user wants to end session: class CancelAndStopIntentHdlr(AbstractRequestHandler): def can_handle(self, handler_input): # type: (HandlerInput) -> bool return (is_intent_name("AMAZON.CancelIntent")(handler_input) or (is_intent_name("AMAZON.StopIntent")(handler_input) or is_intent_name("AMAZON.NoIntent")(handler_input))) def handle(self, handler_input): # type: (HandlerInput) -> Response speech_text = "Hope you learned something about Hofstra. Goodbye." handler_input.response_builder.speak(speech_text).set_card(SimpleCard("Stop", speech_text)).set_should_end_session(True) return handler_input.response_builder.response #cancelstop handler # help intent handler class HelpIntentHdlr(AbstractRequestHandler): def can_handle(self, handler_input): # type: (HandlerInput) -> bool return is_intent_name("AMAZON.HelpIntent")(handler_input) or is_intent_name("AMAZON.FallbackIntent")(handler_input) def handle(self, handler_input): # type: (HandlerInput) -> Response speech_text = "just say yes or no" handler_input.response_builder.speak(speech_text).ask(speech_text).set_card(SimpleCard("Help", speech_text)) return handler_input.response_builder.response #helpintent ### standard handler class SessionEndedRequestHdlr(AbstractRequestHandler): def can_handle(self, handler_input): # type: (HandlerInput) -> bool return (is_request_type("SessionEndedRequest")(handler_input) or is_intent_name(AMAZON.NavigateHomeIntent)(handler)(input)) def handle(self, handler_input): # type: (HandlerInput) -> Response # any cleanup logic goes here return handler_input.response_builder.response #end handler # catch all exceptions class AllExceptionHdlr(AbstractExceptionHandler): def can_handle(self, handler_input, exception): # type: (HandlerInput, Exception) -> bool return True def handle(self, handler_input, exception): # type: (HandlerInput, Exception) -> Response logger.info("exception: "+str(exception)) # log the exception speech_text = "Please speak more clearly" handler_input.response_builder.speak(speech_text).ask(speech_text) return handler_input.response_builder.response #exception handler ### creating lambda handler - order matters sb.add_request_handler(LaunchRequestHdlr()) sb.add_request_handler(YesIntentHdlr()) sb.add_request_handler(HelpIntentHdlr()) sb.add_request_handler(CancelAndStopIntentHdlr()) sb.add_request_handler(SessionEndedRequestHdlr()) sb.add_exception_handler(AllExceptionHdlr()) handler = sb.lambda_handler() # skill builder sb defined above # referr to handler as hofstrafacts.handler # to retrieve value inside handler method: # slots = handler_input.request_envelope.request.intent.slots # answerval = int(slots["answer_value"].value)