从Firebase Cloud Functions使用Slack的API dialog_open

时间:2018-07-27 19:26:48

标签: firebase google-cloud-functions slack-api google-api-nodejs-client request-promise

目标

一个Slack Event-Subscription事件击中了我的Firebase端点,然后,一个Firebase Cloud Function使用dialog_open端点在Slack中打开一个对话框,其中包含Firebase的一些值。

问题

当Firebase Cloud功能击中Slack的dialog_open端点时,控制台日志中出现错误。

我得到body: { ok: false, error: 'trigger_expired' }

但是,Firebase中的日志显示往返时间少于500毫秒。但是,我看不到触发器ID,该ID将从第一个请求登录到trigger_id(请参见下面的代码)。

4:57:12.443 PM | info | helloWorld | body: { ok: false, error: 'trigger_expired' } 
4:57:04.163 PM | outlined_flag | helloWorld | Function execution took 254 ms, finished with status code: 200
4:57:03.910 PM | outlined_flag | helloWorld | Function execution started

在重新部署后第二次,第三次或第四次触发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 Event-Subscription事件中的json.trigger_id

最后,我尝试了JSON.stringify:

trigger_id: JSON.stringify(json.trigger_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

问题

我一定在做蠢事。有人猜测我的trigger_id怎么了?

代码

这是Firebase Cloud功能:

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));
});

1 个答案:

答案 0 :(得分:0)

答案

破损的承诺:在我的示例中,这些承诺是错误的。

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

代码异味不需要web.chat.postMessage发回消息。可以使用Firebase Cloud Function的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);
});
...