【问题标题】:How to check and get Alexa slot value with Python ask sdk如何使用 Python ask sdk 检查和获取 Alexa 插槽值
【发布时间】:2019-06-06 14:33:41
【问题描述】:

在我的交互模型中,我定义了一个名为 city 的插槽,它是可选的,不是必需的,以实现一个意图。

我正在使用 python ask sdk,所以我的处理程序是这样的:

class IntentHandler(RequestHandler):
    """
    Intent handler
    """

    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return is_request_type("IntentRequest")(handler_input) and \
               is_intent_name("ExampleIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        access_token = handler_input.request_envelope.context.system.user.access_token

        if access_token is None:
            raise Exception("no access_token provided")

        speech = RESPONSE_MESSAGE_DEFAULT
        handler_input.response_builder.speak(speech)

    return handler_input.response_builder.response

如何检查槽是否被用户填满以及如何获取其值?

【问题讨论】:

    标签: python alexa alexa-skills-kit alexa-skill alexa-slot


    【解决方案1】:

    在您的处理程序中,您可以执行以下操作:

    slots = handler_input.request_envelope.request.intent.slots
    city = slots['city']
    if city.value:
        # take me down to the paradise city
    else:
        # this city was not built on rock'n'roll
    

    slots 是一个包含str: Slot 值的字典,有关详细信息,请参阅IntentSlot 的源代码。

    【讨论】:

    • 对于我的用例,使用内置的动物插槽,我有这样的工作:python {'animal': { 'confirmation_status': 'NONE', 'name': 'animal', 'resolutions': None, 'value': 'a dog'} } 然后我通过执行以下操作访问这些数据:python slots = handler_input.request_envelope.request.intent.slots print(slots) animal_slot = slots['animal'] animal = animal_slot.value print(f"ANIMAL REQUESTED: {animal}")
    【解决方案2】:

    根据here 找到的文档,您可以执行以下操作:

    import ask_sdk_core.utils as ask_utils
    slot = ask_utils.request_util.get_slot(handler_input, "slot_name")
    if slot.value:
        # do stuff
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      相关资源
      最近更新 更多