Search code examples
pythonslackslack-api

Can't fetch 'channel_list' in Python


I am using Slack + Python and trying to fetch channel_list after authenticating with Slack user. But the application can't allow them to choose a channel in channel_list. I use python_slackclient

AttributeError: 'WebClient' object has no attribute 'channels'

This is code:
Slack api client

def fetch_channels():
  client = slack.WebClient(token=current_user.token) 
  channels = client.channels.list
  return channels
  ## <bound method WebClient.channels_list of <slack.web.client.WebClient object at XXXXXXX>>

  return channels

View

<select name="channel">
  {% for channel in channels %}
  <option value="{ channel.name }">{ channel.name }</option>
  {% endfor %}
</select>

via Channel List


Solution

  • The reason you are getting this error is that the name of the method is spelled incorrectly.

    While the API endpoint is called channels.list, the method of the class WebClient is called channels_list. Also it's a method, so you need to call it with parenthesis. Finally it won't return the list of channels directly, but a dict that includes the list of channels as the property names channels.

    Btw. You can see all parameters and what the methods return in the description of the API endpoint.

    Here is a corrected version of your code:

    response = client.channels_list()
    assert(response['ok'])
    channels = response['channels']