This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack-delete-history.js
91 lines (67 loc) · 2.42 KB
/
slack-delete-history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const https = require('https');
const args = process.argv.slice(2);
const ts_to = args[0];
const token = args[1];
const channel = args[2];
if (!ts_to || !token) {
console.log("Must provide timestamp and token","\n","$ node ./slack-delete-history.js [timestamp] [token]");
return;
}
const isPrivateChannel = false;
const delay = 500; // Don't get Throttled! ;)
const channelApi = isPrivateChannel ? 'groups' : 'channels';
const baseApiUrl = 'https://slack.com/api/';
const historyApiUrl = baseApiUrl + channelApi + '.history?token=' + token + '&count=1000&channel=' + channel + '&latest=' + ts_to + '&oldest=' + 0;
const deleteApiUrl = baseApiUrl + 'chat.delete?token=' + token + '&channel=' + channel + '&ts='
const messages = [];
// ---------------------------------------------------------------------------------------------------------------------
function deleteMessage() {
if (messages.length == 0) {
return;
}
const ts = messages.pop();
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(response);
messages.push(ts);
}
setTimeout(deleteMessage, delay);
});
}).on('error', function (e) {
console.log("Got an error: ", e);
});
}
// ---------------------------------------------------------------------------------------------------------------------
console.log("Beginning Bulk Delete", historyApiUrl);
https.get(historyApiUrl, function(res) {
let body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
const response = JSON.parse(body);
console.log(response);
if (response.ok === false) {
return;
}
if (!response.messages) {
console.log(response);
return;
}
for (let i = 0; i < response.messages.length; i++) {
console.log(i, response.messages[i].ts);
messages.push(response.messages[i].ts);
}
deleteMessage();
});
}).on('error', function (e) {
console.log("Got an error: ", e);
});