Skip to content

Instantly share code, notes, and snippets.

@ypchen
Forked from benoit-cty/slack_backup.py
Created July 22, 2022 01:34
Show Gist options
  • Save ypchen/d9398de5f6899b1d11dfc390984535ec to your computer and use it in GitHub Desktop.
Save ypchen/d9398de5f6899b1d11dfc390984535ec to your computer and use it in GitHub Desktop.
Script to archive Slack messages from a channel list.
'''
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment