python - 在 Python 中使用 Slack API
问题描述
我正在按照以下教程Slack API 教程在 slack 上发布消息。我已经创建了 Slack 应用程序并且能够将它与 Incoming Webhooks 一起使用现在我想使用来自 python 的 API。所以我尝试了:
import os
from slackclient import SlackClient
slack_token = 'xoxp-long-sequence-of-numbers'
sc = SlackClient(slack_token)
sc.api_call(
"chat.postMessage",
channel="my_test_channel",
text="Hello from Python! :tada:"
)
我收到以下错误消息:
{'error': 'missing_scope',
'headers': {'Access-Control-Allow-Headers': 'slack-route, x-slack-version-ts',
'Access-Control-Allow-Origin': '*',
'Access-Control-Expose-Headers': 'x-slack-req-id',
'Cache-Control': 'private, no-cache, no-store, must-revalidate',
'Connection': 'keep-alive',
'Content-Encoding': 'gzip',
'Content-Length': '105',
'Content-Type': 'application/json; charset=utf-8',
'Date': 'Fri, 05 Apr 2019 17:07:06 GMT',
'Expires': 'Mon, 26 Jul 1997 05:00:00 GMT',
'Pragma': 'no-cache',
'Referrer-Policy': 'no-referrer',
'Server': 'Apache',
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload',
'Vary': 'Accept-Encoding',
'Via': '1.1 6529456e34a07353ab1987432f192696.cloudfront.net (CloudFront)',
'X-Accepted-OAuth-Scopes': 'chat:write:user',
'X-Amz-Cf-Id': 'wunHQVQpZicf-ynJO2u_n6CAQEGlYBH67ysu0fP1mfTEt86rRiAbrw==',
'X-Cache': 'Miss from cloudfront',
'X-Content-Type-Options': 'nosniff',
'X-OAuth-Scopes': 'identify,incoming-webhook',
'X-Slack-Req-Id': '990760fb-b110-43a1-86cb-fd4f8e2e34fa',
'X-Via': 'haproxy-www-km3w',
'X-XSS-Protection': '0'},
'needed': 'chat:write:user',
'ok': False,
'provided': 'identify,incoming-webhook'}
我的频道名称字面意思是:my_test_channel
。可能我需要使用不同的名称,但我怎样才能找到它?
更新
所以我找到了编码的频道 ID 并将我的脚本更改为:
import os
from slackclient import SlackClient
slack_token = 'xoxp-long-sequence-of-numbers'
sc = SlackClient(slack_token)
sc.api_call(
"chat.postMessage",
channel="CHXXXXXXX",
text="Hello from Python! :tada:"
)
但错误仍然存在missing scope
解决方案
根据错误('需要':'chat:write:user'),它看起来缺少用户选项:
sc.api_call(
"chat.postMessage",
channel="my_test_channel",
text="Hello from Python! :tada:"
user="U0XXXXXXX"
)
推荐阅读
- html - 在弹出窗口中显示 base64 图像仅显示图像的一部分?
- mysql - MySQL Workbench 未在 ubuntu 上打开
- apache - 删除 XAMPP 中 URL 的最后一个斜杠
- sql - 向表中添加列和数据
- powerbi - 将日期与其所属的范围匹配
- python - 如何按天聚合多列 [Pyspark]?
- angular - Angular 5 在 src/app/global-notifications/global-notifications.directive.spec.ts 中出现错误
- sapui5 - 在 TreeTable 绑定中遍历结果 - SAP UI5
- angular - 如何在 Angular 中测试 mat-checkbox 的绑定?
- machine-learning - 为什么过滤器不能学习相同的特征