无法从 Google 个应用脚本打开 Slack 模式

Unable to open Slack modal from Google App Scripts

我正在尝试通过从 GAS 单击按钮打开一个松弛模式。目前我正在发送一个具有此功能的交互式按钮:

function Send(){
  
  var url = "my slack url here"
  var payload = 
    {
      "text": "Here's your interactive buttons message.",
    "blocks": [
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Click Me",
                        "emoji": true
                    },
                    "value": "id_btn",
                    "action_id": "id_btn"
                }
            ]
        }
    ]
    }

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

UrlFetchApp.fetch(url,options); 
  }

但是,当我与按钮交互时没有任何反应。我目前的Get/Post方法如下:

function doGet(e){
return ContentService.createTextOutput(""); 
}

function doPost(e) {

  if (typeof e !== 'undefined') { 

    // Extract the relevant data
    var parameter = e.parameter;
    var date = new Date();
    var payload = JSON.parse(parameter.payload)

    var trigger_id = payload.trigger_id;
    var slackUrl = "https://slack.com/api/views.open";
    var myToken = "Token I got after installing app to slack"

var payload_upd ={
    "trigger_id": trigger_id,
    "type": "modal",
    "view":{
    "title": {
        "type": "plain_text",
        "text": "Gratitude Box",
        "emoji": true
    },
    "submit": {
        "type": "plain_text",
        "text": "Submit",
        "emoji": true
    },
    "close": {
        "type": "plain_text",
        "text": "Cancel",
        "emoji": true
    },
    "blocks": [
        {
            "type": "input",
            "block_id": "my_block",
            "element": {
                "type": "plain_text_input",
                "action_id": "my_action"
            },
            "label": {
                "type": "plain_text",
                "text": "Say something nice!",
                "emoji": true
            }
        }
    ]
    }
}

// Send options   
    var options_upd = {
    "headers": {"Authorization": myToken},
    "method": "post",
    "contentType": "application/json",
    "payload": JSON.stringify(payload_upd),
  }; 
  
    UrlFetchApp.fetch(slackUrl, options_upd);
  }  
  
}

我尝试了多种方法可以将 trigger_id 和令牌组合到我的负载和选项中,但我无法找到有效的方法。任何帮助将不胜感激,谢谢。

您的负载似乎无效,因为 "type": "modal" 应该是 view 的一部分。试试这个负载:

var payload_upd = {
  "trigger_id": trigger_id,
  "view": {
    "type": "modal",
    "title": {
      "type": "plain_text",
      "text": "Gratitude Box",
      "emoji": true
    },
    "submit": {
      "type": "plain_text",
      "text": "Submit",
      "emoji": true
    },
    "close": {
      "type": "plain_text",
      "text": "Cancel",
      "emoji": true
    },
    "blocks": [
      {
        "type": "input",
        "block_id": "my_block",
        "element": {
          "type": "plain_text_input",
          "action_id": "my_action"
        },
        "label": {
          "type": "plain_text",
          "text": "Say something nice!",
          "emoji": true
        }
      }
    ]
  }
};

此外,请确保您在授权中指定“Bearer”header。

var options_upd = {
  "headers": {"Authorization": "Bearer " + myToken},
  "method": "post",
  "contentType": "application/json",
  "payload": JSON.stringify(payload_upd),
};