Help us understand the problem. What is going on with this article?

Google Apps Script で Slack interactive message

More than 1 year has passed since last update.

Slack の interactive message を Google Apps Script (GAS) で実現したので、自分用にメモ。

Setting

GAS

プロジェクトファイルを作成し、Webアプリとして公開→URL取得

Slack

SlackAPIからAppを作成し、Interactive ComponentsのURLに、GASのエンドポイントを設定

Google Apps Script

sendMessage()

この function によって,Slackにボタンとリストが付いたメッセージを送信できる.

    //=================================
    // sendMsgWithButton()
    //=================================
    function sendMsgWithButton() {

      // slack channel url (where to send the message)
      var slackUrl = [SlackIncomingWebhookURL];

      // message text  
      var messageData = {
        "text": "Here's your interactive buttons message.",
        "attachments": [
          {
            "text": "Can you click the button?",
            "fallback": "Sorry, no support for buttons.",
            "callback_id": "ButtonResponse",
            "color": "#3AA3E3",
            "attachment_type": "default",
            "actions": [
              {
                    "name": "button",
                    "text": "OK",
                    "style": "primary",
                    "type": "button",
                    "value": "ok"
              },
              {
                    "name": "button",
                    "text": "NO",
                    "style": "danger",
                    "type": "button",
                    "value": "no"
              }
            ]
          },
          {
            "text":"Can you select one from the list?",
            "callback_id": "ptNotificationButtonResponse2",
            "color":"warning",
            "actions":[
              {
                "name": "select",
                "text": "Select test",
                "style": "primary",
                "type": "select",
                "options": [
                  {
                    text:"A",
                    value:"a"
                  },
                  {
                    text:"B",
                    value:"b"
                  },
                  {
                    text:"C",
                    value:"c"
                  }
                ]
              }
            ]
          }
        ]      
      }

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

doPost(e)

Buttonを押すと飛ぶHTTP Postを受取り,選択肢によって異なる反応を示すように設定.

    function doPost(e) {
      // exploit JSON from payload
      //var data = contents.substr(8); //[payload={JSON}]
      var parameter=e.parameter;
      var data = parameter.payload;
      var json = JSON.parse(decodeURIComponent(data));

      // check answer
      // button
      if (json.actions[0].name == "button"){
        var text = "You clicked " + json.actions[0].value;
      // select
      } else if (json.actions[0].name == "select") {
        var text = "You selected " +  json.actions[0].selected_options[0].value;
      }

      // modify message
      var replyMessage = {
        "replace_original": true,
        "response_type": "in_channel",
        "text": "I see you clicked the button.\n" + text
      };
      return ContentService.createTextOutput(JSON.stringify(replyMessage)).setMimeType(ContentService.MimeType.JSON);     
    }
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした