首页 > 解决方案 > Slack 斜杠命令从 lambda 获取 400_client_error

问题描述

我在 heroku 上托管了一个简单的斜杠命令,效果很好。我把它移到了 lambda,但现在它得到了:

Darn - that slash command didn't work (error message: `400_client_error`). Manage the command at .

该错误没有提供更多信息,也没有说明失败的原因。

我可以用 curl 到达端点,它会返回在我看来格式正确的 json:

curl -X POST \
  https://5hi90e8v8e.execute-api.us-east-2.amazonaws.com/Prod \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Postman-Token: eacb33ee-96ac-457e-a325-9fdc132f808e' \
  -H 'cache-control: no-cache'
{
    "text": "Have suggestions to improve this bot? Message <@UC8R7PA87>",
    "response_type": "in_channel"
}

标题:

Date →Sat, 09 Mar 2019 01:12:02 GMT
Content-Type →application/json
Content-Length →922
Connection →keep-alive
x-amzn-RequestId →57cec19a-4208-11e9-924c-89367de80c6d
x-amz-apigw-id →WP_jLFWYiYcFiXg=
X-Amzn-Trace-Id →Root=1-5c8312e1-327ac177db85f799dcd267c1;Sampled=0

我的旧 heroku 端点的响应:

{
    "response_type": "in_channel",
    "text": "Have suggestions to improve this bot? Message <@UC8R7PA87>"
}

标题:

Connection →keep-alive
Server →gunicorn/19.6.0
Date →Sat, 09 Mar 2019 01:01:36 GMT
Content-Type →application/json
Content-Length →946
Via →1.1 vegur

关于如何找到实际错误/如何调试的任何想法?

标签: aws-lambdaslackslack-api

解决方案


更新:简单的解决方案是使用API Gateway Lambda 代理集成,它将繁重的工作从 API Gateway 转移到 Lambda 函数:了解 API Gateway Lambda 代理集成


Lambda总是期望从API 网关接收数据,JSON并且在某些情况下发送到 API 的数据不是JSON。来自Slack的斜杠命令就是这种情况。他们使用 Content-Type 发出请求,因此请求的正文如下所示:POSTapplication/x-www-form-urlencoded

token=gIkuvaNzQIHg97ATvDxqgjtO&command=/weather&text=lorem

API Gateway中,集成请求映射模板部分,您必须为我们期望的请求的 Content-Type 添加一个模板。在这种情况下,它是.application/x-www-form-urlencoded

您可以简单地将请求正文按原样包装在一个JSON对象中,以便我们可以将值传递给 Lambda 进行解析。

{
  "postBody" : $input.json("$")
}

此信息取自https://medium.com/@farski/learn-aws-api-gateway-with-the-slack-police-ca8d636e9fc0


推荐阅读