-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_channels_list.py
82 lines (67 loc) · 2.11 KB
/
slack_channels_list.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
#!/usr/bin/env python
"""
Slackのチャンネル一覧を生成する
"""
from __future__ import annotations
from slacker import Slacker
import settings
from google_api import get_sheets_service
# チャンネル一覧を書き込むスプレッドシートのID
SHEET_ID = "1Z93vxxC6zdSunO52-9hQpeGpBqQeFPIPLn2ayPRmtJY"
def get_channels_list() -> list[dict[str, str | int]]:
"""
Slackのチャンネル一覧から必要な情報を返す
https://api.slack.com/methods/channels.list
"""
slack = Slacker(settings.SLACK_TOKEN)
r = slack.channels.list(exclude_archived=True)
channels = []
for channel in r.body["channels"]:
channel_info = {
"id": channel["id"],
"name": "#" + channel["name"],
"topic": channel["topic"]["value"],
"purpose": channel["purpose"]["value"],
"members": channel["num_members"],
}
channels.append(channel_info)
return channels
def store(channels: list[dict[str, str | int]]):
"""
Slackのチャンネル情報をGoogleスプレッドシートに書き込む
https://docs.google.com/spreadsheets/d/1Z93vxxC6zdSunO52-9hQpeGpBqQeFPIPLn2ayPRmtJY/edit#gid=0
"""
values = []
# チャンネル情報を配列にする
KEYS = ("name", "id", "members", "purpose", "topic")
for channel in channels:
# 辞書からKEYSに従った順番に値を取り出す
values.append([channel[k] for k in KEYS])
data = [
{
"range": "A2:E100",
"values": values,
}
]
body = {
"valueInputOption": "RAW",
"data": data,
}
service = get_sheets_service()
result = (
service.spreadsheets()
.values()
.batchUpdate(spreadsheetId=SHEET_ID, body=body)
.execute()
)
return result
def main() -> None:
"""
Slackのチャンネル一覧情報を取得する
"""
# チャンネル一覧を取得
channels = get_channels_list()
# チャンネルの情報をスプレッドシートに書き出す
store(channels)
if __name__ == "__main__":
main()