forked from endyourif/PythonSlackBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
36 lines (29 loc) · 937 Bytes
/
bot.py
File metadata and controls
36 lines (29 loc) · 937 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
import time
import event
from slackclient import SlackClient
class Bot(object):
def __init__(self):
self.slack_client = SlackClient("xoxb-****************")
self.bot_name = "jamiestest"
self.bot_id = self.get_bot_id()
if self.bot_id is None:
exit("Error, could not find " + self.bot_name)
self.event = event.Event(self)
self.listen()
def get_bot_id(self):
api_call = self.slack_client.api_call("users.list")
if api_call.get('ok'):
# retrieve all users so we can find our bot
users = api_call.get('members')
for user in users:
if 'name' in user and user.get('name') == self.bot_name:
return "<@" + user.get('id') + ">"
return None
def listen(self):
if self.slack_client.rtm_connect(with_team_state=False):
print "Successfully connected, listening for commands"
while True:
self.event.wait_for_event()
time.sleep(1)
else:
exit("Error, Connection Failed")