可以通过以下代码示例实现获取口语值而不是插槽ID的解决方法:
from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.utils import is_intent_name
from ask_sdk_model import IntentRequest
class MyIntentHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return is_intent_name("YourIntent")(handler_input)
def handle(self, handler_input):
slots = handler_input.request_envelope.request.intent.slots
slot_value = slots["YourSlotName"].value if slots["YourSlotName"].value else ""
# 使用口语值而不是插槽ID
if slot_value == "口语值1":
# 执行相应的逻辑
elif slot_value == "口语值2":
# 执行相应的逻辑
else:
# 处理未知的口语值
return handler_input.response_builder.response
# 添加自定义Intent处理程序到技能的请求处理链中
sb.add_request_handler(MyIntentHandler())
在上面的代码中,我们首先通过handler_input.request_envelope.request.intent.slots
获取到所有的插槽,并使用slots["YourSlotName"].value
获取到指定插槽的口语值。如果插槽的口语值存在,则将其存储在slot_value
变量中。然后,我们可以根据不同的口语值执行相应的逻辑。
注意:在上述代码中,将YourIntent
和YourSlotName
替换为你自己的意图名称和插槽名称。