Stack Overflow: A New Way for Developers to Get Support for Alexa

Now you can find Alexa-specific tags and topics in the AWS Collective on Stack Overflow. Ask questions and get help from badged experts and the Alexa developer community!

question

RamaK avatar image
RamaK asked

New SDK and Python 3.6 Progressive response Sample Code

Hi All,

Can some one help me on - Progressive response using new sdk with python 3.6.

here is my sample code - but i am not aware how to create directive

def _progressive_response_(handler_input): 
#Call Alexa Directive Service. 
    requestEnvelope = handler_input.request_envelope

    directiveServiceClient = handler_input.service_client_factory.get_directive_service()

    requestId = requestEnvelope.request.request_id
 endpoint = handler_input.request_envelope.context.system.api_endpoint 
    accessToken = handler_input.request_envelope.context.system.api_access_token 
# main problem for me here - how to generate directive object for progressive response ( i am new to python)
directive = (
         header = {
            requestId,
        },
        directive = {
            'type' : 'VoicePlayer.Speak',
            'speech' : 'your request is in progress please wait for few secs'
        }
    )

    #send directive 
    return directiveServiceClient.enqueue(directive,endpoint,accessToken)

Regards,

Rama

alexa skills kitsdkpython
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

roy-1 avatar image
roy-1 answered

Hi Rama,
Thanks for posting. Please see the following code snippet for using the Progressive Response API with the ASK SDK for Python (Beta):

from ask_sdk_core.skill_builder import CustomSkillBuilder
from ask_sdk_core.api_client import DefaultApiClient
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
    
sb = CustomSkillBuilder(api_client=DefaultApiClient())
    
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!"
    
        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)
        # Adding a 5 second sleep for testing
        time.sleep(5)
    
        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

3 comments
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sameer Mahajan avatar image Sameer Mahajan commented ·

Thanks for the post. I think you are missing

sb.add_request_handler(HelloWorldIntentHandler())
0 Likes 0 ·

When I try this I get an error during the response. The log shows something like:

[ERROR] DispatchException: Unable to find a suitable request handler
Traceback (most recent call last):
File "/var/task/ask_sdk_core/skill_builder.py", line 109, in wrapper
request_envelope=request_envelope, context=context)
File "/var/task/ask_sdk_core/skill.py", line 199, in invoke
handler_input=handler_input)
File "/var/task/ask_sdk_runtime/dispatch.py", line 130, in dispatch
raise e
File "/var/task/ask_sdk_runtime/dispatch.py", line 118, in dispatch
output = self.__dispatch_request(handler_input) # type: Union[Output, None]
File "/var/task/ask_sdk_runtime/dispatch.py", line 165, in __dispatch_request
"Unable to find a suitable request handler")

any ideas?

0 Likes 0 ·
Sameer Mahajan avatar image Sameer Mahajan newuser-3329e962-c968-43dc-b24a-ea35ec6537eb commented ·

It must be because of the lambda timeout that you are hitting. Try increasing the timeout value.

0 Likes 0 ·