问题描述
(此处为 Slack API n00b)
我正在尝试使用 Slack API 在我们的一些公共 Slack 频道(我们组织内部)中搜索消息。
我通过 Python SDK 使用 https://www.slack.com/api/search.messages
API,这意味着我正在执行以下操作:
import logging
from slack_sdk import WebClient
from pprint import pprint
def main():
logging.basicConfig(level=logging.INFO)
# TOKEN:
t = 'xoxp-***'
print("Getting client")
c = WebClient(token=t)
response = c.search_messages(query='in:#some_public_channel some-search-string')
print("Response Code: {}".format(response.status_code))
print("Found {} messages.".format(len(response.data['messages']['matches'])))
if __name__ == '__main__':
main()
如果我使用“用户 OAuth 令牌”,我只能使上面的代码正常工作,不幸的是,该令牌也可以访问与该用户相关的所有私人消息。
我想要一个只能通过公共渠道搜索的 OAuth 令牌(只读)。通过这种方式,它可以在同一团队的成员之间共享(例如)。
有什么建议可以解决这个问题吗?
背景信息:
DEBUG:slack_sdk.web.slack_response: Received the following response -
status: 200,headers: {...},body: {'ok': False,'error': 'not_allowed_token_type'}
- 我使用的令牌是通过我专门为此目的而创建的“应用程序”生成的:
解决方法
只有用户才能访问 search.messages API,机器人不能访问。我可以建议的最佳解决方法是 -
- 授予您的机器人 channels:history 和 channels:join 范围
- 致电 conversations.join 获取相关公共渠道。
- 调用 conversations.history 并遍历结果以查找
some-search-string