Home>
What you can do

You can send a message with a button to Slack with a bot.
In addition, when you send"/hogehoge" ;, a dialog will pop up that you can enter.
The following functions are used.

  • Google Apps Script
  • Incoming Webhooks
  • Slack API
  • Slash Command
Current code
// Slash Comand received
function doPost (e) {
  var response = send_Button (e);
    return ContentService.createTextOutput (JSON.stringify (response)). setMimeType (ContentService.MimeType.JSON);
}
function send_Button (e) {
  var input_text = e.parameter.text;
  var slackUrl = "https://hooks.slack.com/services/xxxxx.....xxxxxxx";
  // message text
  var messageData = {
    "text": "subject:" + input_text,
    "attachments": [
    {
    "text": "Are you ok?",
    "fallback": "Sorry, no support for buttons.",
    "callback_id": "lessonbutton",
    "color": "# 3AA3E3",
    "attachment_type": "default",
    "actions": [
    {
    "name": "button",
    "text": "OK",
    "style": "primary",
    "type": "button",
    "value": "ok"
    }]
  }]
}

// format for Slack
var options = {
  'method': 'post',
  'contentType': 'application/json',
  // Convert the JavaScript object to a JSON string.
  'payload': JSON.stringify (messageData)
};
// post to Slack
UrlFetchApp.fetch (slackUrl, options);
return input_text;
}


Then you will receive an interactive message with a button on Slack.

function Slack_Dialog (e) {
  var text = e.parameter.text;
  var response = {text: text};
  var trigger_id = e.parameter.trigger_id;
  var slackUrl = "https://hogehogehoge.slack.com/api/dialog.open";
  var dialog = {
    "token": "xoxxxxxxxxxxxxxxxxxxxxxxxxxxxxx .... xxxx",
    "trigger_id": trigger_id,
    "dialog": JSON.stringify ({
      "callback_id": "wantdo3",
      "title": "What do I want to do there?",
      "submit_label": "Request",
      "elements": [
        {
          "type": "text",
          "label": "In The Morning",
          "name": "dialg_morning"
        },
        {
          "type": "text",
          "label": "In The Afternoon",
          "name": "dialg_afternoon"
        },
        {
          "type": "text",
          "label": "In The Night",
          "name": "dialg_night"
        }
      ]
    })
  }
  var options = {
    'method': 'post',
    'payload': dialog,
  };
  return response = UrlFetchApp.fetch (slackUrl, options);
}


A dialog will open.

If i enter

"/hogehoge" ;, you will receive a message with a button, and when you press the button, a dialog will appear.
https://www.slideshare.net/yasuyuki_ogawa/slack-apps
There was an important callback on this link slide (p14).
But we still don't understand callbacks.

  • Answer # 1

    Slack Dialog function

    Because personal information is included, it's a bit omitted
    First, the contents of Dialog

    function Slack_Dialog (trigger_id, callback_id) {
      var slackUrl = "https://hogehoge.slack.com/api/dialog.open";
      var dialog = {
        "token": "xoxxxxxxx ... xxxxxxxx", // OAuth token
        "trigger_id": trigger_id,
        "dialog": JSON.stringify ({
          "callback_id": "dialog",
          "title": "Title here.",
          "submit_label": "Submit",
          "elements": [
            {
              "type": "text",
              "label": "title",
              "name": "dialg_subject",
              "placeholder": "Enter title ..."
            }
          ]
        })
      };
      var options = {
        'method': 'post',
        'payload': dialog,
      };
      UrlFetchApp.fetch (slackUrl, options);
      return ContentService.createTextOutput ();// Important
    }


    This function is arranged to be executed after processing of buttons and buttons.

    In fact, the input data to Dialog goes on payload.

    function doPost (e) {
        var parameter = e.parameter;
        var data = parameter.payload;
        var json = JSON.parse (decodeURIComponent (data));
        var submission = parameter.submission;
        var submission = json.submission;
        else if (callback_id == "dialog") {
          GmailApp.sendEmail ("[email protected]", "outgoings", JSON.stringify (submission.dialg_subject));
        }
    }


    I couldn't see how to check the data entered properly inlogger.log (), so I checked it by email for the time being.
    I was able to confirm the string entered in Dialog properly.

Related articles