【问题标题】:Alexa Lambda KeyError: 'intent'Alexa Lambda KeyError:“意图”
【发布时间】:2023-04-10 02:54:01
【问题描述】:

我对编程知之甚少,但已经使用树莓派工作了几年。我想在 Pi 中使用 Alexa 并运行脚本来打开和关闭 GPIOS。在尝试了一些教程之后,我将 AWS Lambda 与 Alexa 技能连接起来。问题是我在测试技能时遇到错误。 “技能反应被标记为失败 卢斯 请求标识符:amzn1.echo-api.request.49687858-4c4f-482f-b82d-dd0ffedc9841

目标 Lambda 应用程序返回失败响应"

我检查了 AWS 云上的日志,这就是我得到的。

'intent': KeyError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 40, in lambda_handler
    intent_name = event['request']['intent']['name']
KeyError: 'intent 

我不知道该怎么做。我改编了来自 hackster.io 教程的代码。我使用 python 2.7 的 Lambda 代码是:

import boto3

access_key = 
access_secret = 
region =
queue_url = 

def build_speechlet_response(title, output, reprompt_text, should_end_session):
    return {
        'outputSpeech': {
            'type': 'PlainText',
            'text': output
        },
        'card': {
            'type': 'Simple',
            'title': "SessionSpeechlet - " + title,
            'content': "SessionSpeechlet - " + output
        },
        'reprompt': {
            'outputSpeech': {
                'type': 'PlainText',
                'text': reprompt_text
            }
        },
        'shouldEndSession': should_end_session
    }

def build_response(session_attributes, speechlet_response):
    return {
        'version': '1.0',
        'sessionAttributes': session_attributes,
        'response': speechlet_response
    }

def post_message(client, message_body, url):
    response = client.send_message(QueueUrl = url, MessageBody= message_body)
    
def lambda_handler(event, context):
    client = boto3.client('sqs', aws_access_key_id = access_key, aws_secret_access_key = access_secret, region_name = region)
    intent_name = event['request']['intent']['name']
    if intent_name == "LightOn":
        post_message(client, 'on', queue_url)
        message = "on"
    elif intent_name == "LightOff":
        post_message(client, 'off', queue_url)
        message = "off"
    else:
        message = "Unknown"
        
    speechlet = build_speechlet_response("Mirror Status", message, "", "true")
    return build_response({}, speechlet)

我只想打开然后关闭 LED,但我不知道我是否需要所有这些代码。如果有一种简单的方法可以在 lba 中进行编码,请告诉我。

非常感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    有几种不同的请求类型,“标准的”是:CanFulfillIntentRequestLaunchRequestIntentRequestSessionEndedRequest

    你先打开技能,发送的是LaunchRequest——不包含intent参数。这可能就是您收到KeyError 的原因。

    一个好的方法是在尝试处理之前检查请求类型。

    if event['request']['type'] == "LaunchRequest":
        print("I'm a launch request.")
    elif event['request']['type'] == "IntentRequest":
        print("I'm an intent request.")
    

    各种请求类型和它们可以容纳的参数可以在Alexa documentation找到。

    【讨论】:

    • 那么,LaunchRequest 就像声明一个变量?所以我需要从import boto3 LaunchRequest == intent 开始?
    • 好的!我设法按照你的建议去做。它打印“我是一个启动请求”。并得到与请求相同的错误。那么,我需要将其更改为“意图请求”吗?
    • @PablinhoRiveiro 如果您正在努力了解 Alexa 技能的工作原理以及它们如何与 lambda 函数交互 - 一个好主意可能是看看 Alexa GitHub page 那里有很多入门时很有帮助的基本技能功能示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多