繁体   English   中英

从Python使用Slack API

[英]Using Slack API from Python

我正在关注以下教程Slack API教程,以在slack上发布消息。 我已经创建了Slack应用程序,现在可以将其与传入的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 可能需要使用其他名称,但是如何找到呢?

UPDATE

因此,我找到了编码的频道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

似乎根据错误(“需要”:“聊天:写:用户”)缺少用户选项:

    sc.api_call(
      "chat.postMessage",
      channel="my_test_channel",
      text="Hello from Python! :tada:"
      user="U0XXXXXXX"
    )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM