forked from capitalone/slackbot-destroyer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
287 lines (235 loc) · 10.3 KB
/
app.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import requests
import random
from slackclient import SlackClient
from constants import *
from global_store import GlobalStore
slack_client = SlackClient(SLACK_BOT_TOKEN)
def handle_command(command, channel):
"""
Handles any generic command that is pointed
towards the bot.
"""
if channel not in config.destroy.keys():
# If the bot is being used in a new channel we store a new dictionary entry for it.
config.update_channel(channel, State.DEACTIVATED)
if command.startswith(HELP_COMMAND):
# Displays a list of available commands.
return send_attachment_message(
{
'fallback': 'Available Commands',
'color': '#150650',
'title': 'A v a i l a b l e C o m m a n d s',
'title_link': 'http://unitedincome.com',
'text': '',
"fields": [
{
"title": "`destroy`",
"value": "`Deletes all incoming messages from Slackbot.`",
"short": True
},
{
"title": "`deactivate`",
"value": "`Stops Slackbot deletion.`",
"short": True
},
{
"title": "`stats`",
"value": "`Shows how many messages from Slackbot that have been deleted.`",
"short": True
},
{
"title": "`commands`",
"value": "`Displays a list of available commands.`",
"short": True
},
{
"title": "`teleport`",
"value": "`Teleport to the future.`",
"short": True
},
{
"title": "`moderate`",
"value": "`Moderate slackbot responses in this channel.`",
"short": True
},
{
"title": "`wassup`",
"value": "`Show state of bot in this channel.`",
"short": True
},
{
"title": "`track`",
"value": "`Show chance of being seen for each response the bot has seen.`",
"short": True
},
{
"title": "`hunt index chance`",
"value": "`Change the chance of being seen for response at the given index (lower = less frequent).`",
"short": True
}
],
}
, channel)
if command.startswith(TOGGLE_DESTROY_COMMAND):
if config.state_in_channel(channel) == State.DESTROYING:
return send_basic_message('`A L R E A D Y D E S T R O Y I N G` :robot_face:', channel)
config.update_channel(channel, State.DESTROYING)
return send_basic_message('`D E S T R O Y` :robot_face:', channel)
if command.startswith(TOGGLE_DEACTIVATE_COMMAND):
if not config.state_in_channel(channel) == State.DEACTIVATED:
return send_basic_message('`S T A N D I N G B Y` :robot_face:', channel)
config.update_channel(channel, State.DEACTIVATED)
return send_basic_message('`D E A C T I V A T E` :robot_face:', channel)
if command.startswith(TELEPORT_COMMAND):
# Returns a random robot inspired videos.
return send_basic_message(random.choice(TELEPORT_VIDEOS), channel)
if command.startswith(STATS_COMMAND):
return send_basic_message('`I H A V E D E S T R O Y E D %s M E S S A G E F R O M S L A C K B O T` :robot_face:' % config.deletions, channel)
if command.startswith(MODERATE_COMMAND):
if config.state_in_channel(channel) == State.MODERATING:
return send_basic_message('`A L R E A D Y M O D E R A T I N G` :robot_face:', channel)
config.update_channel(channel, State.MODERATING)
return send_basic_message('`M O D E R A T E` :robot_face:', channel)
if command.startswith(SHOW_STATE):
return send_basic_message('`C U R R E N T S T A T U S: %r` :robot_face:' % config.state_in_channel(channel).name, channel)
if command.startswith(SHOW_RESPONSES):
return send_basic_message('L O C A T E D:\n{}'.format(config.format_responses()), channel)
if command.startswith(CONFIGURE_RESPONSE):
options = command.split(' ')
if len(options) != 3 or float(options[2]) > 1:
return send_basic_message('I N V A L I D C O M M A N D', channel)
index = int(options[1])
new_chance = round(float(options[2]), 1)
config.set_response_chance(index, new_chance)
return send_basic_message('A D J U S T E D R E S P O N S E F R E Q U E N C Y', channel)
if command.startswith(CYBER_COMMAND):
return send_basic_message('`T H E Y D I E B E T T E R` :robot_face:', channel)
def send_basic_message(message, channel):
""" Sends a basic message with the Slack API """
slack_client.api_call("chat.postMessage", channel=channel,
text=message, as_user=True)
def send_attachment_message(attachment, channel):
"""
Sends an attachment message to Slack. The payload argument
should be a JSON object containing the following keys.
{
'fallback': 'Fallback text',
'color': '#fff',
'title': 'Title text',
'title_link': 'http://unitedincome.com',
'text': 'Text',
'fields': []
}
"""
ts = int(time.time())
payload = [
{
'fallback': attachment['fallback'],
'color': attachment['color'],
'title': attachment['title'],
'title_link': "%s" % (attachment['title_link']),
'text': attachment['text'],
'ts': ts,
'fields': attachment['fields'],
'footer': 'https://github.com/UnitedIncome/slackbot-destroyer',
'image_url': 'https://i.imgur.com/22XLQna.jpg',
'footer_icon': 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/google/146/robot-face_1f916.png',
}
]
slack_client.api_call("chat.postMessage", channel=channel,
attachments=payload, as_user=True)
def delete_message(timestamp, channel):
""" Deletes a message using the Slack Web API """
options = {
'token': SLACK_USER_TOKEN,
'channel': channel,
'ts': timestamp,
'as_user': True
}
try:
requests.post('https://slack.com/api/chat.delete', params=options)
except requests.exceptions.RequestException as error:
print(error)
config.increment()
def moderate_message(timestamp, channel, text):
""" Delete a message if random chance doesn't work out for it """
chance = config.get_response_chance(text)
dice_roll = random.random()
if dice_roll > chance:
delete_message(timestamp, channel)
def get_channel_list():
""" Gets a list of channels using the Slack Web API """
options = {
'token': SLACK_USER_TOKEN,
}
try:
request = requests.get('https://slack.com/api/channels.list', params=options)
request.raise_for_status()
request_json = request.json()
except requests.exceptions.RequestException as error:
request_json = {}
print(error)
return request_json
def remove_from_channel(id, channel):
""" Removes a user from a channel, primarily used to remove the bot from
non whitelisted channels """
options = {
'token': SLACK_USER_TOKEN,
'user': id,
'channel': channel
}
try:
requests.post('https://slack.com/api/channels.kick', params=options)
except requests.exceptions.RequestException as error:
print(error)
def parse_slack_output(slack_rtm_output):
"""
Returns None unless a message is directed at the bot
based on its ID.
"""
output_list = slack_rtm_output
if output_list and len(output_list) > 0:
for output in output_list:
# If the channel name is not found in the whitelist, the bot removes its self.
# This functionality requires the channels:read and channels:write permissions.
if 'type' in output.keys() and output['type'] == 'channel_joined' and CHANNEL_WHITELIST is not None:
channel_white_list = CHANNEL_WHITELIST.strip().split(',')
channel_list = get_channel_list()
if 'channels' in channel_list.keys():
for channel in channel_list['channels']:
if channel['name'] not in channel_white_list:
remove_from_channel(BOT_ID, channel['id'])
# Handles message types
if 'type' in output.keys() and output['type'] == 'message':
if 'subtype' in output.keys() and output['subtype'] == 'slackbot_response':
if 'channel' in output.keys() and 'ts' in output.keys():
if config.state_in_channel(output['channel']) == State.DESTROYING:
delete_message(output['ts'], output['channel'])
elif config.state_in_channel(output['channel']) == State.MODERATING:
moderate_message(output['ts'], output['channel'], output.get('text', ''))
if 'text' in output.keys():
config.add_response(output['text'])
if output and 'text' in output and AT_BOT in output['text']:
# Return text after the @ mention, whitespace removed
return output['text'].split(AT_BOT)[1].strip(), \
output['channel']
return None, None
if __name__ == "__main__":
global config
READ_WEBSOCKET_DELAY = 1
if slack_client.rtm_connect():
print('Connected to RTM')
config = GlobalStore()
config.load()
print(config)
print('Launch successful, waiting for input...')
while True:
command, channel = parse_slack_output(slack_client.rtm_read())
if command and channel:
handle_command(command, channel)
time.sleep(READ_WEBSOCKET_DELAY)
else:
print("Connection failed. This is likely due to an invalid Slack token or Bot ID.")