Skip to content

Instantly share code, notes, and snippets.

@devStepsize
Last active August 7, 2023 09:28
Show Gist options
  • Save devStepsize/b1b795309a217d24566dcc0ad136f784 to your computer and use it in GitHub Desktop.
Save devStepsize/b1b795309a217d24566dcc0ad136f784 to your computer and use it in GitHub Desktop.
POST a JSON payload to a Slack Incoming Webhook using Python requests
'''
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/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
slack_data = {'text': "Sup! We're hacking shit together @HackSussex :spaghetti:"}
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)
)
@rafaelnpaiva
Copy link

Hi @guymorrell
A simple text worked fine , but I'm trying to send this payload :
{
"Title": "Resources in AWS ~> eng-sbx",
"icon_emoji": ":money:",
"Resource_Count": {
"CloudFormationStack": "259",
"CloudWatchLog Group": "4319",
"DynamoDBTable": "119",
"EC2Address": "7",
"EC2Instance": "34",
"EC2Security Group": "877",
"GlueDatabase": "14",
"LambdaFunction": "831",
"ECRRepository": "117",
"ECRImage": "6602",
"ELBLoad Balancer": "34",
"EMRCluster": "1",
"ElastiCacheCluster": "20",
"ElastiCacheReplication Group": "5",
"GlueCrawler": "10",
"KinesisStream": "24",
"EFSFile System": "9",
"EKSNode Group": "13",
"EKSCluster": "5",
"SageMakerApp": "7",
"SageMakerNotebook Instance": "2",
"RedshiftSnapshot": "2",
"RDSInstance": "18",
"RDSSnapshot": "3",
"EC2Snapshot": "40",
"EC2Volume": "224",
"S3Bucket": "123",
"IAMPolicy": "157",
"IAMRole": "359"
}
}

I keep having error 400 . Can you help me please ?

Traceback (most recent call last):
File "testing.py", line 183, in
print(test())
File "testing.py", line 179, in test
response = post_slack(result)
File "testing.py", line 160, in post_slack
raise ValueError(
ValueError: Request to slack returned an error 400, the response is:
missing_text_or_fallback_or_attachments

@aswinin22
Copy link

json={"text": data}

Amazing! Working like a charm!

@aronmarden
Copy link

@rafaelnpaiva - I have this problem also.

You need to use the block strucutre that Slack is expecting: (found here)

{
	"blocks": [
		{
			"type": "section",
			"text": {
				"type": "plain_text",
				"text": "This is a plain text section block.",
				"emoji": true
			}
		}
	]
}

I also need to send a simple JSON payload, and I really don't understand why there isn't a way to send our own key:value JSON block...I get that it won't nessasarily be pretty like Slack likes things to be... but sometimes we just need payloads.

@guymorrell do you have any ideas?

@eugenyuk
Copy link

eugenyuk commented Aug 7, 2023

Hi, this is working example:

import requests

def send_slack_notif():

    url = 'YOUR_SLACK_WEBHOOK_URL'
    payload = {
        "blocks": [
            {
			    "type": "section",
			    "text": {
				    "type": "plain_text",
				    "text": ":red_circle: This is a plain text section block.",
				    "emoji": True
			    }
	}
        ]
    }
    r = requests.post(url, json=payload)
    
send_slack_notif()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment