Sending Python variable contents to a Slack Webhook

Viewed 374

I'm new to looking at python with slack. I've successfully managed to send data from a python script to a slack webhook, using code similar to the following:

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)
    )

I'm looking for a way to sent the contents of a python variable in the script as the slack_data payload. For example, if I had a 'date' variable i'd like to include it in the string sent to slack, rather than just the text string listed in the slack_data variable. Can anyone advise how to do this please?

Thanks in advance!

0 Answers
Related