Last active
February 10, 2020 03:54
-
-
Save AWtnb/5b9077c93ec88a524efd529fdfec0ac2 to your computer and use it in GitHub Desktop.
gyotaku 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
| /* | |
| slack のカスタムアクションでシートに記入していく。 | |
| フルに稼働するためにはそのチャンネルにアプリとして追加する必要あり。 | |
| scope: interactive component / users.read / reactions.write / channels:read / groups:read | |
| */ | |
| var PROPERTIES = PropertiesService.getScriptProperties(); | |
| var BOT_USER_TOKEN = PROPERTIES.getProperty("BOT_USER_TOKEN"); | |
| var SHEET_ID = PROPERTIES.getProperty("SHEET_ID"); | |
| var LOG_SHEET = SpreadsheetApp.openById(SHEET_ID); | |
| var sheets = LOG_SHEET.getSheets(); | |
| var logSheet = sheets[0]; | |
| function getUserNameById(userId) { | |
| var payload = { | |
| "token": BOT_USER_TOKEN, | |
| }; | |
| var options = { | |
| "method" : "POST", | |
| "payload": payload | |
| } | |
| var res = UrlFetchApp.fetch("https://slack.com/api/users.list", options); | |
| var jsonData = JSON.parse(res); | |
| var user = jsonData.members.filter(function(item, index){ | |
| if (item.id == userId) return true; | |
| }); | |
| return user[0].profile.real_name | |
| } | |
| function getNameOfPrivateGroup(groupId){ | |
| var payload = { | |
| "token": BOT_USER_TOKEN, | |
| "limit": 150, | |
| "types": "private_channel" | |
| }; | |
| var options = { | |
| "method" : "POST", | |
| "payload": payload | |
| } | |
| var res = UrlFetchApp.fetch("https://slack.com/api/conversations.list", options); | |
| var jsonData = JSON.parse(res); | |
| var group = jsonData.channels.filter(function(item, index){ | |
| if (item.id == groupId) return true; | |
| }); | |
| if (group.length < 1) { | |
| return 0 | |
| } | |
| return group[0].name | |
| } | |
| function addReaction (ts, channelId) { | |
| var payload = { | |
| "token": BOT_USER_TOKEN, | |
| "name": "blowfish", | |
| "channel": channelId, | |
| "timestamp": ts | |
| }; | |
| var options = { | |
| "method" : "POST", | |
| "payload": payload | |
| } | |
| return UrlFetchApp.fetch("https://slack.com/api/reactions.add", options); | |
| } | |
| function doPost(e) { | |
| var payload = e.parameter.payload; | |
| var jsonData = JSON.parse(payload); | |
| var teamDomain = jsonData.team.domain; | |
| var actionBy = jsonData.user.name; | |
| var channelId = jsonData.channel.id; | |
| var channelName = jsonData.channel.name; | |
| if (channelName == "privategroup") { | |
| var groupName = getNameOfPrivateGroup(channelId) | |
| if (groupName != 0) { | |
| channelName = groupName | |
| } | |
| } | |
| var messageTs = jsonData.message.ts; | |
| var message = jsonData.message.text; | |
| var authorId = jsonData.message.user; | |
| var authorName = getUserNameById(authorId); | |
| var res = addReaction(messageTs, channelId); | |
| var newLine = [new Date(), teamDomain, actionBy, authorName, channelName, channelId, messageTs, message]; | |
| logSheet.appendRow(newLine); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment