无法通过Google Apps脚本打开“松弛"对话框 [英] can't open Slack dialog through google apps scripts

查看:133
本文介绍了无法通过Google Apps脚本打开“松弛"对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Apps脚本和Slack来使我的工作自动化.我希望在松弛"对话框中输入一些文本,以使用Google Apps脚本修改我的Google电子表格.但是,使用下面的代码,我无法通过Slack-API's Slash command打开对话框.我的代码有问题吗?

I'm trying to use google apps scripts and slack to automate my work. And I wish to enter some text with the Slack dialog to modify my google spreadsheet with google apps scripts. However, with the below code, I can't open a Dialog via Slack-API's Slash command. Is my code have some problems?

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);
 }}

推荐答案

此修改如何?

  • 使用字符串对"UrlFetchApp.fetch(url,params)"进行"url"设置.
  • JSON.stringify()用于对象dialogdialog.
  • 不需要
  • 'contentType': 'application/json',.
  • Use a string to "url" of "UrlFetchApp.fetch(url, params)".
  • Use JSON.stringify() for dialog of the object dialog.
  • 'contentType': 'application/json', is not required.
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响应,且正文为空.这将完成对话框.

  • 使用对话框时,由于上面的状态代码无法通过Google Apps脚本进行自定义,因此它使用上面的ContentService.createTextOutput()返回空主体.如果未返回空主体,则会发生错误.
  • 此修改后的脚本假定您已经完成了使用松弛"对话框的设置.
  • 如果您修改了脚本,请重新部署Web Apps作为新版本.这样,最新版本的脚本就会反映到Web Apps.
    • When it uses dialog, it returns the empty body using ContentService.createTextOutput() for above, because the status code cannot be customized by Google Apps Script. When the empty body is not returned, the error occurs.
    • This modified script supposes that your setting for using Slack dialog has already been done.
    • If you modified your script, please redeploy Web Apps as a new version. By this, the script of the latest version is reflected to Web Apps.
    • 在我的环境中,我确认此修改后的脚本有效.但是,如果这不起作用,对不起.

      In my environment, I confirmed that this modified script works. But if this didn't work, I'm sorry.

      这篇关于无法通过Google Apps脚本打开“松弛"对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