Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created July 9, 2018 00:13
Show Gist options
  • Save tanaikech/c111f5560d21a07529e0da870af06a7d to your computer and use it in GitHub Desktop.
Save tanaikech/c111f5560d21a07529e0da870af06a7d to your computer and use it in GitHub Desktop.
Using Dialog Box of Slack by Google Apps Script

Using Dialog Box of Slack by Google Apps Script

This is a GAS sample script for using a dialog box of Slack.

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

Reference :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment