Last active
September 28, 2022 14:47
-
-
Save nikhilym/0b99003ae11ec74dbfb92947a10592e8 to your computer and use it in GitHub Desktop.
Example usage of Progressive Response in ASK Python SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ask_sdk_core.skill_builder import CustomSkillBuilder | |
from ask_sdk_core.api_client import DefaultApiClient | |
from ask_sdk_core.dispatch_components import AbstractRequestHandler | |
from ask_sdk_core.dispatch_components import AbstractExceptionHandler | |
from ask_sdk_core.utils import is_request_type, is_intent_name | |
from ask_sdk_model.ui import SimpleCard | |
from ask_sdk_model.services.directive import ( | |
SendDirectiveRequest, Header, SpeakDirective) | |
from ask_sdk_core.handler_input import HandlerInput | |
from ask_sdk_model.response import Response | |
import time | |
import logging | |
sb = CustomSkillBuilder(api_client=DefaultApiClient()) | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.INFO) | |
class LaunchRequestHandler(AbstractRequestHandler): | |
# Handler for Skill Launch | |
def can_handle(self, handler_input): | |
# type: (HandlerInput) -> bool | |
return is_request_type("LaunchRequest")(handler_input) | |
def handle(self, handler_input): | |
# type: (HandlerInput) -> Response | |
speech_text = "Welcome to the Alexa Skills Kit, you can say hello!" | |
handler_input.response_builder.speak(speech_text).set_card( | |
SimpleCard("Hello World", speech_text)).set_should_end_session( | |
False) | |
return handler_input.response_builder.response | |
def get_progressive_response(handler_input): | |
# type: (HandlerInput) -> None | |
request_id_holder = handler_input.request_envelope.request.request_id | |
directive_header = Header(request_id=request_id_holder) | |
speech = SpeakDirective(speech="Ok, give me a minute") | |
directive_request = SendDirectiveRequest( | |
header=directive_header, directive=speech) | |
directive_service_client = handler_input.service_client_factory.get_directive_service() | |
directive_service_client.enqueue(directive_request) | |
time.sleep(5) | |
return | |
class HelloWorldIntentHandler(AbstractRequestHandler): | |
# Handler for Hello World Intent | |
def can_handle(self, handler_input): | |
# type: (HandlerInput) -> bool | |
return is_intent_name("HelloWorldIntent")(handler_input) | |
def handle(self, handler_input): | |
# type: (HandlerInput) -> Response | |
speech_text = "Hello World!" | |
get_progressive_response(handler_input) | |
handler_input.response_builder.speak(speech_text).set_card( | |
SimpleCard("Hello World", speech_text)).set_should_end_session( | |
False) | |
return handler_input.response_builder.response | |
class HelpIntentHandler(AbstractRequestHandler): | |
# Handler for Help Intent | |
def can_handle(self, handler_input): | |
# type: (HandlerInput) -> bool | |
return is_intent_name("AMAZON.HelpIntent")(handler_input) | |
def handle(self, handler_input): | |
# type: (HandlerInput) -> Response | |
speech_text = "You can say hello to me!" | |
handler_input.response_builder.speak(speech_text).ask( | |
speech_text).set_card(SimpleCard( | |
"Hello World", speech_text)) | |
return handler_input.response_builder.response | |
class CancelOrStopIntentHandler(AbstractRequestHandler): | |
# Single handler for Cancel and Stop Intent | |
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 = "Goodbye!" | |
handler_input.response_builder.speak(speech_text).set_card( | |
SimpleCard("Hello World", speech_text)) | |
return handler_input.response_builder.response | |
class SessionEndedRequestHandler(AbstractRequestHandler): | |
# Handler for Session End | |
def can_handle(self, handler_input): | |
# type: (HandlerInput) -> bool | |
return is_request_type("SessionEndedRequest")(handler_input) | |
def handle(self, handler_input): | |
# type: (HandlerInput) -> Response | |
return handler_input.response_builder.response | |
class CatchAllExceptionHandler(AbstractExceptionHandler): | |
# Catch all exception handler, log exception and | |
# respond with custom message | |
def can_handle(self, handler_input, exception): | |
# type: (HandlerInput, Exception) -> bool | |
return True | |
def handle(self, handler_input, exception): | |
# type: (HandlerInput) -> Response | |
logger.exception( | |
"Encountered following exception: {}".format(exception), | |
exc_info=True) | |
speech = "Sorry, there was some problem. Please try again!!" | |
handler_input.response_builder.speak(speech).ask(speech) | |
return handler_input.response_builder.response | |
sb.add_request_handler(LaunchRequestHandler()) | |
sb.add_request_handler(HelloWorldIntentHandler()) | |
sb.add_request_handler(HelpIntentHandler()) | |
sb.add_request_handler(CancelOrStopIntentHandler()) | |
sb.add_request_handler(SessionEndedRequestHandler()) | |
sb.add_exception_handler(CatchAllExceptionHandler()) | |
handler = sb.lambda_handler() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ah. it doesn't get you any more time :(
"Note: Progressive responses do not change the overall time allowed for a response. When a user invokes a skill, the skill has approximately 8 seconds to return a full response. The skill must finish processing any progressive responses as well as the full response within this time."
progressive responses are so there's not a 8 seconds of silence.