-
-
Save benoit-cty/a5855dea9a4b7af03f1f53c07ee48d3c to your computer and use it in GitHub Desktop.
''' | |
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) |
You need to be able to create a "bot" to have a token. So I thing a normal user could not do it.
I suppose it not possible to use this for private messages since you cannot add apps to private messages under free version of slack?
You're probably right.
I did a little tweak in order to get the messages from a date onwards using the oldest parameter and the timestamp of the last message retrieved in the call. I needed to get all messages for the last 3 months essentially.
result = client.conversations_history(channel=channel_id,oldest='1621850176')
all_message = []
all_message += result["messages"]
last_elem = result["messages"][-1]
ts = last_elem["ts"]
last_message_date = datetime.fromtimestamp(int(float(ts)))
print(f'last message timestamp: {last_message_date}')
while result['has_more']:
print("\tGetting next 100 messages...")
result = client.conversations_history(channel=channel_id, cursor=result['response_metadata']['next_cursor'],oldest=ts)
all_message += result["messages"]
last_elem = result["messages"][-1]
ts = last_elem["ts"]
last_message_date = datetime.fromtimestamp(int(float(ts)))
print(f'Last message timestamp: {last_message_date}')
thanks for sharing
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)?
@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.
Do you need slack admin rights for this?