Sending Slack Messages with Python

's avatar
Ruan Bekker ・February 15, 2021
1 minute read ・Updated February 20, 2021
Sending Slack Messages with Python

In this post I will demonstrate how to send messages to slack using python based on the status of an event.

We will keep it basic, that when something is down or up, it should send a slack message with the status, message, color and embed your grafana dashboard links inside the alert (or any links that you would like).

Create a Webhook

From a previous post on how to use curl to send slack messages I showed how to create your webhook, so you can just follow that post if you want to follow along.

Once you have a webhook, which will look like, you are good to follow to the next step.

Creating the Script

First we need requests:

$ pip install requests

Then we will create the, just ensure that you replace your slack webhook url and slack channel to yours:

import requests
import sys
import os

SLACK_WEBHOOK_URL = '<your>/<slack>/<webhook>'
SLACK_CHANNEL = "#your-slack-channel"
ALERT_STATE = sys.argv[1]

alert_map = {
    "emoji": {
        "up": ":white_check_mark:",
        "down": ":fire:"
    "text": {
        "up": "RESOLVED",
        "down": "FIRING"
    "message": {
        "up": "Everything is good!",
        "down": "Stuff is burning!"
    "color": {
        "up": "#32a852",
        "down": "#ad1721"

def alert_to_slack(status, log_url, metric_url):
    data = {
        "text": "AlertManager",
        "username": "Notifications",
        "channel": SLACK_CHANNEL,
        "attachments": [
            "text": "{emoji} [*{state}*] Status Checker\n {message}".format(
            "color": alert_map["color"][status],
            "attachment_type": "default",
            "actions": [
                    "name": "Logs",
                    "text": "Logs",
                    "type": "button",
                    "style": "primary",
                    "url": log_url
                    "name": "Metrics",
                    "text": "Metrics",
                    "type": "button",
                    "style": "primary",
                    "url": metric_url
    r =, json=data)
    return r.status_code

alert_to_slack(ALERT_STATE, "https://grafana-logs.dashboard.local", "https://grafana-metrics.dashboard.local")

Testing it out

Time to test it out, so let's assume something is down, then we can react on that event and action the following:

$ python down

Which will look like the following on slack:


Checkout our latest product - the ultimate tailwindcss page creator 🚀

And when recovery is in place, we can action the following:

$ python up

Which will look like this:



That was a basic example on how you can use python to send slack messages.

Still have a question?

Get help or discuss anything development/design related.

Start a discussion