首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法通过google应用程序脚本打开Slack对话框

无法通过google应用程序脚本打开Slack对话框
EN

Stack Overflow用户
提问于 2018-06-29 00:01:32
回答 1查看 1.2K关注 0票数 3

我试图使用谷歌应用程序脚本和松懈来自动化我的工作。我希望输入一些文字与Slack对话框,以修改我的谷歌电子表格与谷歌应用程序脚本。但是,使用下面的代码,我无法通过Slack-API's Slash command打开对话框。我的代码有问题吗?

代码语言:javascript
运行
复制
function doPost(e){
var params = e.parameter;
var token = params.token;
var text = params.text;
var trigger_id = params.trigger_id;
var slackUrl = ["https://slack.com/api/dialog.open"];
if (token == "[token from slack]"){
    var dialog = {
  "token": "[OAuth Token]",
  "trigger_id":trigger_id,
  "dialog":{
  "callback_id": "ryde-46e2b0",
    "title": "Request a Ride",
      "submit_label": "Request",
        "elements": [
          {
            "type": "text",
            "label": "Pickup Location",
            "name": "loc_origin"
          },
          {
            "type": "text",
            "label": "Dropoff Location",
            "name": "loc_destination"
          }
        ]
}
};
var options = {
  'method' : 'POST',
  'contentType': 'application/json',
  'payload' : dialog}; 
UrlFetchApp.fetch(slackUrl, options);
}  
else{
 var res = {"text":"failed token verification!"} 
return          ContentService.createTextOutput(JSON.stringify(res)).setMimeType(ContentService.MimeType.JSON);
 }}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-29 01:26:29

这个修改怎么样?

修改要点:

  • 使用字符串"UrlFetchApp.fetch(url,params)“的"url”。
  • 使用JSON.stringify()表示对象dialogdialog
  • 不需要'contentType': 'application/json',

修改脚本:

代码语言:javascript
运行
复制
function doPost(e) {
  var params = e.parameter;
  var token = params.token;
  var text = params.text;
  var trigger_id = params.trigger_id;
  var slackUrl = "https://slack.com/api/dialog.open";
  if (token == "[token from slack]"){ // Please input this.
    var dialog = {
      "token": "[OAuth Token]", // Please input this.
      "trigger_id": trigger_id,
      "dialog": JSON.stringify({
        "callback_id": "ryde-46e2b0",
        "title": "Request a Ride",
        "submit_label": "Request",
        "elements": [
          {
            "type": "text",
            "label": "Pickup Location",
            "name": "loc_origin"
          },
          {
            "type": "text",
            "label": "Dropoff Location",
            "name": "loc_destination"
          }
        ]
      })
    }
    var options = {
      'method' : 'post',
      'payload' : dialog,
    }; 
    UrlFetchApp.fetch(slackUrl, options);
  }  
  else{
    var res = {"text":"failed token verification!"} 
    return ContentService.createTextOutput(JSON.stringify(res)).setMimeType(ContentService.MimeType.JSON);
  }
  return ContentService.createTextOutput(); // Important
}

注意:

当对话框提交中没有异常时,您的应用程序必须用200 OK响应空体。这将完成对话框。

  • 当它使用对话框时,它使用上面的ContentService.createTextOutput()返回空体,因为状态代码不能被Google脚本自定义。当空体未返回时,将发生错误。
  • 这个修改后的脚本假设您已经完成了使用Slack对话框的设置。
  • 如果您修改了脚本,请将Web重新部署为新版本。这样,最新版本的脚本就会反映到Web .中。

参考资料:

在我的环境中,我确认了这个修改过的脚本可以工作。但如果这不管用,我很抱歉。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51092613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档