在Alexa Skills Ki中了解插槽并获取其价值

2024-04-25 21:03:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

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会说出来。如果不是的话,那就不是了

现在:

^{pr2}$

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

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

而不是像在第一个函数中(像这样):

handler_input.attributes_manager.session_attributes[color_slot_key]

我想第一个只是检查会话属性中是否有它(我不太明白这些属性是什么),另一个与Alexa请求有关。但为什么格式不同呢?在


Tags: key函数inputresponsesessionbuildermanagerspeech
1条回答
网友
1楼 · 发布于 2024-04-25 21:03:55

你把两种不同的东西混在一起。会话属性与插槽值无关。 您可以通过以下方式获取插槽:

handler_input.request_envelope.request.intent.slots

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

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

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

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

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

相关问题 更多 >