How To Backup Slack Chat History

If there are any issues that prevent you from completing the rewrite, please notify us with the error message: Unable to process the request due to encountered difficulties.

Slack has rapidly emerged as the primary communication platform for companies and teams around the globe. Thanks to its diverse range of tools and effortless integration with numerous productivity apps, it’s no surprise that teams heavily depend on it for their daily operations. However, what if you need to safeguard your Slack chat records? Whether it’s for compliance, auditing, or simply for documentation purposes, creating a backup of your chat history can be beneficial.

Today, we will guide you on how to do just that. But first, let’s understand some basics.

Is Slack Chat History Stored?

The length of time that Slack stores your messages depends on your team’s plan. Free teams will have access to the most recent 10,000 messages. Standard, Plus, and Enterprise Grid plans have unlimited message history.

Backing Up Slack Chat History

Unfortunately, Slack does not provide a built-in feature to backup or export your chat history. However, there are a couple of workarounds that you can use. One such method is using Slack’s API.

Note: you will need administrative privileges to perform this action.

Using Slack’s API

Slack provides an API that you can use to pull your chat history. Here’s a simple step-by-step process:

Step 1: Create a Slack App

First, you need to create a Slack App to access the API. Here’s how to do it:

  1. Visit https://api.slack.com/apps?new_app=1 and click on the ‘Create New App’ button.
  2. Give a name to your app and select the workspace you want to back up chat history from, then click on Create App.

Step 2: Get Your API Token

After you’ve created your app, you need to get your API token. Follow these steps:

  1. From the left sidebar of your app page, click on ‘OAuth & Permissions’.
  2. Scroll down to find the ‘Bot Token Scopes’ section and click on ‘Add an OAuth Scope’.
  3. Select the ‘channels:history’ scope for public channels, or ‘groups:history’ for private channels and direct messages.
  4. After adding the necessary scopes, scroll up to the top of the page and click on ‘Install App to Workspace’, and then ‘Allow’.
  5. You will be redirected back to the ‘OAuth & Permissions’ page, under the ‘Tokens for Your Workspace’ section, you will see your ‘Bot User OAuth Token’. This is your API token.

Step 3: Use the API to Backup Your Chat History

Next, you will use this token to backup your chat history. Here’s a Python script that uses Slack’s API to retrieve and backup chat history:

    import requests
    import json

    token = "your-bot-user-oauth-token"    # Replace with your bot user OAuth token
    channel = "channel-id"    # Replace with your channel ID

    def get_history(token, channel, latest='now'):
        if latest == 'now':
            latest = ''
        payload = {
            'token': token,
            'channel': channel,
            'latest': latest,
            'count': 1000
        }
        r = requests.get('https://slack.com/api/conversations.history', params=payload)
        return r.json()

    def write_to_file(filename, messages):
        with open(filename, 'a') as f:
            for message in messages:
                f.write(json.dumps(message) + '\n')

    filename = 'chat_history.json'
    latest = 'now'
    while True:
        history = get_history(token, channel, latest)
        messages = history['messages']
        write_to_file(filename, messages)
        if history['has_more']:
            latest = messages[-1]['ts']
        else:
            break
    

This script will create a file named ‘chat_history.json’ that contains your chat history.

Remember, you will need to have Python and the requests package installed on your machine to run this script.

Conclusion

While backing up Slack chat history might seem like a daunting task initially, it’s not as complex as it appears. Using Slack’s API is an effective way to retrieve and backup your chat history. So, the next time you need to backup your messages, remember this guide.

Happy Slacking!