Created
March 11, 2019 11:07
-
-
Save westonplatter/30254c747882a22ecd0f6e01910288e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from slacker import Slacker | |
# | |
# https://api.slack.com/custom-integrations/legacy-tokens | |
# | |
token = "--- put api token here ---" | |
slack = Slacker(token) | |
# | |
# Get users list | |
# | |
response = slack.users.list() | |
users = response.body['members'] | |
# | |
# filter and extract data | |
# | |
rows = [] | |
for user in users: | |
if "email" in user["profile"]: | |
# put email in dictionary | |
d = {"email": user["profile"]["email"]} | |
# put other fields in dictionary if present | |
fields = ["real_name", "first_name", "last_name", "title", "phone"] | |
for f in fields: | |
if f in user["profile"]: | |
d[f] = user["profile"][f] | |
rows.append(d) | |
# | |
# export to csv | |
# | |
import csv | |
with open('slack_users.csv', 'w', newline='') as csvfile: | |
fieldnames = ["email", "real_name", "first_name", "last_name", "title", "phone"] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
[writer.writerow(r) for r in rows] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment