微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决用 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 举报,一经查实,本站将立刻删除。