Skip to content

Instantly share code, notes, and snippets.

@joriatyBen
Forked from devStepsize/slack_webhook_post.py
Last active August 24, 2019 12:23
Show Gist options
  • Save joriatyBen/76fce4fddd03514322a1e8607b6b7686 to your computer and use it in GitHub Desktop.
Save joriatyBen/76fce4fddd03514322a1e8607b6b7686 to your computer and use it in GitHub Desktop.
POST a JSON payload to Slack via Webhook
import json
import requests
data = open('message.json', 'r').read()
def post_to_slack(message):
webhook_url = 'https://hooks.slack.com/webhook'
slack_data = json.dumps({'blocks': message})
response = requests.post(
webhook_url, data=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)
)
post_to_slack(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment