Skip to content

Instantly share code, notes, and snippets.

@kyo-ago
Created August 16, 2022 00:46
Show Gist options
  • Save kyo-ago/fdf033bf5d11bfb52cef22bf3dbce3ec to your computer and use it in GitHub Desktop.
Save kyo-ago/fdf033bf5d11bfb52cef22bf3dbce3ec to your computer and use it in GitHub Desktop.
slack channel rename bot
var parseParams = function (qs) {
return qs.split(/&/).reduce((base, cur) => {
const kv = cur.split(/=/);
base[decodeURIComponent(kv.shift())] = decodeURIComponent(kv.join('='));
return base;
}, {});
}
function doPost(e) {
var prop = PropertiesService.getScriptProperties().getProperties();
var SlackApp = SlackClient.create(prop.SLACK_OAUTH_ACCESS_TOKEN);
var conversationsLeave = function (channel) {
return SlackApp.fetch_(Utilities.formatString('%s/conversations.leave', SlackApp.apiUrl), { method: 'post', payload: {
channel: channel
}
});
}
var conversationsRename = function (channel, name) {
return SlackApp.fetch_(Utilities.formatString('%s/conversations.rename', SlackApp.apiUrl), { method: 'post', payload: {
channel: channel,
name: name
}
});
}
var responseMessage = function (url, message, type) {
return SlackApp.fetch_(url, { method: 'post', payload: {
"text": message,
"response_type": type || "in_channel"
}
});
}
var postData = e.postData.getDataAsString();
var req = parseParams(postData);
if (req.token !== prop.SLACK_VERIFICATION_TOKEN) {
throw new Error("Invalid token");
}
delete req.token;
if (!req.channel_id) {
throw new Error("channel_id not found");
}
if (!req.response_url) {
throw new Error("response_url not found");
}
if (!req.text) {
responseMessage(req.response_url, "新しいチャンネル名が指定されてません。 `/rename-channel <変更したいチャンネル名>` といった形で指定してください:pray:", "ephemeral");
return;
}
conversationsRename(req.channel_id, req.text);
// conversationsLeave(req.channel_id);
responseMessage(req.response_url, "変わったかな?", "ephemeral");
return ContentService.createTextOutput("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment