-
Notifications
You must be signed in to change notification settings - Fork 1
/
groups_read.py
50 lines (37 loc) · 1.61 KB
/
groups_read.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
RESPONSE_BASE = "Here is the list of affinity groups & the commands you can run to join each of them.\n"
NO_AFFINITY_GROUPS_RESPONSE = (
"No affinity groups found. To populate this list, add Affinity Groups Bot to private "
"channels."
)
def find_private_channels(client):
# Permissions note:
# Bot presence (in a channel) is how we're managing which channels show in the list.
# Thus, this must be some form of bot token (xoxb). Only groups.list scope is required.
return client.api_call(
api_method="conversations.list",
params={"types": "private_channel", "exclude_archived": "true"},
)
def get_groups_list(client):
# Permissions note:
# Bot presence (in a channel) is how we're managing which channels show in the list.
# Thus, this must be some form of bot token (xoxb). Only groups.list scope is required.
response = find_private_channels(client)
if not response["ok"]:
raise AssertionError
return _build_list_response(response)
def _grab_channel_info(channel_blob):
return {
"id": channel_blob["id"],
"name": channel_blob["name"],
"topic": channel_blob["topic"]["value"],
}
def _build_list_response(slack_response):
if not slack_response["channels"]:
return NO_AFFINITY_GROUPS_RESPONSE
channels = map(_grab_channel_info, slack_response["channels"])
response = RESPONSE_BASE
for c in channels:
response += f":slack: *{ c['name'] }* --"
response += "(No topic provided)" if c["topic"] == "" else c["topic"]
response += f" -- `/join-group { c['name'] }`\n"
return response