forked from K-2L/hey_fireball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hey_fireball.py
421 lines (354 loc) · 14.1 KB
/
hey_fireball.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Standard imports
import os
import time
from collections import namedtuple
import re
import json
# 3rd party imports
from slackclient import SlackClient
# Same package imports
import storage
# Storage info
_storage = None
STORAGE_TYPE = os.environ.get("STORAGE_TYPE", "inmemory")
# starterbot's ID as an environment variable
BOT_ID = os.environ.get("BOT_ID")
EMOJI = os.environ.get('EMOJI')
POINTS = os.environ.get('POINTS')
SELF_POINTS = os.environ.get('SELF_POINTS', "DISALLOW")
# constants
AT_BOT = "<@" + BOT_ID + ">"
MAX_POINTS_PER_DAY = 5
#EMOJI = ':fireball:'
#POINTS = 'shots'
# instantiate Slack & Twilio clients
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
commands = ['leaderboard', 'fullboard', POINTS, '{}left'.format(POINTS)]
commands_with_target = [POINTS, 'all']
user_list = slack_client.api_call("users.list")['members']
user_name_lookup = {x['id'] : x['name'] for x in user_list} # U1A1A1A1A : kyle.sykes
def get_username(user_id):
try:
return user_name_lookup[user_id]
except KeyError:
return user_id
################
# FireballMessage class
################
class FireballMessage():
_USER_ID_PATTERN = '^<@\w+>$'
_user_id_re = re.compile(_USER_ID_PATTERN)
def __init__(self, msg):
self.requestor_id_only = msg['user']
self.requestor_id = f'<@{self.requestor_id_only}>'
try:
self.requestor_name = user_name_lookup[self.requestor_id_only]
except:
self.requestor_name = self.requestor_id
self.channel = msg['channel']
self.text = msg['text']
self.parts = self.text.split()
self.bot_is_first = self.parts[0] == AT_BOT
self.valid = None
# Check if botname was the only token.
if len(self.parts) > 1:
# Extract target.
if self.bot_is_first:
token = self.parts[1]
else:
token = self.parts[0]
self.target_id = self._extract_valid_user(token)
if self.target_id is not None:
self.target_id_only = self.target_id[2:-1]
# Try to get target username.
try:
self.target_name = user_name_lookup[self.target_id_only]
except:
self.target_name = self.target_id
self.command = self._extract_command()
self.count = self._extract_count()
def __str__(self):
return str(vars(self))
@staticmethod
def _extract_valid_user(user_str):
"""Check if string is a valid user id.
Initially just checking for a valid pattern, but eventually need to check
if ID is in Slack user list.
"""
a = FireballMessage._user_id_re.findall(user_str)
if len(a) > 0:
if a[0][2:-1] in user_name_lookup.keys():
return a[0]
return None
def _extract_command(self):
"""Find the command in the message."""
#TODO: Clean up this gnarly logic. Stop hardcoding indices
if self.bot_is_first:
if self.target_id:
cmds = commands_with_target
idx = 2
else:
cmds = commands
idx = 1
# TODO: Check length of parts or error handler here.
if self.parts[idx].lower() in cmds:
return self.parts[idx].lower()
return None
else:
if self.target_id:
cmds = commands_with_target
idx = 1
else:
cmds = commands
idx = 0
# TODO: Check length of parts or error handler here.
if self.parts[idx].lower() in cmds:
return self.parts[idx].lower()
return None
def _extract_count(self):
#TODO: Clean up this gnarly logic. Stop hardcoding indices
if self.bot_is_first:
if self.target_id:
idx = 2
else:
idx = 1
if self.parts[idx] == EMOJI:
return sum(part==EMOJI for part in self.parts[idx:])
else:
try:
return int(self.parts[idx])
except ValueError:
pass
else:
if self.target_id:
idx = 1
else:
idx = 0
if self.parts[idx] == EMOJI:
return sum(part == EMOJI for part in self.parts[idx:])
else:
try:
return int(self.parts[idx])
except ValueError:
pass
'''
# Use the following to catch and handle missing methods/properties as we want
def __getattr__(self, name):
def wrapper(*args, **kwargs):
print "{} is an invalid statement.".format(name)
return wrapper
'''
#####################
# Storing and retrieving data
#####################
def set_storage(storage_type: str):
"""Set the storage mechanism.
Must be set before calling storge functions.
"""
global _storage
storage_type = storage_type.lower()
if storage_type == 'inmemory':
_storage = storage.InMemoryStorage()
elif storage_type == 'redis':
_storage = storage.RedisStorage()
elif storage_type == 'azuretable':
_storage = storage.AzureTableStorage()
else:
raise ValueError('Unknown storage type.')
def get_user_points_remaining(user_id: str) -> int:
"""Return the number of points remaining for user today."""
used_pts = _storage.get_user_points_used(user_id)
return MAX_POINTS_PER_DAY - used_pts
def add_user_points_used(user_id: str, num: int):
"""Add `num` to user's total used points."""
_storage.add_user_points_used(user_id, num)
def get_user_points_received_total(user_id: str) -> int:
"""Return the number of points received by this user total."""
return _storage.get_user_points_received_total(user_id)
def add_user_points_received(user_id: str, num: int):
"""Add `num` to user's total and today's received points."""
_storage.add_user_points_received(user_id, num)
def get_users_and_scores() -> list:
"""Return list of (user, total points received) tuples."""
return _storage.get_users_and_scores_total()
#####################
# Parsing Message
#####################
def parse_slack_output(slack_rtm_output):
"""
The Slack Real Time Messaging API is an events firehose.
this parsing function 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 ((output and 'text' in output) and
((AT_BOT in output['text']) or
(EMOJI in output['text']))):
# This returns after finding the first message containing
# the bot name. Other messages in this output list will
# be ignored. This is how the example was set up. My
# guess is that this is prevent spamming the bot: this
# way the bot can be invoked only once per READ_WEBSOCKET_DELAY.
return extract_fireball_info(output)
return None
def is_valid_message(fireball_message):
"""Determines if the message contained in the FireballMessage instance is valid."""
if fireball_message.command:
return True
return False
def extract_fireball_info(slack_msg):
"""Extract relevant info from slack msg and return a FireballInfo instance.
If required info is missing or the format is not recognized, set valid=False
and return instance.
"""
fireball = FireballMessage(slack_msg)
# Handle `all` command.
if fireball.command == 'all':
fireball.command = 'give'
fireball.count = get_user_points_remaining(fireball.requestor_id)
# Determine if the `give` command was implied.
if (fireball.command is None
and fireball.target_id
and fireball.count):
fireball.command = 'give'
fireball.valid = is_valid_message(fireball)
return fireball
#####################
# Executing commands
#####################
def handle_command(fireball_message):
"""
Receive a valid FireballMessage instance and
execute the command.
"""
msg = ''
attach = None
if fireball_message.command == 'give':
# Check if self points are allowed.
if SELF_POINTS == 'DISALLOW' and (fireball_message.requestor_id == fireball_message.target_id):
msg = 'You cannot give points to yourself!'
send_message_to = fireball_message.requestor_id_only
# Determine if requestor has enough points to give.
elif check_points(fireball_message.requestor_id, fireball_message.count):
# Add points to target score.
add_user_points_received(fireball_message.target_id, fireball_message.count)
# Add points to requestor points used.
add_user_points_used(fireball_message.requestor_id, fireball_message.count)
msg = f'You received {fireball_message.count} {POINTS} from {fireball_message.requestor_name}'
send_message_to = fireball_message.target_id_only
else:
# Requestor lacks enough points to give.
msg = f'You do not have enough {POINTS}!'
send_message_to = fireball_message.requestor_id_only
elif fireball_message.command == POINTS:
if fireball_message.target_id:
# Return target's score.
score = get_user_points_received_total(fireball_message.target_id)
msg = f'{fireball_message.target_name} has received {score} {POINTS}'
send_message_to = fireball_message.channel
else:
# Return requestor's score.
score = get_user_points_received_total(fireball_message.requestor_id)
msg = f'{fireball_message.requestor_name} has received {score} {POINTS}'
send_message_to = fireball_message.channel
elif fireball_message.command == 'leaderboard':
# Post the leaderboard
msg = "Leaderboard"
attach = generate_leaderboard()
send_message_to = fireball_message.channel
elif fireball_message.command == 'fullboard':
# Post the leaderboard
msg = 'Leaderboard'
#attach = "Full HeyFireball Leaderboard\n" + generate_full_leaderboard()
attach = generate_full_leaderboard()
send_message_to = fireball_message.channel
elif fireball_message.command == f'{POINTS}left':
# Return requestor's points remaining.
points_rmn = get_user_points_remaining(fireball_message.requestor_id)
msg = f"You have {points_rmn} {POINTS} remaining"
send_message_to = fireball_message.requestor_id_only
else:
# Message was not valid, so
msg = f'{fireball_message.requestor_id}: I do not understand your message. Try again!'
send_message_to = fireball_message.channel
# Post message to Slack.
slack_client.api_call("chat.postMessage", channel=send_message_to,
text=msg, as_user=True, attachments=attach)
def give_fireball(user_id, number_of_points):
"""Add `number_of_points` to `user_id`'s total score.
"""
add_user_points_received(user_id, number_of_points)
def remove_points(user_id, number_of_points):
"""
"""
pass
def check_points(user_id, number_of_points):
"""Check to see if user_id has enough points remaining today.
"""
return get_user_points_remaining(user_id) >= number_of_points
'''
fireball color palette
http://www.color-hex.com/color-palette/27418
#f05500 (240,85,0)
#ee2400 (238,36,0)
#f4ac00 (244,172,0)
#ffdb00 (255,219,0)
#ff9a00
'''
colors = ['#d4af37', '#c0c0c0', '#cd7f32', '#36a64f']
def leaderboard_item(user, score, idx):
"""Generate a leaderboard item."""
return {
"fallback": "{}: {}".format(user, score),
"color": colors[min(idx, len(colors) - 1)],
"title": "{}: {}".format(user, score)
}
def generate_leaderboard():
"""Generate a formatted leaderboard."""
# Get sorted list of all users and their scores.
users_and_scores = get_users_and_scores()
if users_and_scores is not None:
leaders = sorted(get_users_and_scores(), key=lambda tup: tup[1], reverse=True)
# Create list of leaderboard items.
board = [leaderboard_item(get_username(tup[0][2:-1]), tup[1], idx) for idx, tup in enumerate(leaders[:10])]
if len(board) > 0:
# Add test to the first element.
#board[0]["pretext"] = "Leaderboard"
pass
else:
board = [{"text": f"No users yet. Start giving {POINTS}!!!"}]
return board
else:
return
def generate_full_leaderboard(full=False):
"""Generate a formatted leaderboard."""
# Get sorted list of all users and their scores.
leaders = sorted(get_users_and_scores(), key=lambda tup: tup[1], reverse=True)
# Create list of leaderboard items.
text = '\n'.join([f'{idx + 1}. {get_username(tup[0][2:-1])} has {tup[1]} {POINTS}' for idx, tup in enumerate(leaders)])
if len(text) == 0:
text = f"No users yet. Start giving {POINTS}!!!"
board = {'text':text, 'color':'#f05500'}
#ee2400
# Add test to the first element.
#board[0]["pretext"] = "HeyFireball Leaderboard"
return [board]
if __name__ == "__main__":
READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
set_storage(STORAGE_TYPE)
if slack_client.rtm_connect():
print("HeyFireball connected and running!")
while True:
# Parse messages and look for EMOJI.
fireball_message = parse_slack_output(slack_client.rtm_read())
# Check if any messages were found containing EMOJI.
if fireball_message:
#print(fireball_message)
# Check if there was a valid message.
if fireball_message.valid:
handle_command(fireball_message)
time.sleep(READ_WEBSOCKET_DELAY)
else:
print("Connection failed. Invalid Slack token or bot ID?")