-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslackbot.py
More file actions
42 lines (30 loc) · 987 Bytes
/
slackbot.py
File metadata and controls
42 lines (30 loc) · 987 Bytes
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
__author__ = 'reverendken'
import logging
import re
import slackapi
from botusers import BOB
SLAP_RX = re.compile(r"slaps ([A-Za-z0-9\.]+) with a ([A-Za-z0-9\.\s]+)$")
SLACK_BOTS = {}
def message_received(msg):
logging.debug("Message from %s: %s" % (msg.user.name, msg.message))
slap_info = SLAP_RX.search(msg.message)
if slap_info:
whom, what = slap_info.groups()
bot_user = SLACK_BOTS.get(whom)
if bot_user:
logging.info("Slapping %s from %s", msg.user.name, bot_user.name)
bot_user.slap(msg.channel, msg.user, what)
def create_bots(api):
SLACK_BOTS['B.O.B.'] = BOB(api, 'B.O.B.', ':computer:', 'bobslap.json')
def main():
ws = slackapi.start_realtime()
create_bots(ws)
ws.add_callback('message', message_received)
ws.connect()
try:
ws.run_forever()
except KeyboardInterrupt:
ws.close()
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main()