|
import datetime |
|
import os |
|
from slack_sdk import WebClient |
|
|
|
|
|
class SlackChannelReader: |
|
def __init__(self, token, channel_id): |
|
self.client = WebClient(token=token) |
|
self.channel_id = channel_id |
|
|
|
def read_messages(self, start_date): |
|
start_timestamp = int(start_date.timestamp()) |
|
cursor = None |
|
has_more = True |
|
|
|
while has_more: |
|
response = self.client.conversations_history( |
|
channel=self.channel_id, |
|
limit=100, |
|
oldest=start_timestamp, |
|
cursor=cursor |
|
) |
|
|
|
messages = response["messages"] |
|
threads = self._group_messages_by_thread(messages) |
|
|
|
self._write_threads_to_files(threads) |
|
|
|
has_more = response["has_more"] |
|
if has_more: |
|
cursor = response["response_metadata"]["next_cursor"] |
|
|
|
@staticmethod |
|
def _group_messages_by_thread(messages): |
|
threads = {} |
|
|
|
for message in messages: |
|
if "thread_ts" in message: |
|
thread_ts = message["thread_ts"] |
|
user = message["user"] |
|
timestamp = message["ts"] |
|
content = message["text"] |
|
|
|
if thread_ts in threads: |
|
threads[thread_ts].append((user, timestamp, content)) |
|
else: |
|
threads[thread_ts] = [(user, timestamp, content)] |
|
|
|
return threads |
|
|
|
@staticmethod |
|
def _write_threads_to_files(threads): |
|
for thread_ts, thread_messages in threads.items(): |
|
file_name = f"slack_data/{thread_ts}_{thread_messages[0][0]}.txt" |
|
with open(file_name, "w") as file: |
|
for user, timestamp, content in thread_messages: |
|
file.write(f"User: {user}\n") |
|
file.write(f"Timestamp: {timestamp}\n") |
|
file.write(f"Content: {content}\n\n") |
|
|
|
|
|
def main(): |
|
token = os.environ.get('SLACK_API_TOKEN') |
|
channel_id = "C02JEH5KGGN" |
|
start_date = datetime.datetime(2023, 1, 1) |
|
|
|
reader = SlackChannelReader(token, channel_id) |
|
reader.read_messages(start_date) |
|
|
|
|
|
main() |
|
|