Last active
November 5, 2024 13:15
-
-
Save firatkucuk/ee898bc919021da621689f5e47e7abac to your computer and use it in GitHub Desktop.
Deletes slack public/private channel messages, private chat messages and channel thread replies.
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
#!/usr/bin/env node | |
// Channel ID is on the the browser URL.: https://mycompany.slack.com/messages/MYCHANNELID/ | |
// Pass it as a parameter: node ./delete-slack-messages.js CHANNEL_ID | |
// CONFIGURATION ####################################################################################################### | |
const token = 'SLACK TOKEN'; | |
// Legacy tokens are no more supported. | |
// Please create an app or use an existing Slack App | |
// Add following scopes in your app from "OAuth & Permissions" | |
// - channels:history | |
// - groups:history | |
// - im:history | |
// - mpim:history | |
// - chat:write | |
// VALIDATION ########################################################################################################## | |
if (token === 'SLACK TOKEN') { | |
console.error('Token seems incorrect. Please open the file with an editor and modify the token variable.'); | |
} | |
let channel = ''; | |
if (process.argv[0].indexOf('node') !== -1 && process.argv.length > 2) { | |
channel = process.argv[2]; | |
} else if (process.argv.length > 1) { | |
channel = process.argv[1]; | |
} else { | |
console.log('Usage: node ./delete-slack-messages.js CHANNEL_ID'); | |
process.exit(1); | |
} | |
// GLOBALS ############################################################################################################# | |
const https = require('https') | |
const historyApiUrl = `/api/conversations.history?channel=${channel}&count=1000&cursor=`; | |
const deleteApiUrl = '/api/chat.delete'; | |
const repliesApiUrl = `/api/conversations.replies?channel=${channel}&ts=` | |
let delay = 300; // Delay between delete operations in milliseconds | |
// --------------------------------------------------------------------------------------------------------------------- | |
const sleep = delay => new Promise(r => setTimeout(r, delay)); | |
const request = (path, data) => new Promise((resolve, reject) => { | |
const options = { | |
hostname: 'slack.com', | |
port : 443, | |
path : path, | |
method : data ? 'POST' : 'GET', | |
headers : { | |
'Authorization': `Bearer ${token}`, | |
'Content-Type' : 'application/json; charset=utf-8', | |
'Accept' : 'application/json' | |
} | |
}; | |
const req = https.request(options, res => { | |
let body = ''; | |
res.on('data', chunk => (body += chunk)); | |
res.on('end', () => resolve(JSON.parse(body))); | |
}); | |
req.on('error', reject); | |
if (data) { | |
req.write(JSON.stringify(data)); | |
} | |
req.end(); | |
}); | |
// --------------------------------------------------------------------------------------------------------------------- | |
async function deleteMessages(threadTs, messages) { | |
if (messages.length == 0) { | |
return; | |
} | |
const message = messages.shift(); | |
if (message.thread_ts !== threadTs) { | |
await fetchAndDeleteMessages(message.thread_ts, ''); // Fetching replies, it will delete main message as well. | |
} else { | |
const response = await request(deleteApiUrl, {channel: channel, ts: message.ts}); | |
if (response.ok === true) { | |
console.log(message.ts + (threadTs ? ' reply' : '') + ' deleted!'); | |
} else if (response.ok === false) { | |
console.log(message.ts + ' could not be deleted! (' + response.error + ')'); | |
if (response.error === 'ratelimited') { | |
await sleep(1000); | |
delay += 100; // If rate limited error caught then we need to increase delay. | |
messages.unshift(message); | |
} | |
} | |
} | |
await sleep(delay); | |
await deleteMessages(threadTs, messages); | |
} | |
// --------------------------------------------------------------------------------------------------------------------- | |
async function fetchAndDeleteMessages(threadTs, cursor) { | |
const response = await request((threadTs ? repliesApiUrl + threadTs + '&cursor=' : historyApiUrl) + cursor); | |
if (!response.ok) { | |
console.error(response.error); | |
return; | |
} | |
if (!response.messages || response.messages.length === 0) { | |
return; | |
} | |
await deleteMessages(threadTs, response.messages); | |
if (response.has_more) { | |
await fetchAndDeleteMessages(threadTs, response.response_metadata.next_cursor); | |
} | |
} | |
// --------------------------------------------------------------------------------------------------------------------- | |
fetchAndDeleteMessages(null, ''); |
Can you use a lower version nodejs? It seems I need to update the code a
little bit.
Op ma 5 feb. 2024 14:32 schreef joshikumarav ***@***.***>:
… ***@***.**** commented on this gist.
------------------------------
I am getting the below error - any idea how?
(node:16837) UnhandledPromiseRejectionWarning: Error: certificate has
expired
at TLSSocket. (_tls_wrap.js:1116:38)
at emitNone (events.js:106:13)
at TLSSocket.emit (events.js:208:7)
at TLSSocket._finishInit (_tls_wrap.js:643:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:473:38)
(node:16837) UnhandledPromiseRejectionWarning: Unhandled promise
rejection. This error originated either by throwing inside of an async
function without a catch block, or by rejecting a promise which was not
handled with .catch(). (rejection id: 1)
(node:16837) [DEP0018] DeprecationWarning: Unhandled promise rejections
are deprecated. In the future, promise rejections that are not handled will
terminate the Node.js process with a non-zero exit codJKs-Mac:ee898bc919
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/firatkucuk/ee898bc919021da621689f5e47e7abac#gistcomment-4876884>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAC2QSW74DWXGSV62IFZTPTYSDNQNBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA3DSNZXG42DSMVHORZGSZ3HMVZKMY3SMVQXIZI>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
then probably your token does not have enough permissons to delete it. Try
to create a token with a more priviliged user.
yeah it should work in that case you can check the claims of the token.
Excellent script - thanks @firatkucuk !
@im-syk et al. - if anyone is interested in a version of this that deletes last 30 days, I made a version that does that. To change how long it keeps things for, update the keepTime
variable as needed.
Thanks a lot, all work fine.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for your reply, I got it, the idea is to run the script as a second process with the fork command, many thanks I got it now.