无法在 Python 中获取 'channel_list'
Can't fetch 'channel_list' in Python
我正在使用 Slack + Python 并在通过 Slack 用户身份验证后尝试获取 channel_list
。但是应用程序不允许他们选择 channel_list
中的频道。我使用 python_slackclient
AttributeError: 'WebClient' object has no attribute 'channels'
这是代码:
松弛 api 客户端
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
查看
<select name="channel">
{% for channel in channels %}
<option value="{ channel.name }">{ channel.name }</option>
{% endfor %}
</select>
您收到此错误的原因是方法名称拼写错误。
虽然 API 端点被调用 channels.list
,但 class WebClient 的方法被调用 channels_list
。这也是一个方法,所以你需要用括号调用它。最后,它不会直接 return 频道列表,而是包含频道列表作为 属性 名称 channels
的字典。
顺便说一句。您可以在 API 端点的描述中看到所有参数和方法 return。
这是您的代码的更正版本:
response = client.channels_list()
assert(response['ok'])
channels = response['channels']
我正在使用 Slack + Python 并在通过 Slack 用户身份验证后尝试获取 channel_list
。但是应用程序不允许他们选择 channel_list
中的频道。我使用 python_slackclient
AttributeError: 'WebClient' object has no attribute 'channels'
这是代码:
松弛 api 客户端
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
查看
<select name="channel">
{% for channel in channels %}
<option value="{ channel.name }">{ channel.name }</option>
{% endfor %}
</select>
您收到此错误的原因是方法名称拼写错误。
虽然 API 端点被调用 channels.list
,但 class WebClient 的方法被调用 channels_list
。这也是一个方法,所以你需要用括号调用它。最后,它不会直接 return 频道列表,而是包含频道列表作为 属性 名称 channels
的字典。
顺便说一句。您可以在 API 端点的描述中看到所有参数和方法 return。
这是您的代码的更正版本:
response = client.channels_list()
assert(response['ok'])
channels = response['channels']