【问题标题】:Recall a session variable in Python for an Alexa skill为 Alexa 技能调用 Python 中的会话变量
【发布时间】:2017-08-02 22:34:47
【问题描述】:

所以我已经阅读了这篇文章:Adding session attributes in Python for Alexa skills,它解决了我能够存储多个会话变量的问题。

但是,现在我的问题是在另一个函数中调用这些会话变量。这是我正在尝试做的一个示例:

speech_output = "Your invoice for " + session['attributes']['invoiceAmount'] + \
                " dollars."

我也尝试过这种代码来设置会话变量的局部变量:

invoice_amount = int(session['attributes']['invoiceAmount'])

我做错了什么?我以前从未使用过 Python 编程,所以我只是通过查看亚马逊最喜欢的颜色示例代码并根据我的需要调整它来自​​学。我实际上有三个会话变量,但显然如果我能让其中一个工作,我就能找出另一个。谢谢。

【问题讨论】:

    标签: python-2.7 alexa-skills-kit


    【解决方案1】:

    真的很抱歉,我终于找出了错误。我的会话变量正在存储整数,但是我试图将这些整数与文本连接起来。我没有意识到我需要先将它们转换为字符串。我转换成一个字符串,解决了我所有的问题。

    【讨论】:

    • 可以删除问题吗?
    • 当我点击删除时,它说我不应该删除问题。
    【解决方案2】:

    您的问题很可能涉及session 变量的范围。

    以下代码是来自颜色专家技能的意图调用。 正如您在第 1 行中看到的,变量 session 被定义为一个参数。在第 12 行,该变量被传递给函数set_color_in_session(intent,session)

    def on_intent(intent_request, session): # <-------------
        """ Called when the user specifies an intent for this skill """
    
        print("on_intent requestId=" + intent_request['requestId'] +
              ", sessionId=" + session['sessionId'])
    
        intent = intent_request['intent']
        intent_name = intent_request['intent']['name']
    
        # Dispatch to your skill's intent handlers
        if intent_name == "MyColorIsIntent":
            return set_color_in_session(intent, session) # <-------------
        elif intent_name == "WhatsMyColorIntent":
            return get_color_from_session(intent, session) # <-------------
        elif intent_name == "AMAZON.HelpIntent":
            return get_welcome_response()
        else:
            raise ValueError("Invalid intent")
    

    根据提供的信息,我相信您定义了自己的函数以由自定义意图触发,并且很可能忘记将 session 变量传递给这些函数。同样,变量session 仅在作为参数传入时才会存在于您的函数中。以函数def get_color_from_session(intent, session): 为例。因为session 是作为参数传入的,所以它在第6 行的这个函数中可用,favorite_color = session['attributes']['favoriteColor']

    如果您不传入变量session,您将引用一个可能不存在的名为session 的局部变量。

    def get_color_from_session(intent, session):
        session_attributes = {}
        reprompt_text = None
    
        if "favoriteColor" in session.get('attributes', {}):
            favorite_color = session['attributes']['favoriteColor'] #<-------------
            speech_output = "Your favorite color is " + favorite_color + \
                            ". Goodbye."
            should_end_session = True
        else:
            speech_output = "I'm not sure what your favorite color is. " \
                            "You can say, my favorite color is red."
            should_end_session = False
    
        # Setting reprompt_text to None signifies that we do not want to reprompt
        # the user. If the user does not respond or says something that is not
        # understood, the session will end.
        return build_response(session_attributes, build_speechlet_response(
            intent['name'], speech_output, reprompt_text, should_end_session))
    

    【讨论】:

    • 我不知道,但我检查了我的代码,我确实通过 on_intent 函数将会话变量传递给每个函数,因为我刚刚剪切并粘贴了原始代码并更改了名称意图和功能。但是,我在上面链接到的另一篇文章末尾有关于手动创建 lambda 处理程序的代码。我不知道我应该在哪里放置该代码。会不会是这个问题?
    猜你喜欢
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    相关资源
    最近更新 更多