Last active
August 25, 2023 16:32
-
-
Save benoit-cty/a5855dea9a4b7af03f1f53c07ee48d3c to your computer and use it in GitHub Desktop.
Script to archive Slack messages from a channel list.
This file contains hidden or 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
''' | |
Script to archive Slack messages from a channel list. | |
You have to create a Slack Bot and invite him to private channels. | |
View https://github.com/docmarionum1/slack-archive-bot for how to configure your account. | |
Then provide the bot token to this script with the list of channels. | |
''' | |
TOKEN='xoxb-xxxxx-xxxxxx-xxxxxxxxxxx' | |
channels = { | |
'général': 'CSNLXXXXXX', | |
'veille': 'G01XXXXXX' | |
} | |
# Import WebClient from Python SDK (github.com/slackapi/python-slack-sdk) | |
from slack_sdk import WebClient | |
from slack_sdk.errors import SlackApiError | |
import json | |
# WebClient insantiates a client that can call API methods | |
# When using Bolt, you can use either `app.client` or the `client` passed to listeners. | |
client = WebClient(token=TOKEN) | |
# Store conversation history | |
conversation_history = [] | |
def backup_channel(channel_name, channel_id): | |
''' | |
:channel_id: ID of the channel you want to send the message to | |
''' | |
try: | |
print('Getting messages from', channel_name) | |
# Call the conversations.history method using the WebClient | |
# conversations.history returns the first 100 messages by default | |
# These results are paginated | |
result = client.conversations_history(channel=channel_id) | |
all_message = [] | |
all_message += result["messages"] | |
while result['has_more']: | |
print("\tGetting more...") | |
result = client.conversations_history(channel=channel_id, cursor=result['response_metadata']['next_cursor']) | |
all_message += result["messages"] | |
# Save to disk | |
filename = f'{channel_name}.json' | |
print(f' We have downloaded {len(all_message)} messages from {channel_name}.') | |
print(' Saving to', filename) | |
with open(filename, 'w') as outfile: | |
json.dump(all_message, outfile) | |
except SlackApiError as e: | |
print("Error using conversation: {}".format(e)) | |
if __name__ == "__main__": | |
# Iterate channels | |
for chan_name, chan_id in channels.items(): | |
backup_channel(chan_name, chan_id) |
@edemaine I don't know how to set a license for a gist but I give permission to anyone to re-use and modify the code.
Thank you! I've added an MIT license to my fork.
(If you want to add a license to yours, you could probably just mention in a comment in the Python file.)
@benoit-cty , I might have a very simple question, but before testing your code, I would like to know if I have to invite the bot to every channel manually? I can, in the tester (on the API site) with the generated xbot token, get the list of channels, but when I want to read the channel history I get an error: "not in channel", and after inviting the bot this error is gone.
Yes, you have to invite him to private channels.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this helpful starting point! Would you mind releasing this code under an open-source license such as MIT, or otherwise giving me permission to use it in https://github.com/edemaine/slack-backup (where I've added automatic channel listing, user listing, and making the output format compatible with https://github.com/pR0Ps/slack-to-discord)?