aws-lambda - 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
关于如何找到实际错误/如何调试的任何想法?
解决方案
更新:简单的解决方案是使用API Gateway Lambda 代理集成,它将繁重的工作从 API Gateway 转移到 Lambda 函数:了解 API Gateway Lambda 代理集成
Lambda总是期望从API 网关接收数据,JSON
并且在某些情况下发送到 API 的数据不是JSON
。来自Slack的斜杠命令就是这种情况。他们使用 Content-Type 发出请求,因此请求的正文如下所示:POST
application/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
推荐阅读
- php - 使变量不可变
- c++ - 消除`boost::condition::timed_wait`中的`boost::xtime`
- vb6 - 在 vb6 中求解线性方程组
- mysql - Flutter 如何从 JSON 中列出具有多个子项的数组
- mysql - 您如何为不同的 AWS 资源提供不同的权限以在 1 个 RDS 实例中分隔 MySQL 数据库?
- excel - Excel 条件格式问题
- mocha.js - 我可以在没有“@std/esm”库的情况下将 NYC 与 ESM 和 Mocha 一起使用吗
- powershell - PowerShell 过滤远程桌面用户组的输出
- elasticsearch - 如何在 Elasticsearch reindex 无痛脚本中生成 UUID
- c++ - gcc ld:未找到架构 i386 的符号