将Python变量内容发送到Slack Webhook

时间:2019-10-25 12:12:12

标签: python slack

我是初次接触python的新手。我已成功使用类似以下的代码将数据从python脚本发送到松弛的Webhook:

import json
import requests

# Set the webhook_url to the one provided by Slack when you create the webhook at https://my.slack.com/services/new/incoming-webhook/
webhook_url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
slack_data = {"text": "<https://alert-system.com/alerts/1234|Click here> for details!"}

response = requests.post(
    webhook_url, data=json.dumps(slack_data),
    headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:\n%s'
        % (response.status_code, response.text)
    )

我正在寻找一种方法来发送脚本中python变量的内容作为slack_data有效负载。例如,如果我有一个'date'变量,我想将其包括在发送给slack的字符串中,而不仅仅是在slack_data变量中列出的文本字符串中。有人可以建议如何做吗?

提前谢谢!

0 个答案:

没有答案