代码之家  ›  专栏  ›  技术社区  ›  Chadd Yasin Özcan

使用Slack的API对话框从FireBase Cloud函数打开

  •  2
  • Chadd Yasin Özcan  · 技术社区  · 7 年前

    目标

    可宽延事件订阅事件正在命中我的FireBase端点,然后,FireBase Cloud函数正在使用 dialog_open 终结点打开一个对话框,其中包含来自FireBase的一些值。

    问题

    当firebase cloud函数命中slack的 对话开放 端点,我在控制台日志中得到错误。

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

    然而,firebase中的日志显示往返时间不到500毫秒。但是,我没有看到将从第一个请求记录到触发器ID的触发器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
    

    当我在新部署之后第二次、第三次或第四次触发松弛事件时,我会 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毫秒),但触发错误不同,这次我 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 回复  |  直到 6 年前
        1
  •  0
  •   Chadd Yasin Özcan    6 年前

    回答

    破碎的诺言 在我的例子中,承诺是错误的。

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

    代码气味 : web.chat.postMessage 不是 需要 发回信息。可以使用firebase cloud函数的 res .

    下面是一个改进的例子。

    ...
    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);
    });
    ...
    
    推荐文章