can't open Slack dialog through google apps scripts

好久不见. 提交于 2019-12-12 08:57:26

问题


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

回答1:


How about this modification?

Modification points :

  • 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.

Modified script :

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
}

Note :

When there are no exceptions within the dialog submission, your app must respond with 200 OK with an empty body. This will complete the dialog.

  • 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.

References :

  • UrlFetchApp.fetch()

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



来源:https://stackoverflow.com/questions/51092613/cant-open-slack-dialog-through-google-apps-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!