Created
April 1, 2019 15:40
-
-
Save pragmat1c/f110582754e66f0217e0aa125e6e80f8 to your computer and use it in GitHub Desktop.
Bulk delete a bunch of slack messages. Funny story: Someone put something on my desk and somehow it landed just right and the + and enter key were held down and I created a bunch of messages in a slack channel. Rather than deleting one by one, I found this script. I forget where I got out, otherwise I'd give proper credit. However it works (it's…
This file contains hidden or 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 = ''; // You can learn it from: https://api.slack.com/custom-integrations/legacy-tokens | |
// GLOBALS ############################################################################################################# | |
let channel = ''; | |
if (process.argv[0].indexOf('node') !== -1 && process.argv.length > 2) { | |
channel = process.argv[2]; | |
} else if (process.argv[0].indexOf('delete') !== -1 && process.argv.length > 1) { | |
channel = process.argv[1]; | |
} else { | |
console.log('Usage: node ./delete-slack-messages.js CHANNEL_ID'); | |
process.exit(1); | |
} | |
const https = require('https'); | |
const baseApiUrl = 'https://slack.com/api/'; | |
const messages = []; | |
const historyApiUrl = baseApiUrl + 'conversations.history?token=' + token + '&count=1000&channel=' + channel + '&cursor='; | |
const deleteApiUrl = baseApiUrl + 'chat.delete?token=' + token + '&channel=' + channel + '&ts=' | |
let delay = 300; // Delay between delete operations in milliseconds | |
let nextCursor = ''; | |
// --------------------------------------------------------------------------------------------------------------------- | |
function deleteMessage() { | |
if (messages.length == 0) { | |
if (nextCursor) { | |
processHistory(); | |
} | |
return; | |
} | |
const ts = messages.shift(); | |
https.get(deleteApiUrl + ts, function (res) { | |
let body = ''; | |
res.on('data', function (chunk) { | |
body += chunk; | |
}); | |
res.on('end', function(){ | |
const response = JSON.parse(body); | |
if (response.ok === true) { | |
console.log(ts + ' deleted!'); | |
} else if (response.ok === false) { | |
console.log(ts + ' could not be deleted! (' + response.error + ')'); | |
if (response.error === 'ratelimited') { | |
delay += 100; // If rate limited error caught then we need to increase delay. | |
messages.push(ts); | |
} | |
} | |
setTimeout(deleteMessage, delay); | |
}); | |
}).on('error', function (e) { | |
console.error("Got an error: ", e); | |
}); | |
} | |
// --------------------------------------------------------------------------------------------------------------------- | |
function processHistory() { | |
https.get(historyApiUrl + nextCursor, function(res) { | |
let body = ''; | |
res.on('data', function (chunk) { | |
body += chunk; | |
}); | |
res.on('end', function () { | |
nextCursor = null; | |
const response = JSON.parse(body); | |
if (response.messages && response.messages.length > 0) { | |
if (response.has_more) { | |
nextCursor = response.response_metadata.next_cursor; | |
} | |
for (let i = 0; i < response.messages.length; i++) { | |
if(response.messages[i].text === '+') { | |
console.log('response.messages[i]', response.messages[i]); | |
messages.push(response.messages[i].ts); | |
} | |
} | |
deleteMessage(); | |
} | |
}); | |
}).on('error', function (e) { | |
console.error("Got an error: ", e); | |
}); | |
} | |
// --------------------------------------------------------------------------------------------------------------------- | |
processHistory(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment