Skip to content

Instantly share code, notes, and snippets.

@bkuri
Last active August 29, 2015 14:27
Show Gist options
  • Save bkuri/d8bceb88aeef8188bdbc to your computer and use it in GitHub Desktop.
Save bkuri/d8bceb88aeef8188bdbc to your computer and use it in GitHub Desktop.
Fetch *all* slack IMs from a specific user and save them as a JSON file
#!/bin/coffee
_ = (require 'lodash')
fs = (require 'fs')
request = (require 'request')
vantage = (require 'vantage')()
FOLDER = '/tmp'
PORT = 8080
TOKEN = 'add your token here'
URL = 'https://slack.com/api'
req = request.defaults qs: token: TOKEN
vantage
.command('users')
.description('fetch users')
.action (args, callback) ->
req.get "#{URL}/users.list", (error, response, body) =>
list = []
users = (JSON.parse body).members
(list.push user.name) for user in users
@log "listing #{users.length} users:\n#{list.join ', '}"
callback()
vantage
.command('messages <user>')
.description('fetch all DMs from/to <user>.')
.action (args, callback) ->
@log "fetching IMs for user '#{args.user}'..."
messages = []
add = (channel, ims, finished) =>
messages = (messages.concat ims)
if finished
@log "fetched #{messages.length} messages from user '#{args.user}'"
fs.writeFile "#{FOLDER}/#{args.user}.json", (JSON.stringify messages), (error) ->
callback()
else (fetch channel, (_.last ims).ts)
fetch = (channel, latest=0) ->
# adding the query parameters as an object didn't work for some reason...
req.get "#{URL}/im.history?channel=#{channel}&latest=#{latest}&count=1000&inclusive=1", (error, response, body) =>
body = (JSON.parse body)
add channel, body.messages, !body.has_more
req.get "#{URL}/users.list", (error, response, body) =>
user = _.result (_.find (JSON.parse body).members, name: args.user), 'id'
# @log "id: #{user}"
req.get "#{URL}/im.list", (error, response, body) =>
channel = _.result (_.find (JSON.parse body).ims, {user} ), 'id'
# @log "channel: #{channel}"
fetch channel
vantage
.delimiter('slack~$')
.listen(PORT)
.show()
@bkuri
Copy link
Author

bkuri commented Aug 11, 2015

This nodejs script will either list all fellow slack users or download all direct messages that you have with a specific user as a JSON file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment