SlackのBotのメッセージを削除する[GAS]
Slackで自作したBotの投稿メッセージをGASで削除する方法です。誤ってメッセージを投稿した時に備えてスクリプトを用意しておきます。
スクリプトはこちら。
あらかじめスクリプトプロパティでTokenを用意しておきます。
const LINK = "https://********.slack.com/archives/C*******/p********";
function deleteSlackMessage() {
const token = PropertiesService.getScriptProperties().getProperty('********');
const url = 'https://slack.com/api/chat.delete';
const { channelId, ts } = parseSlackMessageUrl(LINK);
const options = {
"method": "post",
"contentType": "application/x-www-form-urlencoded",
"payload": {
"token": token,
"channel": channelId,
"ts": ts,
"muteHttpExceptions": true
}
};
UrlFetchApp.fetch(url, options);
}
function parseSlackMessageUrl(link) {
const regex = /archives\/(C\w+)\/p(\d{16})/;
const match = link.match(regex);
const channelId = match[1];
const rawTs = match[2];
const ts = rawTs.slice(0, 10) + "." + rawTs.slice(10);
return { channelId: channelId, ts: ts };
}削除したいメッセージをSlack上でコピーし、1行目「const LINK」の後のURLにペーストしてからスクリプトを実行するとメッセージが削除されます。
