diff --git a/slackclient/server.py b/slackclient/server.py index f6b105665..039ab74f9 100644 --- a/slackclient/server.py +++ b/slackclient/server.py @@ -15,6 +15,7 @@ class Server(object): ''' + def __init__(self, token, connect=True, proxies=None): self.token = token self.username = None @@ -92,7 +93,7 @@ def parse_slack_login_data(self, login_data, use_rtm_start): if use_rtm_start: self.parse_channel_data(login_data["channels"]) self.parse_channel_data(login_data["groups"]) - self.parse_user_data(login_data["users"]) + self.parse_user_data(login_data["users"], login_data["team"]) self.parse_channel_data(login_data["ims"]) def connect_slack_websocket(self, ws_url): @@ -124,13 +125,19 @@ def parse_channel_data(self, channel_data): channel["id"], channel["members"]) - def parse_user_data(self, user_data): + def parse_user_data(self, user_data, team_data): for user in user_data: if "tz" not in user: user["tz"] = "unknown" if "real_name" not in user: user["real_name"] = user["name"] - self.attach_user(user["name"], user["id"], user["real_name"], user["tz"]) + if "profile" in user and "email" in user["profile"]: + user["email"] = user["profile"]["email"] + elif team_data and "email_domain" in team_data: + user["email"] = "{}@{}".format(user["name"], team_data["email_domain"]) + else: + user["email"] = "" # default to empty. + self.attach_user(user["name"], user["id"], user["real_name"], user["email"], user["tz"]) def send_to_websocket(self, data): """ @@ -196,8 +203,8 @@ def websocket_safe_read(self): raise return data.rstrip() - def attach_user(self, name, user_id, real_name, tz): - self.users.update({user_id: User(self, name, user_id, real_name, tz)}) + def attach_user(self, name, user_id, real_name, email, tz): + self.users.update({user_id: User(self, name, user_id, real_name, email, tz)}) def attach_channel(self, name, channel_id, members=None): if members is None: diff --git a/slackclient/user.py b/slackclient/user.py index 274bddbe2..fee196876 100644 --- a/slackclient/user.py +++ b/slackclient/user.py @@ -1,8 +1,9 @@ class User(object): - def __init__(self, server, name, user_id, real_name, tz): + def __init__(self, server, name, user_id, real_name, email, tz): self.tz = tz self.name = name self.real_name = real_name + self.email = email self.server = server self.id = user_id diff --git a/tests/test_server.py b/tests/test_server.py index 8ce59c7c7..d89260aac 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -32,12 +32,14 @@ def test_server_parse_channel_data(server, rtm_start_fixture): def test_server_parse_user_data(server, rtm_start_fixture): - server.parse_user_data(rtm_start_fixture["users"]) + server.parse_user_data(rtm_start_fixture["users"], rtm_start_fixture["team"]) # Find user by Name userbyname = server.users.find('fakeuser') assert type(userbyname) == User assert userbyname == "fakeuser" assert userbyname != "someotheruser" + assert userbyname.email == "fakeuser@example.com" + # Find user by ID userbyid = server.users.find('U10CX1234') assert type(userbyid) == User