LoginSignup
2
0

Slack APIのconversations.listでプライベートチャンネルが取得できなくてハマった

Last updated at Posted at 2024-03-25

ハマりどころ

conversations.listを利用してワークスペース内のチャンネル一覧を取得するため、types='public_channel,private_channel'としてconversations_listを実行したが、パブリックチャンネルしか取得できない

python
# 取得したトークン
token = os.environ.get('SLACK_BOT_TOKEN')

# Slack の WebClient を生成
client = WebClient(token=token)

# チャンネル一覧を取得
response = client.conversations_list(types='public_channel,private_channel')
channels = response['channels'] # <-パブリックチャンネルしか取得できない

pythonでのapiの呼び出し方の問題かと思い、slackのAPIページのテスターで実行してみたものの結果は変わらず

APIのexampleでもpublic_channel,private_channelとなってるのになんでだろう?と色々試してみるもののなかなか解決せず

image.png

解決法

types='public_channel,private_channel'とするとpublic_channelが1ページ目、public_channelが2ページ目に出力されるので、以下のように各ページに対して処理を行ってあげる

python
# 取得したトークン
token = os.environ.get('SLACK_BOT_TOKEN')

# Slack の WebClient を生成
client = WebClient(token=token)

# チャンネル一覧を取得
response = client.conversations_list(types='public_channel,private_channel')
for page in response:
    for channel in page['channels']:
         # 個別の処理

@seratch(Kazuhiro Sera)さんコメントありがとうございます!)

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0