[英]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.