<目次>
(1) SlackのAPIでユーザー一覧を取得する方法をご紹介
(1-0) やりたいこと
(1-1) STEP1:Slackボットの作成
(1-2) STEP2:サンプルプログラム
(1) SlackのAPIでユーザー一覧を取得する方法をご紹介
(1-0) やりたいこと
●アウトプットイメージ
●処理の流れ
(1-1) STEP1:Slackボットの作成
・②OAuth & Permissionsを選択
↓
(1-2) STEP2:サンプルプログラム
slack_get_user_list.py
import json import pandas as pd import requests import os from pathlib import Path from dotenv import load_dotenv # 親フォルダパスを取得 file_path = Path(__file__).parent # 「.env」ファイルのパスを設定 env_path = file_path/'.env' # 「.env」ファイルのロード load_dotenv(dotenv_path=env_path) def main(): # APIのURL url = os.environ['URL'] # ヘッダー部 headers = { "Content-Type":"application/json", "User-Agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)", "Authorization":"Bearer "+os.environ['SLACK_BOT_TOKEN'] } # POSTリクエスト # →ユーザー一覧を取得します。 response = requests.post(url, headers=headers) # レスポンスの内容をtext形式で取得 result = response.text # レスポンスのJSONを表形式データ(DataFrame)に変形する # JSONライブラリを用いて、JSONファイルのデータをJSONオブジェクトとしてロード json_object = json.loads(result) # pandasの「json_normalize」関数を用いてデータを正規化 # 第1引数:json形式のオブジェクト # 第2引数:分解対象のデータのkey # 第3引数:その他、分解した明細に付与するデータ df = pd.json_normalize(json_object, record_path =['members']) # df = pd.json_normalize(json_object, record_path =['students'],meta=['school_name', 'class']) # 表形式データ(DataFrame)をcsv出力する # →日本語も含まれるため、エンコーディングはshift-jisを指定 df.to_csv('./slack_get_user_list.csv', sep=',', encoding='shift-jis', header=True) if __name__ == "__main__": main()
(図131)
.env
URL='https://xxxxx.slack.com/api/users.list' SLACK_BOT_TOKEN='xoxb-xxxxx'
(図132)
(参考)
{ "ok": true, "members": [ { "id": "USLACKBOT", "team_id": "T02TA6EUXSN", "name": "slackbot", "deleted": false, "color": "757575", "real_name": "Slackbot", "tz": "America/Los_Angeles", "tz_label": "Pacific Standard Time", "tz_offset": -28800, "profile": { "title": "", "phone": "", "skype": "", "real_name": "Slackbot", "real_name_normalized": "Slackbot", "display_name": "Slackbot", "display_name_normalized": "Slackbot", "fields": {}, "status_text": "", "status_emoji": "", "status_emoji_display_info": [], "status_expiration": 0, "avatar_hash": "sv41d8cd98f0", "always_active": true, "first_name": "slackbot", "last_name": "", "image_24": "https://a.slack-edge.com/80588/img/slackbot_24.png", "image_32": "https://a.slack-edge.com/80588/img/slackbot_32.png", "image_48": "https://a.slack-edge.com/80588/img/slackbot_48.png", "image_72": "https://a.slack-edge.com/80588/img/slackbot_72.png", "image_192": "https://a.slack-edge.com/80588/marketing/img/avatars/slackbot/avatar-slackbot.png", "image_512": "https://a.slack-edge.com/80588/img/slackbot_512.png", "status_text_canonical": "", "team": "T02TA6EUXSN" }, "is_admin": false, "is_owner": false, "is_primary_owner": false, "is_restricted": false, "is_ultra_restricted": false, "is_bot": false, "is_app_user": false, "updated": 0, "is_email_confirmed": false, "who_can_share_contact_card": "EVERYONE" }, { "id": "U02SVHS9AA3", "team_id": "T02TA6EUXSN", "name": "xxxxx.xxxxx.xxxxx", "deleted": false, "color": "e7392d", "real_name": "xxxx", "tz": "Asia/Tokyo", "tz_label": "Japan Standard Time", "tz_offset": 32400, "profile": { "title": "", "phone": "", "skype": "", "real_name": "xxxx", "real_name_normalized": "xxxx", "display_name": "Rainbow Planet03", "display_name_normalized": "Rainbow Planet03", "fields": null, "status_text": "", "status_emoji": "", "status_emoji_display_info": [], "status_expiration": 0, "avatar_hash": "5cb320e52419", "image_original": "https://avatars.slack-edge.com/2022-02-22/3157324385041_5cb320e52419489e7cc6_original.jpg", "is_custom_image": true, "first_name": "xxxx", "last_name": "", "image_24": "https://avatars.slack-edge.com/2022-02-22/3157324385041_5cb320e52419489e7cc6_24.jpg", "image_32": "https://avatars.slack-edge.com/2022-02-22/3157324385041_5cb320e52419489e7cc6_32.jpg", "image_48": "https://avatars.slack-edge.com/2022-02-22/3157324385041_5cb320e52419489e7cc6_48.jpg", "image_72": "https://avatars.slack-edge.com/2022-02-22/3157324385041_5cb320e52419489e7cc6_72.jpg", "image_192": "https://avatars.slack-edge.com/2022-02-22/3157324385041_5cb320e52419489e7cc6_192.jpg", "image_512": "https://avatars.slack-edge.com/2022-02-22/3157324385041_5cb320e52419489e7cc6_512.jpg", "image_1024": "https://avatars.slack-edge.com/2022-02-22/3157324385041_5cb320e52419489e7cc6_1024.jpg", "status_text_canonical": "", "team": "T02TA6EUXSN" }, "is_admin": false, "is_owner": false, "is_primary_owner": false, "is_restricted": false, "is_ultra_restricted": false, "is_bot": false, "is_app_user": false, "updated": 1664089582, "is_email_confirmed": true, "who_can_share_contact_card": "EVERYONE" }, ~中略~ ], "cache_ts": 1670836750, "response_metadata": { "next_cursor": "" } }