Skip to content

Instantly share code, notes, and snippets.

@roddds
Created December 27, 2022 20:04
Show Gist options
  • Save roddds/55a1ae985225d6fb83b7136fc9534226 to your computer and use it in GitHub Desktop.
Save roddds/55a1ae985225d6fb83b7136fc9534226 to your computer and use it in GitHub Desktop.
Download a Slack channel's full history, including thread replies
"""
$ export SLACK_TOKEN="xoxb-1234567890-1234567890123-GaT2364GEv72wRpDfSkqn2Wk"
$ python download_slack_history.py CHANNELID
Downloading messages...
...............................................................done!
Downloading threads
...........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................E..................E.......................................................................................................................................................................................................................................................................................................................................EE.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................done!
Channel history written to slack_history_2022-12-27-CHANNELID.json
"""
import os
import sys
import time
import json
import datetime
import slack
CHANNEL_ID = sys.argv[1]
SLACK_TOKEN = os.environ["SLACK_TOKEN"]
def tick(s):
sys.stdout.write(s)
sys.stdout.flush()
def get_conversations_history(client, channel=None, cursor=None):
error_count = 0
while error_count < 5:
try:
return client.conversations_history(channel=channel, cursor=cursor)
except Exception as e:
tick("E")
time.sleep(1)
def get_conversations_replies(client, channel=None, ts=None):
error_count = 0
while error_count < 5:
try:
return client.conversations_replies(channel=channel, ts=ts)
except Exception as e:
tick("E")
time.sleep(1)
if __name__ == "__main__":
client = slack.WebClient(token=SLACK_TOKEN)
all_messages = []
print("Downloading messages...")
result = get_conversations_history(client, channel=CHANNEL_ID)
all_messages += result["messages"]
while result["has_more"]:
result = get_conversations_history(
client,
channel=CHANNEL_ID,
cursor=result["response_metadata"]["next_cursor"],
)
all_messages += result["messages"]
tick(".")
time.sleep(1)
else:
print("done!")
print("Downloading threads...")
for message in all_messages:
if message.get("reply_count"):
message["replies"] = []
replies = get_conversations_replies(
client, channel=CHANNEL_ID, ts=message["ts"]
)
message["replies"] += replies.data["messages"][1:]
tick(".")
time.sleep(1)
print("done!")
filename = f"slack_history_{datetime.date.today().isoformat()}-{CHANNEL_ID}.json"
with open(filename, "w") as f:
json.dump(all_messages, f, indent=4)
print(f"Channel history written to {filename}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment