question

newuser-f8d94517-6454-4c01-9694-4b1f5c58c38a avatar image

DispatchException: Unable to find a suitable request handler

Hi,

I have this error when I run my skill:

ask_sdk_runtime.exceptions.DispatchException: Unable to find a suitable request handler.

Can anyone help me?

Here is my code:


# -*- coding: utf-8 -*-


# This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK for Python.

# Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,

# session persistence, api calls, and more.

# This sample is built using the handler classes approach in skill builder.


import freesound

import logging

import ask_sdk_core.utils as ask_utils

import os

from ask_sdk_s3.adapter import S3Adapter

s3_adapter = S3Adapter(bucket_name=os.environ["S3_PERSISTENCE_BUCKET"])


from ask_sdk_core.skill_builder import CustomSkillBuilder

from ask_sdk_core.dispatch_components import AbstractRequestHandler

from ask_sdk_core.dispatch_components import AbstractExceptionHandler

from ask_sdk_core.handler_input import HandlerInput

from ask_sdk_model import Response


from ask_sdk_model.interfaces.audioplayer import AudioItem, Stream, PlayDirective, PlayBehavior


import boto3

import botocore

from botocore.exceptions import NoCredentialsError


import urllib


logger = logging.getLogger(__name__)

logger.setLevel(logging.INFO)


# COSTANT

API_KEY = "X"

NUMBER_OF_RESULT = 2

S3_BUCKET_ACCESS_KEY = 'X'

S3_BUCKET_SECRET_KEY = 'X'



class LaunchRequestHandler(AbstractRequestHandler):

"""Handler for Skill Launch."""

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return ask_utils.is_request_type("LaunchRequest")(handler_input)


def handle(self, handler_input):

# type: (HandlerInput) -> Response

speak_output = "Welcome, you can say Play or Help. Which would you like to try?"

reprompt_text = "Try to say Play snow"


return (

handler_input.response_builder

.speak(speak_output)

.ask(reprompt_text)

.response

)


class CaptureTitleIntentHandler(AbstractRequestHandler):

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return ask_utils.is_intent_name("CaptureTitleIntent")(handler_input)


def handle(self, handler_input):

return {

'directives': [{

'type': 'AudioPlayer.Play',

'playBehavior': 'REPLACE_ALL',

'audioItem': {

'stream': {

'token': '2',

'url': 'URL',

'offsetInMilliseconds': 1

}

}

}],

'shouldEndSession': True

}



class PlaybackNearlyFinishedEventHandler(AbstractRequestHandler):

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return ask_utils.is_request_type("AudioPlayer.PlaybackNearlyFinished")(handler_input)


def handle(self, handler_input):

# type: (HandlerInput) -> Response

return {

'directives': [{

'type': 'AudioPlayer.Play',

'playBehavior': 'ENQUEUE',

'audioItem': {

'stream': {

'token': '1',

'expectedPreviousToken': '2',

'url': 'URL',

'offsetInMilliseconds': 1

}

}

}],

'shouldEndSession': True

}


class ReproduceIntentHandler(AbstractRequestHandler):

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return ask_utils.is_intent_name("ReproduceIntent")(handler_input)


def handle(self, handler_input):

speak_output = "The download link of song is "


return (

handler_input.response_builder

.speak(speak_output)

.response

)



class NextSoundIntentHandler(AbstractRequestHandler):

"""Handler for Help Intent."""

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return ask_utils.is_intent_name("AMAZON.NextIntent")(handler_input)


def handle(self, handler_input):

# type: (HandlerInput) -> Response

return {

'directives': [{

'type': 'AudioPlayer.Stop'

}],

'shouldEndSession': True

}


class HelpIntentHandler(AbstractRequestHandler):

"""Handler for Help Intent."""

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return ask_utils.is_intent_name("AMAZON.HelpIntent")(handler_input)


def handle(self, handler_input):

# type: (HandlerInput) -> Response

speak_output = "You can say Play or I want hear! How can I help?"


