如何使用Python询问sdk检查并获取Alexa插槽值

问题描述 投票:0回答:2

在我的交互模型中,我定义了一个名为

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-slot alexa-sdk-python
2个回答
13
投票

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

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的源代码了解更多详细信息。


4
投票

根据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
© www.soinside.com 2019 - 2024. All rights reserved.