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 https://hooks.slack.com/services/xx/yy/zz
, you are good to follow to the next step.
Creating the Script
First we need requests:
1
|
|
Then we will create the slack_notifier.py
, just ensure that you replace your slack webhook url and slack channel to yours:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
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:
1
|
|
Which will look like the following on slack:
And when recovery is in place, we can action the following:
1
|
|
Which will look like this:
Thanks
That was a basic example on how you can use python to send slack messages.