Last active
September 28, 2019 09:55
-
-
Save AWtnb/4e6ee362b34dadf7b1a5e46901e7ca90 to your computer and use it in GitHub Desktop.
notify new Channel on slack
This file contains 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 に通知 | |
*/ | |
/////////////////////////////////////////////////////// | |
// グローバル変数 | |
/////////////////////////////////////////////////////// | |
// シート | |
var SHEET_ID = PropertiesService.getScriptProperties().getProperty("SHEET_ID"); | |
var sht = SpreadsheetApp.openById(SHEET_ID).getSheets(); | |
// token | |
var ACCESS_TOKEN = PropertiesService.getScriptProperties().getProperty("ACCESS_TOKEN"); | |
// チャンネル | |
var CHANNEL_ID = PropertiesService.getScriptProperties().getProperty("CHANNEL_ID"); | |
/////////////////////////////////////////////////////// | |
// 応答処理 | |
/////////////////////////////////////////////////////// | |
function doPost (e) { | |
// リクエスト内容をパース | |
var postData = JSON.parse(e.postData.getDataAsString()); | |
var contents = JSON.parse(e.postData.contents); | |
// 認証( url_verification が送信された場合は challenge をオウム返しする) | |
if(postData.type == "url_verification") { | |
var res = {"challenge":postData.challenge} | |
return ContentService.createTextOutput(JSON.stringify(res)).setMimeType(ContentService.MimeType.JSON); | |
} | |
// 無効な入力を弾く・無限ループ回避 | |
if (e == null || e.postData == null) { | |
return; | |
} | |
// シートに加筆 | |
var eventType = contents.event.type; | |
if (eventType == "channel_created") { | |
var channelName = contents.event.channel.name; | |
var channelID = contents.event.channel.id; | |
} | |
else { | |
var channelName = "obsolete_channel"; | |
var channelID = contents.event.channel; | |
} | |
sht[0].appendRow([new Date(), channelName, channelID, eventType]); | |
} | |
/////////////////////////////////////////////////////// | |
// slack 関連 | |
/////////////////////////////////////////////////////// | |
// slack に chat.postMessage 経由でメッセージを投稿する関数 | |
function postAsBot (text) { | |
var payload = { | |
"token": ACCESS_TOKEN, | |
"channel": CHANNEL_ID, | |
"text": text | |
}; | |
var options = { | |
"method" : "POST", | |
"payload": payload | |
} | |
UrlFetchApp.fetch("https://slack.com/api/chat.postMessage", options); | |
} | |
/////////////////////////////////////////////////////// | |
// 通知処理 | |
/////////////////////////////////////////////////////// | |
// 昨日追加されたチャンネルを取得 | |
function getNewChannel () { | |
// 日付 | |
var now = new Date(); | |
var startOfToday = new Date(now.getYear(), now.getMonth(), now.getDate(), 00, 00); | |
var startOfYesterday = new Date(now.getYear(), now.getMonth(), now.getDate()-1, 00, 00); | |
// チャンネル | |
var newChannel = []; | |
// 削除・アーカイブされたもの | |
var exception = []; | |
// データ取得 | |
var maxRow = sht[0].getLastRow(); | |
var data = sht[0].getRange(1, 1, maxRow, 4).getValues(); | |
for (var r = maxRow - 1; r >= 0; r--) { | |
// 2日以上前の場合は抜ける | |
var addDate = new Date(data[r][0]); | |
if (addDate.getTime() < startOfYesterday.getTime()) { | |
break; | |
} | |
var info = " <#" + data[r][2] + "|CHANNELNAME>"; // ID 優先でリンクが貼られるのでチャンネル名は適当 | |
if (data[r][3] == "channel_archive" || data[r][3] == "channel_deleted") { | |
exception.push(info); | |
} | |
// 今日になってから追加されたチャンネルはスキップ | |
if (addDate.getTime() > startOfToday.getTime()) { | |
continue; | |
} | |
newChannel.push(info); | |
} | |
// 削除されていないチャンネルだけ取得 | |
var ret = []; | |
for (var i = 0; i < newChannel.length; i++) { | |
if (exception.indexOf(newChannel[i]) == -1) { | |
ret.push(newChannel[i]); | |
} | |
} | |
return ret; | |
} | |
// 新規チャンネル情報を投稿 | |
function notifyNewChannel () { | |
var newChannel = getNewChannel(); | |
if (newChannel.length < 1) { | |
return; | |
} | |
var msg = "昨日、" + newChannel.length + "個のチャンネルが作成されました!\n" + newChannel.join("\n") | |
postAsBot(msg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment