diff --git a/bot.py b/bot.py index b9e6d5d..fb2e49c 100644 --- a/bot.py +++ b/bot.py @@ -5,111 +5,12 @@ from urllib.parse import urljoin from uuid import uuid4 import json from dotenv import load_dotenv -from exporter import parse_replies, parse_channel_history +from exporter import * app = Flask(__name__) load_dotenv(os.path.join(app.root_path, ".env")) -# chat write interactions - - -def post_response(response_url, text): - requests.post(response_url, json={"text": text}) - - -# pagination handling - - -def get_at_cursor(url, params, response_url, cursor=None): - if cursor is not None: - params["cursor"] = cursor - - r = requests.get(url, params=params) - data = r.json() - - try: - if data["ok"] is False: - post_response(response_url, "I encountered an error: %s" % data["error"]) - - next_cursor = None - if "response_metadata" in data and "next_cursor" in data["response_metadata"]: - next_cursor = data["response_metadata"]["next_cursor"] - if str(next_cursor).strip() == "": - next_cursor = None - - return next_cursor, data - - except KeyError as e: - post_response(response_url, "Something went wrong: %s." % e) - return None, [] - - -def paginated_get(url, params, response_url, combine_key=None): - next_cursor = None - result = [] - while True: - next_cursor, data = get_at_cursor(url, params, response_url, cursor=next_cursor) - try: - result.extend(data) if combine_key is None else result.extend( - data[combine_key] - ) - except KeyError: - post_response( - response_url, "Sorry! I got an unexpected response (KeyError)." - ) - if next_cursor is None: - break - - return result - - -# GET requests - - -def channel_history(channel_id, response_url): - params = { - "token": os.environ["SLACK_USER_TOKEN"], - "channel": channel_id, - "limit": 200, - } - - return paginated_get( - "https://slack.com/api/conversations.history", - params, - response_url, - combine_key="messages", - ) - - -def user_list(team_id, response_url): - params = {"token": os.environ["SLACK_USER_TOKEN"], "limit": 200, "team_id": team_id} - - return paginated_get( - "https://slack.com/api/users.list", params, response_url, combine_key="members" - ) - - -def channel_replies(timestamps, channel_id, response_url): - replies = [] - for timestamp in timestamps: - params = { - "token": os.environ["SLACK_USER_TOKEN"], - "channel": channel_id, - "ts": timestamp, - "limit": 200, - } - r = paginated_get( - "https://slack.com/api/conversations.replies", - params, - response_url, - combine_key="messages", - ) - replies.append(r) - - return replies - - # Flask routes