Skip to content

Instantly share code, notes, and snippets.

@setomits
Created August 31, 2024 00:17
Show Gist options
  • Save setomits/71713138b189dc9ce5559153c823a0bb to your computer and use it in GitHub Desktop.
Save setomits/71713138b189dc9ce5559153c823a0bb to your computer and use it in GitHub Desktop.
'use strict';
function deleteDMsByChannelId() {
const user_id = ''; // User ID : Uxxxxxxxxx
const token = ''; // Slack API の Userトークン : xoxp-xxxxx
const channel_id = ''; // チャンネルID(DMの中のメッセージのリンクの「archive/Dxxxx」の部分)
let has_more = true;
let list_options = {
'method': 'post',
'payload': {
'token': token,
'channel': channel_id,
'limit': 999
}
}
while (has_more) {
const list_resp = UrlFetchApp.fetch('https://slack.com/api/conversations.history', list_options);
const data = JSON.parse(list_resp.getContentText());
const messages = data.messages;
// 自分が送ったメッセージのみを削除
for (let i=0; i<messages.length; i++) {
if(messages[i].user !== user_id) {
continue;
}
const mes_options = {
'method': 'post',
'payload': {
'token': token,
'channel': channel_id,
'ts': messages[i].ts
}
}
const mes_resp = UrlFetchApp.fetch('https://slack.com/api/chat.delete', mes_options);
const data = JSON.parse(mes_resp.getContentText());
if (data.ok) {
// 削除したメッセージの ts
console.log(data.ts)
}
// sleep 2 seconds
Utilities.sleep(2000);
}
has_more = data.has_more;
if (has_more) {
list_options.payload.cursor = data.response_metadata.next_cursor;
console.log('next round : ' + data.response_metadata.next_cursor);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment