Export all Slack logs as a non-admin user

Have you ever wanted to export all Slack logs for your team but you are just too awkward to ask one of your team admins to export everything properly? Now there is a solution.

I have written a Python script called slack-exporter (I’m good with original names as you can see) that allows you to do exactly that but as a regular user with no access to Slack’s official export page.

Not quite trusted yet

Background

The script relies on the channels.history API method. It returns at most 100 messages will always return the most recent 100 messages if there are more messages than that between the oldest and the latest dates you specify. You can use this method to fetch all of the channel’s message history by doing this:

  1. Fetch the 100 messages with your intended oldest and latest dates
  2. Get another 100 messages again but with oldest being your original oldest date and latest being the timestamp of the oldest message in your previous batch
  3. Continue until you get a response which says there is no more data (has_more set to false)

Usage

You can try this script yoursel by installing it with pip like this:

pip install https://github.com/lbn/slack-exporter/archive/0.0.1.zip

To run this script you have to generate a Slack API token here. Next, you should create a file in your home directory called something like .slack-exporter-env and chmod 0600 it. The contents of this file should be the following:

export SLACK_TOKEN=xoxp-123456...

Replace the part after = with your actual token.

Add source ~/.slack-exporter-env to ~/.zshrc or /.bashrc, depending on your shell. Reload your shell using exec -l $SHELL and the Slack token should be in your environment variables.

The most basic use case is exporting everything. This can be done by executing slack-exporter with no arguments.

Here is the full list of arguments you can pass to it:

optional arguments:
  -h, --help            show this help message and exit
  --data DATA_DIR       Data directory where channel, user and message data
                        will be saved (default: data)
  --archived ARCHIVED   Also download history for archived channels (default:
                        False)
  --users USERS         Save user data to DATA_DIR/users.json (default: True)
  --messages MESSAGES   Save user data to DATA_DIR/channels (default: True)
  --min-members MIN_MEMBERS
                        Only export channels which have at least this number
                        of members (default: 0)
  --date-start DATE_START
                        History start date - format YYYY-MM-DD (default: the
                        beginning of time)
  --date-end DATE_END   History end date - format YYYY-MM-DD (default:
                        2016-06-03)

This is what the output data directory looks like:

$ tree ./data
./data
├── channels.json
├── logs
│   ├── channel-one.json
│   ├── ...
│   └── channel-six.json
└── users.json

Every file in the logs directory is actually valid JSON and not newline delimited JSON.