[英]Send Message to Slack Message to Slack Channel through Slack Chat.Post Message API?
虽然有人问过这个问题。 我正在尝试通过 slack chat.post 消息 api这个链接将消息发送到 slack 频道但是消息没有被发送到频道,我得到了
successfully completed post_reports_to_slack and status code 200
代码是:
import requests
def post_image():
url="https://slack.com/api/chat.postMessage"
data = {
"token": "xoxb-7701412070-tooken",
"channels": ['#channel_name'],
"text":"Message to send",
}
response = requests.post(
url=url, data=data,
headers={"Content-Type": "application/json"})
#response = requests.post(url=url, data=payload, params=data, files=file_upload)
if response.status_code == 200:
print("successfully completed post_reports_to_slack "
"and status code %s" % response.status_code)
else:
print("Failed to post report on slack channel "
"and status code %s" % response.status_code)
post_image()
我还尝试使用发布文件 api 发送文件,它工作正常。使用 webhook 也工作正常。但我想通过聊天发送消息。发布消息 API。
我建议使用 Slack SDK。 Slack在这里有一个 Python 的 SDK 。 我在您的代码中注意到的第一件事是关键channels
应该是channel
并且它应该是一个字符串。 经过一些测试,Slack 似乎总是返回 200 并可能出现错误。 最终代码应该是:
import requests
def post_image():
url="https://slack.com/api/chat.postMessage"
data = {
"token": "xoxb-7701412070-tooken",
"channel": 'C1234567890',
"text": "Message to send",
}
response = requests.post(
url=url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded"})
#response = requests.post(url=url, data=payload, params=data, files=file_upload)
if response.status_code == 200:
print("successfully completed post_reports_to_slack "
"and status code %s" % response.text)
else:
print("Failed to post report on slack channel "
"and status code %s" % response.status_code)
post_image()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.