【问题标题】:Error in Lambda Handler for Alexa Skill using Python使用 Python 的 Alexa Skill 的 Lambda 处理程序出错
【发布时间】:2017-08-03 16:00:27
【问题描述】:

我是 Python 编程的新手,但我正在尝试使用它来制作 Alexa 技能,但似乎我在使用 Lambda 处理程序时遇到了错误。我真的不知道这个功能是什么意思,所以我希望能帮助我找出问题所在。当我在 Amazon Developer Console 中测试时,我的技能运行良好,但在 Echo Dot 上测试时却失败了。在我的点上,我收到了欢迎消息,但是当我尝试设置我的第一个变量时,点显示“请求的技能响应存在问题”。我的代码基于 Amazon 发布的最喜欢的颜色示例,我合并了这篇文章中的代码:Adding session attributes in Python for Alexa skills

这是设置第一个变量时调用的函数的代码(这是我的 Echo Dot 失败的地方):

def set_amount_in_session(intent, session):
    """ Sets the invoice amount in the session and prepares the speech to reply to the
    user.
    """

    card_title = intent['name']
    session_attributes = {}
    should_end_session = False

    if 'invoiceAmount' in intent['slots']:
        invoice_amount = intent['slots']['invoiceAmount']['value']
        session['attributes']['invoiceAmount'] = int(invoice_amount)
        """ session_attributes = create_invoice_amount_attributes(invoice_amount) """
        speech_output = "The invoice amount is " + \
                        invoice_amount + \
                        " dollars. Please tell me the terms of the invoice by saying, " \
                        "my invoice terms are net thirty."
        reprompt_text = "Please tell me the terms of the invoice by saying, " \
                        "my invoice terms are net thirty."
    else:
        speech_output = "I'm not sure what the invoice amount is. " \
                        "Please try again."
        reprompt_text = "I'm not sure what the invoice amount is. " \
                        "Please tell me the amount of the invoice by saying, " \
                        "my invoice is for one hundred and fifty dollars."
    return build_response(session['attributes'], build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))
    """ return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session)) """

这是我的 Lambda 处理程序的代码:

def lambda_handler(event, context):
    """ Route the incoming request based on type (LaunchRequest, IntentRequest,
    etc.) The JSON body of the request is provided in the event parameter.
    """
    print("event.session.application.applicationId=" +
          event['session']['application']['applicationId'])

    """
    Uncomment this if statement and populate with your skill's application ID to
    prevent someone else from configuring a skill that sends requests to this
    function.
    """
    if (event['session']['application']['applicationId'] != "amzn1.ask.skill.28a97ae0-0a55-4cfb-96bb-a5fcf06e0f0b"):
        raise ValueError("Invalid Application ID")
    # if (event['session']['application']['applicationId'] !=
    #         "amzn1.echo-sdk-ams.app.[unique-value-here]"):
    #     raise ValueError("Invalid Application ID")

    if event['session']['new']:
        event['session']['attributes'] = {}
        on_session_started( {'requestId': event['request']['requestId'] }, event['session'])
    if event['request']['type'] == "LaunchRequest":
        return on_launch(event['request'], event['session'])
    elif event['request']['type'] == "IntentRequest":
        return on_intent(event['request'], event['session'])
    elif event['request']['type'] == "SessionEndedRequest":
        return on_session_ended(event['request'], event['session'])

当我在 Amazon 的 AWS Lambda 中测试我的代码时,我收到以下错误消息:

{
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      295,
      "lambda_handler",
      "event['session']['application']['applicationId'])"
    ]
  ],
  "errorType": "KeyError",
  "errorMessage": "'session'"
}

事实上,当我测试来自亚马逊最喜欢的颜色示例的未更改代码时,我在同一个地方遇到了同样的错误。但最喜欢的颜色技能确实适用于我的 Echo Dot。

【问题讨论】:

    标签: python amazon-web-services aws-lambda alexa alexa-skills-kit


    【解决方案1】:

    看起来您正在尝试将 int 与字符串连接。

    session['attributes']['invoiceAmount'] = int(invoice_amount)
    """ session_attributes = create_invoice_amount_attributes(invoice_amount) """
    speech_output = "The invoice amount is " + \
                            invoice_amount + \
                            " dollars. Please tell me the terms of the invoice by saying, " \
                            "my invoice terms are net thirty."
    

    使用str()方法将invoice_amount转换为字符串以防止TypeErrors

    speech_output = "The invoice amount is " + \
                                str(invoice_amount) + \
                                " dollars. Please tell me the terms of the invoice by saying, " \
                                "my invoice terms are net thirty."
    

    【讨论】:

    • 不行,试过了,没用。 invoice_amount 实际上已经是一个字符串,它来自一个 Amazon.NUMBER 插槽,但它将值存储为一个字符串,例如“100”。
    • 你是对的。我看到您转换为 int() 但意识到您使用了先前的变量。哎呀
    【解决方案2】:

    我找到了解决问题的方法。我必须启动所有会话变量,然后程序开始在我的 Echo Dot 上运行。所以在我的 get_welcome_response 函数中我添加了:

    session['attributes']['invoiceAmount'] = 0
    

    然后它开始工作了。

    但是,当我在 AWS Lambda 控制台中进行测试时,我仍然遇到与之前相同的错误,我不确定如何解决该问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 2018-01-21
      • 2018-05-05
      • 1970-01-01
      • 2019-11-09
      • 2017-10-06
      • 2019-12-10
      相关资源
      最近更新 更多