return (

handler_input.response_builder

.speak(speak_output)

.ask(speak_output)

.response

)



class CancelOrStopIntentHandler(AbstractRequestHandler):

"""Single handler for Cancel and Stop Intent."""

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return (ask_utils.is_intent_name("AMAZON.CancelIntent")(handler_input) or

ask_utils.is_intent_name("AMAZON.PauseIntent")(handler_input) or

ask_utils.is_intent_name("AMAZON.StopIntent")(handler_input))


def handle(self, handler_input):

# type: (HandlerInput) -> Response

return {

'directives': [{

'type': 'AudioPlayer.Stop'

}],

'shouldEndSession': True

}



class SessionEndedRequestHandler(AbstractRequestHandler):

"""Handler for Session End."""

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return ask_utils.is_request_type("SessionEndedRequest")(handler_input)


def handle(self, handler_input):

# type: (HandlerInput) -> Response


return {

'directives': [{

'type': 'AudioPlayer.Stop'

}],

'shouldEndSession': True

}


#return handler_input.response_builder.response



class IntentReflectorHandler(AbstractRequestHandler):

"""The intent reflector is used for interaction model testing and debugging.

It will simply repeat the intent the user said. You can create custom handlers

for your intents by defining them above, then also adding them to the request

handler chain below.

"""

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return ask_utils.is_request_type("IntentRequest")(handler_input)


def handle(self, handler_input):

# type: (HandlerInput) -> Response

intent_name = ask_utils.get_intent_name(handler_input)

speak_output = "You just triggered " + intent_name + "."


return (

handler_input.response_builder

.speak(speak_output)

# .ask("add a reprompt if you want to keep the session open for the user to respond")

.response

)



class CatchAllExceptionHandler(AbstractExceptionHandler):

"""Generic error handling to capture any syntax or routing errors. If you receive an error

stating the request handler chain is not found, you have not implemented a handler for

the intent being invoked or included it in the skill builder below.

"""

def can_handle(self, handler_input, exception):

# type: (HandlerInput, Exception) -> bool

return True


def handle(self, handler_input, exception):

# type: (HandlerInput, Exception) -> Response

logger.error(exception, exc_info=True)


speak_output = "Sorry, I had trouble doing what you asked. Please try again."


return (

handler_input.response_builder

.speak(speak_output)

.ask(speak_output)

.response

)


sb = CustomSkillBuilder(persistence_adapter=s3_adapter)


sb.add_request_handler(LaunchRequestHandler())

sb.add_request_handler(CaptureTitleIntentHandler())

sb.add_request_handler(ReproduceIntentHandler())

sb.add_request_handler(HelpIntentHandler())

sb.add_request_handler(NextSoundIntentHandler())

sb.add_request_handler(PlaybackNearlyFinishedEventHandler())

sb.add_request_handler(CancelOrStopIntentHandler())

sb.add_request_handler(SessionEndedRequestHandler())

sb.add_request_handler(IntentReflectorHandler())


sb.add_exception_handler(CatchAllExceptionHandler())


lambda_handler = sb.lambda_handler()


alexa skills kitalexaintentsechopython
1 comment
10 |5000

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

Which intent are you trying to invoke when you are getting this error? This error indicates that there are no handlers registered for that request.

0 Likes 0 ·

1 Answer

Gaetano@Amazon avatar image
Gaetano@Amazon answered

Hi,

can you please confirm you have a handler registered for the IntentRequest your backend is receiving?

Please share the JSON request and the skill-ID so we can have a better look at it.

Thanks,
Gaetano

4 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.

Skill ID: amzn1.ask.skill.b90ec4f7-fe0f-4442-bd13-0d5544181e9b


Thank you very much

0 Likes 0 ·
Gaetano@Amazon avatar image Gaetano@Amazon ♦ newuser-f8d94517-6454-4c01-9694-4b1f5c58c38a ·

can you share the JSON request as well?

thanks

0 Likes 0 ·

I don't know where is the problem in my code. Which request should I share?

0 Likes 0 ·
Show more comments