marcusshep
marcusshep

Reputation: 1964

Slack: Retrieve all messages

I want to retrieve all the messages that were sent in my teams slack domain. Although, I'd prefer that the data be received in XML or JSON I am able to handle the data in just about any form.

How can I retrieve all these messages? Is it possible? If not, can I retrieve all the messages for a specific channel?

Upvotes: 41

Views: 73958

Answers (8)

Pukeko
Pukeko

Reputation: 103

I know that this might be late for the OP, but if anyone is looking for a tool capable of doing the full Slack Workspace export, try Slackdump it's free and open source (I'm the author, but anyone can contribute).

To do the workspace export, run it with -export switch:

./slackdump -export my_export.zip

If you need to download attachments as well, use the -f switch (stands for "files"):

./slackdump -f -export my_export.zip

It will open the browser asking you to login. If you need to do it headless, grab a token and cookie, as described in the documentation

It will generate the export file that would be compatible with another nice tool slack-export-viewer.

Upvotes: 4

SMRITI MAHESHWARI
SMRITI MAHESHWARI

Reputation: 93

In order to retrieve all the messages from a particular channel in slack this can be done by using conversations.history method in slack_sdk library in python.

def get_conversation_history(self, channel_id, latest, oldest):
        """Method to fetch the conversation history of particular channel"""
        try:
            result = client.conversations_history(
                channel=channel_id,
                inclusive=True,
                latest=latest,
                oldest=oldest,
                limit=100)
            all_messages = []
            all_messages += result["messages"]
            ts_list = [item['ts'] for item in all_messages]
            last_ts = ts_list[:-1]
            while result['has_more']:
                result = client.conversations_history(
                    channel=channel_id,
                    cursor=result['response_metadata']['next_cursor'],
                    latest=last_ts)
                all_messages += result["messages"]
            return all_messages
        except SlackApiError as e:
            logger.exception("Error while fetching the conversation history")

Here, i have provided latest and oldest timestamps to cover a time range when we need to collect the messages from the all messages in conversation history.

And the cusor argument is being used to point the next cursor value as this method can only collect 100 messages at one time but it supports pagination through which we can point the next cursor value from result['response_metadata']['next_cursor'].

Hope this will be helpful.

Upvotes: 0

pPanda_beta
pPanda_beta

Reputation: 648

If anyone is still looking for a solution in 2021, and of course have no assistance from their workspace admins to export messages then obviously they can do the following.

Step 1: Get the api token from your UI cookie

  • Clone and install requirements and run SlackPirate
  • Open slack on a browser and copy the value of the cookie named d
  • Run python3 SlackPirate.py --cookie '<value of d cookie>'

Step 2: Dump the channel messages

  • Install slackchannel2pdf (Requires python)
  • slackchannel2pdf --token 'xoxb-1466...' --write-raw-data T0EKHQHK2/G015H62SR3M

Step 3: Dump the direct messages

  • Install slack-history-export (Requires node)
  • slack-history-export -t 'xoxs-1466...' -u '<correct username>' -f 'my_colleagues_chats.json'

Upvotes: 4

Erik Kalkoken
Erik Kalkoken

Reputation: 32698

Here is another tool for exporting all messages from a channel.

The tool is called slackchannel2pdf and will export all messages from a public or private channel to a PDF document.

You only need a token with the required scopes and access.

Upvotes: 0

Erik Kalkoken
Erik Kalkoken

Reputation: 32698

With the new Conversations API this task is bit easier now. Here is a full overview:

Fetching messages from a channel

The new API method conversations.history will allow you to download messages from every type of conversation / channel (public, private, DM, Group DM) as long as your token has access to it.

This method also supports paging allowing you to download large amounts of messages.

Resolving IDs to names

Note that this method will return messages in a raw JSON format with IDs only, so you will need to call additional API method to resolve those IDs into plain text:

Fetching threads

In addition use conversations.replies to download threads in a conversation. Threads function a bit like conversations within a conversation and need to be downloaded separately.

Check out this page of the official documentation for more details on threading.

Upvotes: 9

vk1011
vk1011

Reputation: 7179

This Python script exports everything to JSON by a simple run: https://gist.github.com/Chandler/fb7a070f52883849de35

It creates the directories for you and you have the option to exclude direct messages or channels.

All you need to install is the slacker module, which is simply pip install slacker. Then run it with --token='secret-token'. You need a legacy token, which is available here at the moment.

Upvotes: 14

wjagodfrey
wjagodfrey

Reputation: 195

For anyone looking for Direct Message history downloads, this node based cli tool allows you to download messages from DMs and IMs in both JSON and CSV. I've used it, and it works very well.

Upvotes: 11

seanrose
seanrose

Reputation: 8685

If you need to do this dynamically via API you can use the channels.list method to list all of the channels in your team and channels.history method to retrieve the history of each channel. Note that this will not include DMs or private groups.

If you need to do this as a one time thing, go to https://my.slack.com/services/export to export your team's message archives as series of JSON files

message archive export screen

Upvotes: 28

Related Questions