-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalentbot.py
64 lines (52 loc) · 2.01 KB
/
talentbot.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
import time
import logging
from slackevent import SlackEvent
class TalentBot:
def __init__(self, slackClient):
self.slack = slackClient
self.commands = []
def addCommand(self, command):
if len(command) > 1:
for cmd in command:
self.commands.append(cmd)
else:
self.commands.append(command)
def run(self):
if not self.slack.rtm_connect():
logging.critical("Error: Failed to connect to Slack servers")
exit(-1)
else:
logging.info("Talentbot is up and running")
while True:
try:
events = self.slack.rtm_read()
self.processEvents(events)
except Exception, e:
logging.exception(e)
# In general Slack allows one message per second,
# although it may allow short bursts over the limit
# for a limited time. https://api.slack.com/docs/rate-limits
time.sleep(1)
def processEvents(self, events):
if events is None:
return
for event in events:
self.processEvent(SlackEvent(event))
def processEvent(self, event):
if not event.isMessage():
logging.debug("Non-message event\n- %s" % event)
return
if event.isTalentBot():
logging.debug("Event regarding myself\n- %s" % event)
return
if event.isMessage():
logging.debug("Incoming message\n- %s" % event)
if event.textContainsKeyword('help'):
helpMsg = ":paperclip: Here are some things you can say to me:\n>>>"
helpMsg += '\n'.join(cmd.help() for cmd in self.commands)
self.slack.rtm_send_message(event.channel(), helpMsg)
return
for command in self.commands:
if command.shouldTriggerOn(event):
command.executeOn(event)
break