-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
45 lines (36 loc) · 1.24 KB
/
bot.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
import time
import event
from slackclient import SlackClient
'''
@author ssaxena36
'''
class Bot(object):
def __init__(self):
'''
Ensuring Safety of API Token
'''
file = open("/home/ssaxena36/Desktop/Hack36Apps/key.txt", "r")
self.slack_client = SlackClient(file.read().strip())
self.bot_name = "py3bot"
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")