Skip to content

Instantly share code, notes, and snippets.

@shizone
Created August 6, 2019 03:24
Show Gist options
  • Save shizone/e8f2a765c9f9900143495b5cd613541f to your computer and use it in GitHub Desktop.
Save shizone/e8f2a765c9f9900143495b5cd613541f to your computer and use it in GitHub Desktop.
Slackのチャンネル一覧を定期実行するためのGAS
// エントリーポイント
function ChannelGuide() {
var channels = fetchChannles();
postSlack(channels);
}
// APIトークン
var token = 'thisissecret';
// Incoming WebhookのURL
var webhookUrl = 'thisissecret';
var channelsUrl = 'https://slack.com/api/channels.list?token=' + token + '&exclude_archived=1&pretty=1';
// 除外するチャンネル
var excludePatterns = ['times_.*', 'general', 'random'];
function fetchChannles() {
var channels = JSON.parse(UrlFetchApp.fetch(channelsUrl)).channels;
Logger.log(channels);
var filterdChannels = [];
for (var i = 0; i < channels.length; i++) {
var channel = channels[i];
var exclude = false;
for (var j = 0; j < excludePatterns.length; j++) {
var excludePattern = excludePatterns[j]
pattern = new RegExp(excludePattern);
if (channel.name.match(pattern)) {
exclude = true;
break;
}
}
if (!exclude) {
filterdChannels.push(channel);
}
}
return filterdChannels;
}
function postSlack(channels) {
if (channels.length <= 0) {
return;
}
var baseUrl = webhookUrl;
var headers = {
'Content-Type': 'application/json'
};
var params = {
'channel': '#random',
'username': '番組表',
'icon_emoji': ':tokyo_tower:',
'text': createPostMessage(channels),
}
var options = {
'method': 'POST',
'headers' : headers,
'payload': JSON.stringify(params)
};
UrlFetchApp.fetch(baseUrl, options);
}
function createPostMessage(channels) {
var message = ':zap:チャンネル一覧:zap:(気になるチャンネルに参加してみましょう!)\n';
for (var i = 0; i < channels.length; i++) {
var channel = channels[i];
message += '<#' + channel.id + '|' + channel.name + '>';
if (channel.purpose) {
message += ' ' + channel.purpose.value;
}
message += '\n';
}
return message;
}
@shizone
Copy link
Author

shizone commented Aug 6, 2019

  • SlackのAPIトークン
  • Incoming webhookのURL
  • 除外するチャンネルリスト

を設定したら動きます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment