forked from aficnar/slackpolice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_data_slack.py
More file actions
68 lines (61 loc) · 2.59 KB
/
upload_data_slack.py
File metadata and controls
68 lines (61 loc) · 2.59 KB
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
"""
This module uploads the previously downloaded Reddit data to Slack.
There's a Slack team called `Slack Police`, with 5 bots there.
The idea is to pick one of the first four bots randomly as we upload the
messages to Slack, so it looks like a real conversation.
The last bot, officer_newman will be the one monitoring discussions and
warning about users going off-topic.
"""
import os
import pandas as pd
import praw
import SlackClient
import time
import db_connect # Connect to SQL
# Initialize the Slack bot clients
bot_tokens = [os.environ["SLACKBOT_TOKEN_JERRY"],
os.environ["SLACKBOT_TOKEN_ELAINE"],
os.environ["SLACKBOT_TOKEN_COSMO"],
os.environ["SLACKBOT_TOKEN_GEORGE"],
os.environ["SLACKBOT_TOKEN_NEWMAN"]]
bot_names = ['jerry', 'elaine', 'cosmo', 'george', 'officer_newman']
slack_uploaders = [SlackClient(bot_tokens[i]) for i in range(4)]
# List of all the channels on our Slack team
channel_list = slack_uploaders[0].api_call("channels.list")['channels']
# Select the subreddits to be uploaded and the corresponding channels:
sql_query = "SELECT name FROM main_subreddits"
all_subreddit_upload_names = pd.read_sql(sql_query, con)['name'].tolist()
all_channel_upload_list = [channel_list[i]['id'] for i in
range(len(all_subreddit_upload_names))]
subreddit_upload_names = all_subreddit_upload_names
channel_upload_list = all_channel_upload_list
# Loop over them and, for now, just upload the first 1000 comments.
# Also also pause for 1s between each ping, due to Slack's API rate limit.
comment_limit = 1000
cnt = 0
for i_sub in range(len(subreddit_upload_names)):
# Get the comments from a given subreddit
sql_query = """
SELECT content
FROM main_comments
WHERE subreddit_id = (
SELECT id
FROM main_subreddits
WHERE name = '%s'
)
""" % subreddit_upload_names[i_sub]
comments = pd.read_sql(sql_query, con)['content'].tolist()[:comment_limit]
# Upload each comment to Slack as a random bot
for c in comments:
bot_index = random.randint(0,3)
slack_uploaders[bot_index].api_call("chat.postMessage",
channel=channel_upload_list[i_sub],
text = c, as_user = True);
time.sleep(1)
cnt = cnt + 1
output_string = ("\rTime remaining: " + convert_secs(comment_limit
* len(subreddit_upload_names) - cnt))
sys.stdout.write(output_string)
sys.stdout.flush()
con.commit()
con.close()