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 import json import requests from stu import * #sb = SkillBuilder() logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) ######### # Application logic class stuinfo: def load(self): r = requests.get('http://cs.hofstra.edu/~cscccl/csc15p/3/stuinfo.txt') #self.raw = r.text self.info = readstus(r.text) # dict of student objects # loads self.info def __init__(self): self.state = 0 self.load() def intro(self): # state 0 output self.state = 0 return "Welcome to The Hofstra Student Information Service. Would you like to hear which students I have information for?" def listnames(self): # state 1 output self.state=1 speech_text = "I have information for " i,n = 0,len(self.Names) while i bool return is_request_type("LaunchRequest")(handler_input) def handle(self, handler_input): speech_text = Sinst.intro() handler_input.response_builder.speak(speech_text).set_card(SimpleCard("Student Information", speech_text)).set_should_end_session(False) return handler_input.response_builder.response #launchrequest handler #### yes intent class YesIntentHdlr0(AbstractRequestHandler): def can_handle(self, handler_input): return (Sinst.state==0 and is_intent_name("AMAZON.YesIntent")(handler_input)) def handle(self, handler_input): speech_text = Sinst.listnames() # goto state 1 handler_input.response_builder.speak(speech_text).set_should_end_session(False) return handler_input.response_builder.response # yes intent class NoIntentHdlr0(AbstractRequestHandler): def can_handle(self, handler_input): return (Sinst.state==0 and is_intent_name("AMAZON.NoIntent")(handler_input)) def handle(self, handler_input): speech_text = Sinst.askwho() # goto state 2 handler_input.response_builder.speak(speech_text).set_should_end_session(False) return handler_input.response_builder.response # no intent class NameIntentHdlr0(AbstractRequestHandler): def can_handle(self,handler_input): return is_intent_name("NameIntent")(handler_input) def handle(self, handler_input): #logging.info("NameIntent invoked") slots = handler_input.request_envelope.request.intent.slots name = slots["student_name"].value name = str(name) logging.info("got name:"+str(name)+".") speech_text = Sinst.getinfo(name) # goto state 3 handler_input.response_builder.speak(speech_text).set_card(SimpleCard("Student "+name, speech_text)).set_should_end_session(False) return handler_input.response_builder.response # name intent 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) def handle(self, handler_input): # type: (HandlerInput) -> Response speech_text = "I hope you found the information you were looking for. 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 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 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 # Log the exception in CloudWatch Logs logging.info("exception: "+str(exception)) speech_text = "Please speak clearly." # speech_text = hamgame.greeting handler_input.response_builder.speak(speech_text).ask(speech_text) return handler_input.response_builder.response #exception handler # creating lambda handler sb = SkillBuilder() sb.add_request_handler(LaunchRequestHdlr()) sb.add_request_handler(YesIntentHdlr0()) sb.add_request_handler(NoIntentHdlr0()) sb.add_request_handler(NameIntentHdlr0()) sb.add_request_handler(CancelAndStopIntentHdlr()) sb.add_request_handler(SessionEndedRequestHdlr()) sb.add_exception_handler(AllExceptionHdlr()) handler = sb.lambda_handler()