使用Slack';s API对话框\u从Firebase云函数打开 目标

使用Slack';s API对话框\u从Firebase云函数打开 目标,firebase,google-cloud-functions,slack-api,google-api-nodejs-client,request-promise,Firebase,Google Cloud Functions,Slack Api,Google Api Nodejs Client,Request Promise,Slack Event Subscription事件正在命中我的Firebase端点,然后Firebase云函数正在使用dialog\u open端点在Slack中打开一个对话框,其中包含来自Firebase的一些值 问题 当Firebase Cloud函数点击Slack的对话框\u open端点时,控制台日志中会出现错误 我得到正文:{ok:false,错误:'trigger\u expired'} 5:31:29.757 PM helloWorld body: { ok: false, er

Slack Event Subscription事件正在命中我的Firebase端点,然后Firebase云函数正在使用
dialog\u open
端点在Slack中打开一个对话框,其中包含来自Firebase的一些值

问题 当Firebase Cloud函数点击Slack的
对话框\u open
端点时,控制台日志中会出现错误

我得到
正文:{ok:false,错误:'trigger\u expired'}

5:31:29.757 PM helloWorld body: { ok: false, error: 'invalid_trigger' }
5:31:28.744 PM helloWorld Function execution took 9 ms, finished with status code: 200
5:31:28.740 PM helloWorld json.trigger_id: "405615464868.7467239747.e706f2732257c541c445ad3938a29fd3"
5:31:28.735 PM helloWorld Function execution started
但是,Firebase中的日志显示往返时间小于500毫秒。但是,我没有看到将从第一个请求记录到触发器ID的触发器ID(参见下面的代码)

当我在新部署后第二次、第三次或第四次触发Slack事件时,我会得到
body:{ok:false,error:'invalid_trigger'}

5:31:29.757 PM helloWorld body: { ok: false, error: 'invalid_trigger' }
5:31:28.744 PM helloWorld Function execution took 9 ms, finished with status code: 200
5:31:28.740 PM helloWorld json.trigger_id: "405615464868.7467239747.e706f2732257c541c445ad3938a29fd3"
5:31:28.735 PM helloWorld Function execution started
这也发生得足够快(9毫秒),但触发错误不同,这次我看到Slack事件订阅事件中的
json.trigger\u id

最后,我尝试了JSON.stringify:

trigger\u id:JSON.stringify(JSON.trigger\u id),

现在,日志和错误是不同的:

5:33:24.512 PM | info | helloWorld | body: { ok: false, error: 'internal_error' }
5:33:23.565 PM | outlined_flag | helloWorld | Function execution took 13 ms, finished with status code: 200
5:33:23.559 PM | info | helloWorld | json.trigger_id: "406895248231.7467239747.7490e460213b3d65a44eef9f2e30c168"
5:33:23.553 PM | outlined_flag | helloWorld | Function execution started
问题: 我一定是在做傻事。猜猜我的
触发器id有什么问题吗

代码 以下是Firebase云函数:

import * as rp from "request-promise";
import * as functions from "firebase-functions";

export const helloWorld = functions.https.onRequest((request, response) => {
  return new Promise((_resolve, _reject) => {
    let json = JSON.parse(request.body.payload);
    console.log("json.trigger_id:", json.trigger_id);

    const options = {
      method: "POST",
      uri: "https://slack.com/api/dialog.open",
      body: {
        trigger_id: json.trigger_id,
        dialog: {
          callback_id: json.callback_id,
          title: "Request a Ride",
          submit_label: "Request",
          elements: [
            { type: "text", label: "Pickup Location", name: "loc_origin" },
            { type: "text", label: "Dropoff Location", name: "loc_destination" }
          ]
        }
      },
      json: true,
      headers: {
        "Content-type": "application/json; charset=utf-8",
        Authorization:
          "Bearer xoxp-secret"
      }
    };

    rp(options)
      .then(function(body) {
        console.log("body:", body);
      })
      .catch(function(err) {
        console.log("err:", err);
      });
    return response.status(200).json({ message: "Hello from Firebase" });
  }).catch(err => response.status(500).send(err));
});
答复 违背承诺:在我的例子中,承诺是错误的

消息与对话框:对话框不需要或不适合此目标

代码气味:发送回消息不需要web.chat.postMessage。可以使用Firebase Cloud函数的
res
将消息发送回Slack

下面是一个改进的示例

...
exports.eventsSlackBot = functions.https.onRequest((req, res) => {
  return new Promise((resolve, reject) => {
      // asyc things happening
    })
    .then(() => {
      resolve.status(200).send({
        "text": "I am a test message http://slack.com",
        "attachments": [{
          "text": "And here’s an attachment!"
        }]
      });
    })
    .catch(console.error);
});
...

对于其他遇到此问题的人。我看不出你做错了什么。。。。但我要提到的是,如果您通过Firebase控制台查看函数日志,有时并非所有日志消息都显示在父级日志显示上。查找日志消息更可靠的地方是直接在GCP日志中:访问并选择左侧边栏菜单上的云功能,然后单击3点并选择“查看日志”。可能会帮你调试。我想我犯了一些愚蠢的错误。请参见slackapi GitHub页面上对该问题的出色回答。一旦修复,我将在这里发布解决方案。