forked from sebseager/slack-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
150 lines (119 loc) · 4.49 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import requests
from flask import Flask, request, Response
from urllib.parse import urljoin
from uuid import uuid4
import json
from dotenv import load_dotenv
from exporter import *
app = Flask(__name__)
load_dotenv(os.path.join(app.root_path, ".env"))
# Flask routes
@app.route("/slack/events/export-channel", methods=["POST"])
def export_channel():
data = request.form
try:
team_id = data["team_id"]
team_domain = data["team_domain"]
ch_id = data["channel_id"]
ch_name = data["channel_name"]
response_url = data["response_url"]
command_args = data["text"]
except KeyError:
return Response("Sorry! I got an unexpected response (KeyError)."), 200
post_response(response_url, "Retrieving history for this channel...")
ch_hist = channel_history(ch_id, response_url)
export_mode = str(command_args).lower()
exports_subdir = "exports"
exports_dir = os.path.join(app.root_path, exports_subdir)
file_ext = ".txt" if export_mode == "text" else ".json"
filename = "%s-ch_%s-%s%s" % (team_domain, ch_id, str(uuid4().hex)[:6], file_ext)
filepath = os.path.join(exports_dir, filename)
loc = urljoin(request.url_root, "download/%s" % filename)
if not os.path.isdir(exports_dir):
os.makedirs(exports_dir, exist_ok=True)
with open(filepath, mode="w") as f:
if export_mode == "text":
num_msgs = len(ch_hist)
sep = "=" * 24
header_str = "Channel Name: %s\nChannel ID: %s\n%s Messages\n%s\n\n" % (
ch_name,
ch_id,
num_msgs,
sep,
)
data_ch = header_str + parse_channel_history(
ch_hist, user_list(team_id, response_url)
)
f.write(data_ch)
else:
json.dump(ch_hist, f, indent=4)
post_response(
response_url,
"Done! This channel's history is available for download here (note that this link "
"is single-use): %s" % loc,
)
return Response(), 200
@app.route("/slack/events/export-replies", methods=["POST"])
def export_replies():
data = request.form
try:
team_id = data["team_id"]
team_domain = data["team_domain"]
ch_id = data["channel_id"]
ch_name = data["channel_name"]
response_url = data["response_url"]
command_args = data["text"]
except KeyError:
return Response("Sorry! I got an unexpected response (KeyError)."), 200
post_response(response_url, "Retrieving reply threads for this channel...")
print(ch_id)
ch_hist = channel_history(ch_id, response_url)
print(ch_hist)
ch_replies = channel_replies(
[x["ts"] for x in ch_hist if "reply_count" in x],
ch_id,
response_url=response_url,
)
export_mode = str(command_args).lower()
exports_subdir = "exports"
exports_dir = os.path.join(app.root_path, exports_subdir)
file_ext = ".txt" if export_mode == "text" else ".json"
filename = "%s-re_%s-%s%s" % (team_domain, ch_id, str(uuid4().hex)[:6], file_ext)
filepath = os.path.join(exports_dir, filename)
loc = urljoin(request.url_root, "download/%s" % filename)
if export_mode == "text":
header_str = "Threads in: %s\n%s Messages" % (ch_name, len(ch_replies))
data_replies = parse_replies(ch_replies, user_list(team_id, response_url))
sep = "=" * 24
data_replies = "%s\n%s\n\n%s" % (header_str, sep, data_replies)
else:
data_replies = ch_replies
if not os.path.isdir(exports_dir):
os.makedirs(exports_dir, exist_ok=True)
with open(filepath, mode="w") as f:
if export_mode == "text":
f.write(data_replies)
else:
json.dump(data_replies, f, indent=4)
post_response(
response_url,
"Done! This channel's reply threads are available for download here (note that this "
"link is single-use): %s" % loc,
)
return Response(), 200
@app.route("/download/<filename>")
def download(filename):
path = os.path.join(app.root_path, "exports", filename)
def generate():
with open(path) as f:
yield from f
os.remove(path)
mimetype = (
"text/plain" if os.path.splitext(filename)[-1] == ".txt" else "application/json"
)
r = app.response_class(generate(), mimetype=mimetype)
r.headers.set("Content-Disposition", "attachment", filename=filename)
return r
if __name__ == "__main__":
app.run(debug=True)