【问题标题】:Slack slash command getting 400_client_error from lambdaSlack 斜杠命令从 lambda 获取 400_client_error
【发布时间】:2019-03-09 01:23:00
【问题描述】:

我在 heroku 上托管了一个简单的 slack slash 命令,效果很好。我把它移到了 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-lambda slack slack-api


    【解决方案1】:

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


    Lambda 总是希望以 JSON 从 API 网关 接收数据,并且在某些情况下发送到 API 的数据不是JSON。 来自 Slack 的 slash commands 就是这种情况。 他们使用application/x-www-form-urlencoded Content-Type 发出POST 请求,因此请求的正文类似于:

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

    在 API Gateway,Integration Request 的 Mapping Templates 部分,您必须为我们期待的请求。在这种情况下,它是application/x-www-form-urlencoded。

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

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

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

    【讨论】:

      【解决方案2】:

      可能是 AWS Gateway 中的问题以及它如何转换请求。尝试使用:

      {
        "method": "$context.httpMethod",
        "body" : $input.json('$'),
        "headers": {
          #foreach($param in $input.params().header.keySet())
          "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end
      
          #end
        }
      }
      

      在集成请求/映射模板/应用程序/json 中进行调试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-09
        • 1970-01-01
        • 2017-02-11
        相关资源
        最近更新 更多