Skip to content

Instantly share code, notes, and snippets.

@adam8810
Created June 22, 2017 22:35
Show Gist options
  • Save adam8810/a25aed6eb5621af221d0c3a36c527e24 to your computer and use it in GitHub Desktop.
Save adam8810/a25aed6eb5621af221d0c3a36c527e24 to your computer and use it in GitHub Desktop.
Clear files from channel
var
Slack = require('slack-node'),
T = require ('data.task'),
R = require ('ramda'),
M = require ('control.monads'),
logI = function (x) { console.log (x); return x; },
id = function (x) { return x; },
konst = R.curry (function (a, b) { return a; }),
du = function (M) { return function () { return R.apply (R.pipe, arguments)(M.of ({})); }; },
bind = function (a) { return R.chain (konst (a)); },
chain = R.chain,
map = R.map,
ap = R.curry (function (ma, mf) { return mf.ap (ma); }),
random = R.curry (function (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }),
runTaskIO = R.curry (function (rej, res, t) { return IO (function () { t.fork (rej, res); }); }),
apiToken = "xoxp-TOKEN",
getFileIds = function (slack, channel) {
return new T (function (reject, resolve) {
slack.api("files.list", { channel: channel }, function(err, response) {
if (err) { return reject (err); }
return resolve (map (R.prop ('id'), R.prop ('files', response)));
});
});
},
deleteFile = R.curry (function (slack, fileId) {
return new T (function (reject, resolve) {
slack.api ('files.delete', { file: fileId }, function (err, res) {
if (err) { return reject (err); }
return resolve (res);
});
});
}),
main = function () {
const
slack = new Slack(apiToken),
channel = "CHANNEL_NAME";
du (T) ( bind (getFileIds (slack, channel))
, map (map (deleteFile (slack)))
, chain (M.sequence (T))
, id ).fork (logI, logI);
},
nil = null;
main ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment