Last active
March 28, 2019 03:29
-
-
Save AWtnb/5075799654ed8c17dcca80fb33b80176 to your computer and use it in GitHub Desktop.
send emoji url with slash command
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 | |
| 投稿された絵文字のURLを返すことでスタンプのように投稿する | |
| */ | |
| ///////////////////////////////////////////////////////////////////////// | |
| // グローバル変数 | |
| ///////////////////////////////////////////////////////////////////////// | |
| // token | |
| var ACCESS_TOKEN = PropertiesService.getScriptProperties().getProperty("ACCESS_TOKEN"); | |
| ///////////////////////////////////////////////////////////////////////// | |
| // 事前処理関数 | |
| ///////////////////////////////////////////////////////////////////////// | |
| // emoji の URL を特定する関数 | |
| function getEmojiURL(emojiName) { | |
| var ret = ""; | |
| var res = UrlFetchApp.fetch("https://slack.com/api/emoji.list?token=" + ACCESS_TOKEN) | |
| var list = JSON.parse(res.getContentText()); | |
| var emojis = list.emoji; | |
| Object.keys(emojis).some(function (emj) { | |
| if (emj == emojiName) { | |
| ret = emojis[emj]; | |
| return true; | |
| } | |
| }); | |
| return ret; | |
| } | |
| ///////////////////////////////////////////////////////////////////////// | |
| // slack 関数 | |
| ///////////////////////////////////////////////////////////////////////// | |
| // slack に chat.postMessage 経由でメッセージを投稿する関数 | |
| function postAsBot (text, channelID, emojiUrl) { | |
| var block = [ | |
| { | |
| "type": "image", | |
| "title": { | |
| "type": "plain_text", | |
| "text": text, | |
| "emoji": true | |
| }, | |
| "image_url": emojiUrl, | |
| "alt_text": emojiUrl | |
| } | |
| ]; | |
| var payload = { | |
| "token": ACCESS_TOKEN, | |
| "channel": channelID, | |
| "text": text, | |
| "blocks": JSON.stringify(block) | |
| }; | |
| var options = { | |
| "method" : "POST", | |
| "payload": payload | |
| } | |
| return UrlFetchApp.fetch("https://slack.com/api/chat.postMessage", options); | |
| } | |
| ///////////////////////////////////////////////////////////////////////// | |
| // メイン処理 | |
| ///////////////////////////////////////////////////////////////////////// | |
| // このスクリプトにPOSTされた内容を受け取って処理する関数 | |
| function doPost(e) { | |
| var text = e.parameter.text; | |
| var userName = e.parameter.user_name; | |
| var channelId = e.parameter.channel_id; | |
| text = text.replace(/\s/); | |
| // 絵文字一つが投稿された場合のみ作動 | |
| if (/^:[^:]+:$/.test(text)) { | |
| var emojiName = text.replace(/:/g, ""); | |
| var emojiUrl = getEmojiURL(emojiName); | |
| if (emojiUrl == "") { | |
| var response = { | |
| "response_type": "ephemeral", | |
| "replace_original": true, | |
| "text": "代弁できるのはカスタム絵文字だけなんです……" | |
| }; | |
| response = JSON.stringify(response); | |
| } | |
| else { | |
| var msg = userName + "さんの気持ちを代弁します!"; | |
| postAsBot (msg, channelId, emojiUrl); | |
| var response = ""; | |
| } | |
| } | |
| else { | |
| var response = { | |
| "response_type": "ephemeral", | |
| "replace_original": true, | |
| "text": "絵文字は1つだけ指定してください!" | |
| }; | |
| response = JSON.stringify(response); | |
| } | |
| return ContentService.createTextOutput(response).setMimeType(ContentService.MimeType.JSON); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment