Last active
April 22, 2016 12:27
-
-
Save simon-jentzsch/b33023b40179ec55c970 to your computer and use it in GitHub Desktop.
slack message exporter
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
/* | |
this is free software: you can redistribute it and/or modify | |
it under the terms of the GNU Lesser General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
it is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU Lesser General Public License for more details. | |
see <http://www.gnu.org/licenses/>. | |
*/ | |
/** | |
* @file exporter.js | |
* @author: | |
* Simon Jentzsch <[email protected]> | |
* @date 2016 | |
*/ | |
'use strict'; | |
var _ = require("underscore"); | |
var fs = require("fs"); | |
var Slack = require('slack-api'); | |
var token = "YOUR-TOKEN"; // put your token here! | |
Slack.users.list({token:token}, function (error, data) { | |
// create a map with the real usernames | |
var users = {}; | |
data.members.forEach(function(u) { | |
users[u.id]=u.profile.real_name+" ( "+u.name+")"; | |
}); | |
exportAllChannels(users); | |
}); | |
function timestamp(ts) { | |
var p = ts.indexOf("."); | |
var v = new Date(1000 * parseInt(p<0?ts:ts.substr(0,p))).toUTCString(); | |
return v.substring(5, v.indexOf("GMT")-4); | |
} | |
function exportGroup(group, users, api) { | |
// name of the current channel ( I added a # for public and a _ for private ) | |
var name = (group.is_channel ? "#" : "_") + group.name; | |
// get the last 1000 messages | |
api.history({token:token, channel: group.id , count: 1000 }, function (error, slackdata) { | |
var archFile = "archive/"+name+".json"; // file for json-storage | |
fs.readFile(archFile, function (err, data) { | |
var exists = {}, msgs = [], newEntries = false; | |
if (!err && data) { | |
// parse existing data and use the timestamp as id | |
msgs = JSON.parse(data); | |
msgs.forEach(function(m){ exists[m.ts]=m; }); | |
} | |
// for all data, we don't already have we insert them. | |
slackdata.messages.forEach(function(m) { | |
if (!exists[m.ts]) { | |
msgs.splice(0,0,m); | |
newEntries=true; | |
} | |
}); | |
// sort the messages by timestamp | |
msgs = _.sortBy(msgs, function(m) { return 0 - parseFloat(m.ts); }); | |
// export the archive | |
fs.writeFile(archFile ,JSON.stringify(msgs)); | |
if (!newEntries) return; | |
// create a easy to read text-file | |
var all = ""; | |
msgs.forEach(function(msg) { | |
if (msg.type!="message") return; | |
all="#"+timestamp(msg.ts)+" "+(users[msg.user] || msg.user) + "\n"+msg.text+"\n\n"+all; | |
}); | |
fs.writeFile("txt/"+name+".txt" ,all); | |
}); | |
}); | |
} | |
function exportAllChannels(users) { | |
// export private channels | |
Slack.groups.list({token:token}, function (error, data) { | |
data.groups.forEach(function(g) { exportGroup(g,users,Slack.groups);}); | |
}); | |
// export the public channels | |
Slack.channel.list({token:token}, function (error, data) { | |
data.channels.forEach(function(g) { exportGroup(g,users,Slack.channel); }); | |
}); | |
} |
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
{ | |
"name": "slack", | |
"version": "0.1.0", | |
"description": "exports all messages", | |
"homepage": "http://slock.it", | |
"main": "exporter.js", | |
"dependencies": { | |
"slack-api": "^0.1.11", | |
"underscore": "^1.8.3" | |
}, | |
"devDependencies": {}, | |
"keywords": [], | |
"author": "slockt.it", | |
"license": "MIT" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment