【问题标题】:Understanding slots and getting its values in Alexa Skills Kit在 Alexa Skills Kit 中了解插槽并获取其值
【发布时间】:2019-04-16 07:27:50
【问题描述】:

我正在尝试正确理解插槽如何以编程方式工作。在编写任何代码之前,我试图通过查看examples for alexa sdk for python 来很好地理解它。

具体来说,我试图了解ColorPicker 示例中插槽的基础知识(我正确理解了hello_world,并自己编写了一些代码添加内容。工作正常)。

我很难理解如何访问这些槽值(因为我看到它以两种不同的方式完成)。

def whats_my_color_handler(handler_input):
    """Check if a favorite color has already been recorded in
    session attributes. If yes, provide the color to the user.
    If not, ask for favorite color.
    """
    # type: (HandlerInput) -> Response
    if color_slot_key in handler_input.attributes_manager.session_attributes:
        fav_color = handler_input.attributes_manager.session_attributes[
            color_slot_key]
        speech = "Your favorite color is {}. Goodbye!!".format(fav_color)
        handler_input.response_builder.set_should_end_session(True)
    else:
        speech = "I don't think I know your favorite color. " + help_text
        handler_input.response_builder.ask(help_text)

    handler_input.response_builder.speak(speech)
    return handler_input.response_builder.response

我对这个函数的理解是,color_slot_key是 Alexa(de frontend,Alexa Developer)中插槽中变量的名称。但是,据我所知,handler_input.attributes_manager.session_attributes 是一般的插槽,通过其键 handler_input.attributes_manager.session_attributes['color_slot_key'] 访问将获得具有该键的插槽的值。

如果我理解正确,这对我来说是有意义的。如果有颜色,alexa 会说出它。如果没有,它不会。

现在:

@sb.request_handler(can_handle_func=is_intent_name("MyColorIsIntent"))
def my_color_handler(handler_input):
    """Check if color is provided in slot values. If provided, then
    set your favorite color from slot value into session attributes.
    If not, then it asks user to provide the color.
    """
    # type: (HandlerInput) -> Response
    slots = handler_input.request_envelope.request.intent.slots

    if color_slot in slots:
        fav_color = slots[color_slot].value
        handler_input.attributes_manager.session_attributes[
            color_slot_key] = fav_color
        speech = ("Now I know that your favorite color is {}. "
                  "You can ask me your favorite color by saying, "
                  "what's my favorite color ?".format(fav_color))
        reprompt = ("You can ask me your favorite color by saying, "
                    "what's my favorite color ?")
    else:
        speech = "I'm not sure what your favorite color is, please try again"
        reprompt = ("I'm not sure what your favorite color is. "
                    "You can tell me your favorite color by saying, "
                    "my favorite color is red")

    handler_input.response_builder.speak(speech).ask(reprompt)
    return handler_input.response_builder.response

我不明白为什么在这个函数中,颜色的值是通过:

handler_input.request_envelope.request.intent.slots[color].value

而不是像第一个函数中的东西(像这样):

handler_input.attributes_manager.session_attributes[color_slot_key]

我猜第一个只是检查会话属性中是否存在(我不太明白这些是什么),另一个与 Alexa requests。但是为什么格式不一样呢?

【问题讨论】:

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


    【解决方案1】:

    你混合了两种不同的东西。会话属性与槽值无关。 您通过以下方式检索插槽:

    handler_input.request_envelope.request.intent.slots
    

    为了方便起见,示例将检索到的颜色存储在会话属性中,以便在整个技能会话期间保存。 将会话属性视为在技能会话中存在的持久属性(键 + 值)(它们可以是您想要的任何东西)。

    会话属性的颜色选择器示例使用说明如下:

    https://developer.amazon.com/de/docs/custom-skills/manage-skill-session-and-session-attributes.html#save-data-during-the-session

    (你甚至有一个选项卡来选择 python)

    在测试选项卡中,测试技能,看看传入和传出的json。事情会变得清楚的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-28
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2023-03-06
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多