【发布时间】: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