forked from strongdm/accessbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-slack-handle.py
77 lines (63 loc) · 2.35 KB
/
get-slack-handle.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
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
import os
import sys
import argparse
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
# $ python tools/get-slack-handle.py -e [email protected]
# The nick for that user is: @rodolfo
# $ python tools/get-slack-handle.py -d "Rodolfo Campos"
# The nick for that user is: @rodolfo
slack_token = os.environ["SLACK_BOT_TOKEN"]
client = WebClient(token=slack_token)
USERS_PAGE_LIMIT = 500
def get_user_by_email(email):
user = client.users_lookupByEmail(email=email)["user"]
return user
def get_user_by_name(name):
def get_users(**kwargs):
response = client.users_list(**kwargs)
members = response['members']
next_cursor = response['response_metadata']['next_cursor']
return members, next_cursor
def find_user_by_name_fields(data, value):
for item in data:
if item.get("display_name") == value or item.get("real_name") == value:
return item
next_cursor = None
user = None
while not user:
members, next_cursor = get_users(limit = USERS_PAGE_LIMIT, cursor = next_cursor)
user = find_user_by_name_fields(members, name)
if not next_cursor:
break
return user
def print_slack_handle(user):
if user is None:
print("User not found.")
else:
name = user["name"]
print(f"The nick for that user is: @{name}")
def main(arguments):
parser = argparse.ArgumentParser(
epilog="""Example:\n python get-slack-handle.py <[email protected]>
The script will get the slack handle by user's email passed as argument.""",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("-e", "--email", help="A valid email.")
parser.add_argument("-d","--display_name", help="A valid display name.")
args = parser.parse_args(arguments)
if not (args.email or args.display_name):
parser.error('Please, provide an email or display name. Use -h for help.')
try:
user = None
if args.email:
user = get_user_by_email(args.email)
elif args.display_name:
user = get_user_by_name(args.display_name)
print_slack_handle(user)
except SlackApiError as e:
print("Slack API error {}".format(e))
except Exception as e:
print("Error {}".format(e))
if __name__ == "__main__":
main(sys.argv[1:])