Hello, there fellow friends!
This is an example of how to send data to Slack webhooks in Python with the requests module.
First, you need to set up your incoming-webhook into your desired slack channel and use the example below to test your payload.
Don’t forget to change the code webhook_url


'''
This is an example of how to send data to Slack webhooks in Python with the
requests module.
Detailed documentation of Slack Incoming Webhooks:
https://api.slack.com/incoming-webhooks
'''

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/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
slack_data = {'text': "API call using Python test by Michael "}

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


1 Comment

WINDA EKA SAMODRA · July 7, 2019 at 6:22 AM

What the function of Slack webhooks in Python?

Leave a Reply to WINDA EKA SAMODRA Cancel reply

Avatar placeholder

Your email address will not be published. Required fields are marked *