Last active
March 27, 2019 03:31
-
-
Save AWtnb/9f4ca614cd26b8dc492b5cb70eba671b to your computer and use it in GitHub Desktop.
dummy robot on slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| slash command | |
| 言いたいことをロボットに代弁してもらう | |
| scope | |
| chat:write:bot | |
| interactive components | |
| */ | |
| ///////////////////////////////////////////////////////////////////////// | |
| // グローバル変数 | |
| ///////////////////////////////////////////////////////////////////////// | |
| // ログ用シート | |
| var SHEET_ID = PropertiesService.getScriptProperties().getProperty("SHEET_ID"); | |
| var sht = SpreadsheetApp.openById(SHEET_ID).getSheets(); | |
| //token | |
| var BOT_TOKEN = PropertiesService.getScriptProperties().getProperty("BOT_TOKEN"); | |
| ///////////////////////////////////////////////////////////////////////// | |
| // slack 投稿用 | |
| ///////////////////////////////////////////////////////////////////////// | |
| // slack に chat.postMessage 経由でメッセージを投稿する関数 | |
| function postAsBot (text, channelID) { | |
| var payload = { | |
| "token": BOT_TOKEN, | |
| "channel": channelID, | |
| "text": text | |
| }; | |
| var options = { | |
| "method" : "POST", | |
| "payload": payload | |
| } | |
| UrlFetchApp.fetch("https://slack.com/api/chat.postMessage", options); | |
| } | |
| // slack の投稿に返信する関数 | |
| function respondAsBot (text, hookUrl) { | |
| var payload = { | |
| "text": text, | |
| "blocks": [ | |
| { | |
| "type": "section", | |
| "text": { | |
| "type": "mrkdwn", | |
| "text": text | |
| } | |
| } | |
| ] | |
| }; | |
| var options = { | |
| "method" : "POST", | |
| "payload": JSON.stringify(payload) | |
| } | |
| UrlFetchApp.fetch(hookUrl, options); | |
| } | |
| // ボタンを作成する関数 | |
| function makeButton (text, value) { | |
| return { | |
| "type": "button", | |
| "text": { | |
| "type": "plain_text", | |
| "text": text, | |
| "emoji": true | |
| }, | |
| "value": value | |
| }; | |
| } | |
| ///////////////////////////////////////////////////////////////////////// | |
| // メイン処理 | |
| ///////////////////////////////////////////////////////////////////////// | |
| // このスクリプトにPOSTされた内容を受け取って処理する関数 | |
| function doPost(e) { | |
| // payload の有無で slash command 経由で呼び出されたか判定 | |
| if (e.parameter.payload == null) { | |
| var text = e.parameter.text; | |
| var msg = "以下の内容をロボットに代弁してもらいます\r\n(トラブル回避のためにログを収集しています)"; | |
| var actionBlock = [ | |
| { | |
| "type": "section", | |
| "text": { | |
| "type": "mrkdwn", | |
| "text": msg | |
| } | |
| }, | |
| { | |
| "type": "context", | |
| "elements": [{"type": "mrkdwn","text": text}] | |
| }, | |
| { | |
| "type": "actions", | |
| "elements": [ | |
| makeButton("OK", text), | |
| makeButton("Cancel", "Cancel") | |
| ] | |
| } | |
| ]; | |
| var response = { | |
| "response_type": "ephemeral", | |
| "replace_original": true, | |
| "text": msg, | |
| "blocks": actionBlock | |
| }; | |
| return ContentService.createTextOutput(JSON.stringify(response)).setMimeType(ContentService.MimeType.JSON); | |
| } | |
| else { | |
| // ボタンへの反応に応答する | |
| var payload = JSON.parse(e.parameter.payload); | |
| var channelId = payload.channel.id; | |
| var channelName = payload.channel.name; | |
| var resUrl = payload.response_url; | |
| var pressed = payload.actions[0].value; | |
| var userName = payload.user.username; | |
| if (pressed == "Cancel") { | |
| respondAsBot("キャンセルされました\r\n(ログは保存されていません)", resUrl); | |
| } | |
| else { | |
| respondAsBot("ロボットに代弁してもらいました!", resUrl); | |
| // シートに記入 | |
| sht[0].appendRow([new Date(),userName, channelName, pressed]); | |
| // BOT として投稿 | |
| postAsBot(pressed, channelId); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment