要解决这个问题,你可以使用AMAZON.FallbackIntent来处理超过最大重复提示的情况。以下是一个示例代码,展示了如何在委托获取AMAZON.Date时处理超过最大重复提示的情况:
from ask_sdk_core.skill_builder import SkillBuilder
from ask_sdk_core.dispatch_components import AbstractRequestHandler, AbstractExceptionHandler
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model import Response
class GetDateHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return (
handler_input.request_envelope.request.intent.name == "GetDateIntent"
and handler_input.request_envelope.request.dialog_state != "COMPLETED"
)
def handle(self, handler_input):
return handler_input.response_builder.add_delegate_directive().response
class FallbackIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return (
handler_input.request_envelope.request.intent.name == "AMAZON.FallbackIntent"
and handler_input.request_envelope.request.dialog_state != "COMPLETED"
)
def handle(self, handler_input):
return handler_input.response_builder.add_delegate_directive().response
class MaxRepromptExceptionHandler(AbstractExceptionHandler):
def can_handle(self, handler_input, exception):
return (
isinstance(exception, ValueError)
and str(exception) == "Max reprompt attempts exceeded"
)
def handle(self, handler_input, exception):
speech_text = "对不起,我无法获取日期。"
return (
handler_input.response_builder.speak(speech_text)
.ask(speech_text)
.response
)
sb = SkillBuilder()
sb.add_request_handler(GetDateHandler())
sb.add_request_handler(FallbackIntentHandler())
sb.add_exception_handler(MaxRepromptExceptionHandler())
lambda_handler = sb.lambda_handler()
在上面的示例中,我们定义了两个请求处理程序:GetDateHandler和FallbackIntentHandler。GetDateHandler处理GetDateIntent意图,当对话状态不是“COMPLETED”时,使用add_delegate_directive()指令来委托获取AMAZON.Date插槽值。FallbackIntentHandler处理AMAZON.FallbackIntent意图,当对话状态不是“COMPLETED”时,同样使用add_delegate_directive()指令来委托处理用户的回答。
我们还定义了一个异常处理程序MaxRepromptExceptionHandler,它在处理Max reprompt attempts exceeded异常时,返回一个提示用户无法获取日期的响应。
最后,我们使用SkillBuilder将这些请求处理程序和异常处理程序添加到技能中,并使用lambda_handler作为AWS Lambda函数的入口。
这样,当用户在获取日期时超过最大重复提示时,将触发MaxRepromptExceptionHandler,返回一个无法获取日期的响应。