-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmentorbot.py
More file actions
103 lines (88 loc) · 2.89 KB
/
mentorbot.py
File metadata and controls
103 lines (88 loc) · 2.89 KB
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
import os
import time
import config
import request
import mentor
from slackclient import SlackClient
'''
Wrapper for a SlackBot, using slackclient
'''
class SlackBot(object):
def __init__(self, token, mentor_list):
self.__connect(token)
self.mentors = mentor_list
self.mentor_names = []
for mentor in self.mentors:
self.mentor_names.append(mentor.name)
def __connect(self, token):
self.client = SlackClient(token)
def getUsers(self):
self.users = {}
res = self.client.api_call("users.list")
for user in res["members"]:
self.users[user["id"]] = user["real_name"]
return True
def getID(self, name):
'''
Get the ID of a user given their name.
'''
api_call = self.client.api_call("users.list")
if api_call.get('ok'):
users = api_call.get('members')
for user in users:
if 'name' in user and user.get('name') == name:
return user.get('id')
def privateMessage(self, message, channel):
'''
Send a private message to a user in the given PM channel.
'''
self.client.api_call("chat.postMessage", channel=channel, text=message, as_user=False)
def parseHackerMessage(self, msg_dict):
'''
Creates a Request object from the message dictionary
'''
try:
if (msg_dict[0]["channel"][0] == "D"):
req = request.Request(msg_dict[0])
return req
except:
pass
return None
def fromWho(self, req):
'''
return who the request is from
for mentor_name in
'''
pass
def assignRequest(self, req):
for m in self.mentors:
if m.is_valid(req):
m.assignTask(req)
print 'mentor assigned'
return True
return False
def run(self):
'''
Open socket reading for input.
'''
READ_WEBSOCKET_DELAY = 1
if self.client.rtm_connect():
print("Mentorbot connected and running!")
self.getUsers()
while True:
inc = self.client.rtm_read()
print inc
req = self.parseHackerMessage(inc)
if req is not None:
self.assignRequest(req)
time.sleep(READ_WEBSOCKET_DELAY)
else:
print("Connection failed. Invalid Slack token or bot ID?")
if __name__ == "__main__":
mentor1 = mentor.Mentor({'name': 'mntr1', 'topic': 'stuff', 'language': 'C++'})
mentor2 = mentor.Mentor({'name': 'mntr2', 'topic': 'stuff2', 'language': 'Java'})
mentor_list = [mentor1, mentor2]
bot = SlackBot(config.SLACK_TOKEN, mentor_list)
bot.run()
#[{u'type': u'user_typing', u'user': u'U34LJ4C11', u'channel': u'D341VTH4Y'}]
# format of inputs