Heecheol's picture
Update app.py
52f562a
Raw History Blame Contribute Delete
1.65 kB
import json
import re
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
import gradio as gr
def remove_reactions(text):
reaction_pattern = re.compile(r':\w+:')
return reaction_pattern.sub('', text)
def remove_urls(text):
url_pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
return url_pattern.sub('', text)
def fetch_text_from_slack_thread(bot_token, thread_link):
try:
client = WebClient(token=bot_token)
channel_id, message_ts = re.search("\/archives\/(.+)\/p(\d+)", thread_link).groups()
thread_ts = f"{message_ts[:-6]}.{message_ts[-6:]}"
response = client.conversations_replies(channel=channel_id, ts=thread_ts)
messages = response["messages"]
json_messages = []
for message in messages:
json_message = {}
processed_text = remove_urls(remove_reactions(message["text"]))
if processed_text.strip(): # check if text is not empty after processing
json_message['text'] = processed_text
json_message['user'] = message.get('user')
json_message['ts'] = message.get('ts')
json_messages.append(json_message)
return json_messages
except SlackApiError as e:
print(f"Slack API Error: {e}")
return []
iface = gr.Interface(
fn=fetch_text_from_slack_thread,
inputs=["text", "text"],
outputs="json",
title="Slack Thread Fetcher",
description="Enter the bot token and thread link from Slack to fetch and return the content as JSON."
)
iface.launch()