【发布时间】:2022-10-16 14:38:25
【问题描述】:
我需要获取特定用户令牌(“xoxp ...”)所属的所有公共和私人 Slack 频道的列表。问题是当前 api 只返回一些私人频道,而不是全部(但它正在返回一些)。它曾经返回所有,但现在缺少一些(在此之前由用户找到)。这开始发生在 3 月之后的某个时间(我最后一次查询 API)。
我试过了:
- 创建一个新的私人频道并将用户添加到它以查看它是否看到 => 它确实
- 从频道中删除用户,当 api 调用并将用户重新添加到频道时它看不到 => 问题仍然存在
- 将应用重新安装到工作区 => 问题仍然存在
工作区只有大约 100 个频道,包括已弃用的频道,所以我知道我没有达到限制。
这是我的代码(在 Python 中):
def _getChannels(self, _next_cursor=""):
""" Needs scope channels:read
Archived channels are included by default.
INCLUDES private channels the calling user (person whose token is being used) has access to
"""
kwargs = {"limit":1000,
"types":"public_channel,private_channel"}
if _next_cursor:
kwargs["cursor"] = _next_cursor
results_full = self._callApi("conversations.list", "_getChannels()", kwargs)
results = results_full["channels"]
next_cursor = results_full["response_metadata"]["next_cursor"]
if next_cursor: # Isn't empty
results = results + self._getChannels(next_cursor)
return results
def _callApi(self, call_type, calling_function, kwargs={}):
""" calling_function is a string for error message reporting """
# New API can't handle booleans or extra params
pass_error_through = kwargs.get(self.PASS_ERROR_THROUGH, False)
if self.PASS_ERROR_THROUGH in kwargs:
kwargs.pop(self.PASS_ERROR_THROUGH)
for key in kwargs:
if type(kwargs[key]) == bool:
kwargs[key] = str(kwargs[key]).lower()
# New api raises exceptions instead of returning error, so need to catch
try:
result = self._slack_client.api_call(call_type, params=kwargs)
except Exception as E:
result = str(E) # You used to be able to just call result["error"]
if "error" in result:
if "ratelimited" in result:
print("\nRatelimited. Waiting one min before retrying.", flush=True)
sleep(60)
return self._callApi(call_type, calling_function, kwargs)
elif not pass_error_through:
error_message = ("ERROR: Call to " + calling_function +
" failed due to " + result + ". " +
"\n\nkwargs: " + str(kwargs) + "\n\n--End Message--")
#if "needed" in result:
# error_message += "It needs: " + result["needed"]
print() # To provide spacing before the traceback starts
raise ValueError(error_message)
return result
【问题讨论】:
标签: python slack slack-api channels