用 200 OK 响应 Slack,然后在谷歌应用程序脚本中运行没有时间触发器的函数

问题描述

我有关于 GAS 和 Slack 的问题。问题来自 Slack 要求脚本在 3 秒内响应,但我的脚本需要的时间远不止于此。

我首先必须向 Slack 返回 200 OK,然后运行我想要的函数。最推荐的方法是时间触发器,但是在我的情况下这是不可能的,因为时间触发器需要 10-40 秒来执行函数并且不准确。为了用户体验,我需要在几秒钟内立即做出响应。

一个广泛的例子,如下所示。

首先发送初始有效载荷:

function slacktest() {

  const webHook = "Your Webhook Here"

  const payload = {
      "blocks": [
          {
              "type": "actions","elements": [
                  {
                    "type": "button","text": {
                        "type": "plain_text","text": "Click Me","emoji": true
                    },"value": "click_me_123","action_id": "actionId-0"
                }
              ]
          }
      ]
  }

  var options = {
    "method" : "Post","contentType" : "application/json","payload" : JSON.stringify(payload)
  };
 
  UrlFetchApp.fetch(webHook,options)
  
}

响应 Slack 的交互

function doPost(e){

  return ContentService.createtextoutput("200 OK"); 

  // Does not run because I am already returning 200 OK,and time triggers are not an option
  actualFunction()

}

实际功能可能是这样的:

function actualFunction(){

  const webHook = "Enter the actual slack webhook here"

      const payload2 = {
          "blocks": [
            {
              "type": "actions","elements": [
                    {
                        "type": "button","text": {
                            "type": "plain_text","text": "New Button","emoji": true
                        },"action_id": "actionId-0"
                    }
                ]
            }
        ]
    }

    var options2 = {
      "method" : "Post","payload" : JSON.stringify(payload2)
    };
    
    UrlFetchApp.fetch(webHook,options2)
}

如果有人知道这个问题的解决方案,那会很有帮助。由于 GAS 不支持异步任务,因此我一直无法找到解决方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)