-
Notifications
You must be signed in to change notification settings - Fork 0
/
karmabot.py
107 lines (84 loc) · 3.14 KB
/
karmabot.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
import json
import re
import subprocess
import sys
import time
import redis
from slackclient import SlackClient
sc = SlackClient(sys.argv[1])
r = redis.StrictRedis(host='localhost', port=6379, db=0)
def poll_client():
"""
Connects to Slack and listens for new messages on channels that karmabot has joined.
If an incoming message is of type message and is from a channel, further processing will be
performed.
"""
if sc.rtm_connect():
while True:
messages = sc.rtm_read()
try:
for msg in messages:
if msg['type'] == 'message':
if (msg['channel'][0] == 'C'
or msg['channel'][0] == 'G'):
process_message(msg)
except:
sys.exc_info()[0]
time.sleep(1)
else:
print 'Connection Failed, invalid token?'
def process_message(json_msg):
"""
Parses messages posted to a channel and determines whether or not action needs to be taken.
If the message is a karma message, other messages will be called to update the karma and send
a message to the channel.
---------------------------------------------------------------------------
THIS METHOD IS UNDER CONSTRUCTION.
The logic has been put in place, but needs to be adjusted for readability.
---------------------------------------------------------------------------
"""
regex = '^<?@.+>?:* *[\+-]{2,6}'
splitter_regex = '<*:* *>*'
text = json_msg['text']
result = re.search(regex, text)
if result is not None:
parsed_text = result.group(0)
user_and_amount = filter(None, re.split(splitter_regex, parsed_text))
user_list = get_users()
if user_list['members'] is not None:
for user in user_list['members']:
if ('@' + user['id'] == user_and_amount[0]
or '@' + user['name'] == user_and_amount[0]):
amount = len(user_and_amount[1]) - 1
if user_and_amount[1][0] == '-':
amount = -amount
new_amount = adjust_karma(user['id'], amount)
send_message(user['name'], new_amount, json_msg['channel'])
break
def adjust_karma(user_id, amount):
"""
Increments or decrements a user's karma.
To prevent spam, the maximum adjustment allowed in a single message is 5 points.
"""
curr_amount = int(r.get(user_id) or 0)
if amount >= 5:
new_amount = curr_amount + 5
elif amount <= -5:
new_amount = curr_amount - 5
else:
new_amount = curr_amount + amount
r.set(user_id, new_amount)
return new_amount
def send_message(user_name, amount, channel):
"""
Reports the amount of karma for a given user to a given channel.
"""
msg = user_name + '\'s karma is now ' + str(amount)
sc.rtm_send_message(channel, msg)
def get_users():
"""
Fetches a list of user data for all of the users in the Slack team.
"""
return json.loads(sc.api_call('users.list'))
if __name__ == '__main__':
poll_client()