-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslack.py
83 lines (68 loc) · 2.21 KB
/
slack.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
78
79
80
81
82
83
"""functions for interacting with slack"""
from client_wrapper import ClientWrapper
async def iterate_cursor(fetch, key, url, *, data):
"""
Iterate over a slack response and yield items as they come in
Args:
fetch (function): A function to fetch with, like ClientWrapper().post for example
key (str): The key which contains the list within the request
url (str): The URL to interact with
data (dict): parameters for the request
"""
next_cursor = None
while True:
resp = await fetch(
url,
data={
**({"cursor": next_cursor} if next_cursor is not None else {}),
**data,
},
)
resp.raise_for_status()
resp_json = resp.json()
for item in resp_json[key]:
yield item
next_cursor = resp_json.get("response_metadata", {}).get("next_cursor")
if not next_cursor:
return
async def get_channels_info(slack_access_token):
"""
Get channel information from slack
Args:
slack_access_token (str): Used to authenticate with slack
Returns:
dict: A map of channel names to channel ids
"""
client = ClientWrapper()
# public channels
channels = {}
async for channel in iterate_cursor(
client.post,
"channels",
# see https://api.slack.com/methods/conversations.list
"https://slack.com/api/conversations.list",
data={
"token": slack_access_token,
"types": "public_channel,private_channel",
"limit": 200, # increase from default of 100
"exclude_archived": "true",
},
):
channels[channel["name"]] = channel["id"]
return channels
async def get_doofs_id(slack_access_token):
"""
Ask Slack for Doof's id
Args:
slack_access_token (str): The slack access token
"""
client = ClientWrapper()
async for member in iterate_cursor(
client.post,
"members",
"https://slack.com/api/users.list",
data={"token": slack_access_token},
):
if member["name"] == "doof":
return member["id"]
raise Exception("Unable to find Doof's user id")