🐍
Slack Enterprise Gridにおいて管理者権限を持つユーザーの取得と管理方法(Python - Slack Bolt)
Slack Enterprise Gridにおいて管理者権限を持つユーザーの取得と管理方法(Python - Slack Bolt)📌🔑🛠️
Slack Enterprise Gridは、多数のワークスペースを効率的に管理できる企業向けSlackプランです。この記事では、Slack Bolt(Python)を使い、Enterprise Grid環境において管理者ユーザーや特殊ロールを持つユーザーの情報を取得・管理する代表的なAdmin APIの利用方法を解説します。
Slack Boltのセットアップ🚀⚙️💻
まず、Slack Boltをインストールします。
pip install slack_bolt
次に、Adminスコープ付きのユーザートークンを使用してBoltアプリを初期化します。
from slack_bolt import App
app = App(token="xoxp-your-admin-user-token")
① 全ユーザー一覧の取得📋👥🔍
admin.users.list
- 組織全体または特定のワークスペースのユーザー一覧を取得します。
- 必要なスコープ:
admin.users:read
使用例
response = app.client.admin_users_list(team_id="T12345678")
if response.get("ok"):
users = response["users"]
for user in users:
print(f"ユーザーID: {user['id']}, メール: {user['email']}, 管理者: {user['is_admin']}, オーナー: {user['is_owner']}")
else:
print(f"エラー: {response['error']}")
レスポンス例
{
"users": [
{
"id": "U12345678",
"email": "user@example.com",
"is_admin": true,
"is_owner": false,
"enterprise_user": {
"is_admin": true,
"is_owner": false
}
}
]
}
② ワークスペース管理者のユーザーID取得👑📌📂
admin.teams.admins.list
- 指定したワークスペース内の管理者一覧を取得します。
- 必要なスコープ:
admin.teams:read
使用例
response = app.client.admin_teams_admins_list(team_id="T12345678")
if response.get("ok"):
admin_ids = response["admin_ids"]
print(f"管理者一覧: {admin_ids}")
else:
print(f"エラー: {response['error']}")
レスポンス例
{
"ok": true,
"admin_ids": ["U11111111", "U22222222"]
}
③ 特殊ロール(ユーザー管理者・オーナー等)の取得🛡️🔖📊
admin.roles.listAssignments
- 組織全体の特殊ロール割り当て情報を取得します。
- 必要なスコープ:
admin.roles:read
使用例
response = app.client.admin_roles_listAssignments()
if response.get("ok"):
assignments = response["role_assignments"]
for assignment in assignments:
print(f"ユーザーID: {assignment['user_id']}, ロールID: {assignment['role_id']}, 対象ID: {assignment['entity_id']}")
else:
print(f"エラー: {response['error']}")
レスポンス例
{
"ok": true,
"role_assignments": [
{
"role_id": "Rl03",
"entity_id": "E123ABC456",
"user_id": "U44444444",
"date_create": 1677100000
}
]
}
④ ワークスペースをまたいだユーザーやチャンネルの管理🌐🔗📥
ユーザーを特定ワークスペースに招待
response = app.client.admin_users_invite(
team_id="T12345678",
email="newuser@example.com",
channel_ids="C12345678,C23456789"
)
if response.get("ok"):
print("招待メールを送信しました。");
else:
print(f"エラー: {response['error']}")
組織全体のチャンネル作成
response = app.client.admin_conversations_create(
name="general-announcements",
is_private=False,
org_wide=True
)
if response.get("ok"):
print(f"チャンネル作成成功: {response['channel_id']}")
else:
print(f"エラー: {response['error']}")
まとめ🎯🚀📈
Slack Bolt(Python)を使えば、Enterprise Grid環境での管理者ユーザーや特殊ロールの情報を簡単かつ効果的に取得・管理できます。これにより、管理業務の効率化を実現し、組織全体のコミュニケーションを円滑に進めることが可能です。
Discussion