Skip to content

Commit 83b111d

Browse files
committed
Initial commit
0 parents  commit 83b111d

4 files changed

Lines changed: 58 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# slack-scripts
2+
3+
Scripts to fetch data about our Slack workspace via API.

get-slack-channel-emails.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import csv
2+
import os
3+
import sys
4+
5+
import slack
6+
7+
from dotenv import load_dotenv
8+
9+
# To get email addresses you need a fully fledged oauth app with the users:read.email scope.
10+
# Just using a `slack auth token` token won't fetch email addresses.
11+
# Setting these up is annoying. Create an app in the workspace, set up the following scopes, and install it to the workspace. Then fetch the User OAuth Token from the app's settings.
12+
# Required scopes:
13+
# * channels:read
14+
# * users:read
15+
# * users:read.email
16+
def print_channel_emails(client: slack.WebClient, channel_id: str):
17+
channel_name = client.conversations_info(channel=channel_id)["channel"]["name"]
18+
for user in client.conversations_members(channel=channel_id)["members"]:
19+
user_info = client.users_info(user=user).data["user"]
20+
21+
display_name = user_info.get("name", "UNKNOWN")
22+
real_name = user_info.get("real_name", "UNKNOWN")
23+
email = user_info["profile"].get("email", "UNKNOWN")
24+
25+
writer = csv.writer(sys.stdout)
26+
writer.writerow([channel_name, real_name, display_name, email])
27+
28+
29+
def main(channel_ids):
30+
# Fetch from the User OAuth Token box in https://api.slack.com/apps/A080QLL46BC/oauth
31+
client = slack.WebClient(token=os.environ["SLACK_API_BOT_TOKEN"])
32+
33+
for channel_id in channel_ids:
34+
print_channel_emails(client, channel_id)
35+
36+
if __name__ == "__main__":
37+
if len(sys.argv) == 1:
38+
print(f"Usage: {sys.argv[0]} channel-id...", file=sys.stderr)
39+
sys.exit(1)
40+
41+
load_dotenv()
42+
43+
main(sys.argv[1:])

requirements.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
aiohappyeyeballs==2.4.3
2+
aiohttp==3.10.10
3+
aiosignal==1.3.1
4+
attrs==24.2.0
5+
frozenlist==1.5.0
6+
idna==3.10
7+
multidict==6.1.0
8+
propcache==0.2.0
9+
python-dotenv==1.0.1
10+
slackclient==2.9.4
11+
yarl==1.17.1

0 commit comments

Comments
 (0)