Skip to content

Instantly share code, notes, and snippets.

@drbh
Last active March 26, 2018 18:55
Show Gist options
  • Select an option

  • Save drbh/307c33d92dcc17ce95ed2ff3051a22dd to your computer and use it in GitHub Desktop.

Select an option

Save drbh/307c33d92dcc17ce95ed2ff3051a22dd to your computer and use it in GitHub Desktop.
forked and edited slack cleaner - should be a command line app - or lambda function
var https = require('https');
// CONFIGURATION
var token = 'YOUR TOKEN';
var channel = 'THE CHANNEL ID (not name) YOU WANT TO CLEAN';
var privateChannel = false;
var isDirectMessage = true
var delay = 300; // delay between delete operations in millisecond
// GLOBALS
var channelApi = privateChannel ? 'groups' : 'channels';
var channelApi = isDirectMessage ? 'conversations' : privateChannel
var baseApiUrl = 'https://slack.com/api/';
var historyApiUrl = baseApiUrl + channelApi + '.history?token=' + token + '&count=1000&channel=' + channel;
var deleteApiUrl = baseApiUrl + 'chat.delete?token=' + token + '&channel=' + channel + '&ts='
var messages = [];
function deleteMessage() {
if (messages.length == 0) {
return;
}
var ts = messages.shift();
https.get(deleteApiUrl + ts, function (res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function(){
var response = JSON.parse(body);
if (response.ok === true) {
console.log(ts + ' ' + response.channel + ' deleted!');
} else if (response.ok === false) {
messages.push(ts);
}
setTimeout(deleteMessage, delay);
});
}).on('error', function (e) {
console.log("Got an error: ", e);
});
}
// RUNTIME
https.get(historyApiUrl, function(res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
var response = JSON.parse(body);
console.log(response)
for (var i = 0; i < response.messages.length; i++) {
messages.push(response.messages[i].ts);
}
deleteMessage();
});
}).on('error', function (e) {
console.log("Got an error: ", e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment