Send Notifications to Slack
Mango allows us to customize event handling through custom scripts. Sometimes we want to receive a notification from Mango in several channels besides email. So, in this article we will explain how to send notifications to Slack.
The first thing that we need to do is to create an incoming webhook to to a Slack channel. You have to go to custom integrations in:
https://<slack-group>.slack.com/apps/manage/custom-integrations
We go to incoming webhooks and create a new one. This will create an unique url where you can make POST requests to send custom messages, like this:
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
In Mango, you will need to create a global script where you will define a method to send notifications to Slack.
function notifyToSlack(text, attachments) {
var url = "https://hooks.slack.com/services/xxxxxxx/yyyyyy/zzzzzzzzzzzzzzzzzzzzzzzzzz";
var headers = {
"Content-Type": "application/json"
};
var data = {
"text": text,
"attachments": attachments
};
HttpBuilder.post(url, headers, data).err(function(status, headers, data) {
throw "Script failed with HTTP status : " + status;
}).resp(function(status, headers, data) {
"Hallo"
}).execute();
}
Once you define this global function, you will be able to use it in an event handler script.
var attachments = [
{
"fallback": "alarm",
"color": "#F44336",
"author_name": "Mango Automation",
"author_link": "https://infiniteutomation.com",
"author_icon": "https://i.vimeocdn.com/portrait/19103374_640x640",
"title": "DANGER! High Current Value",
"text": "Device1 Amps",
"fields": [
{
"title": "Priority",
"value": "High",
"short": false
}
],
"image_url": "https://store.infiniteautomation.com/img/logo.png",
"thumb_url": "https://store.infiniteautomation.com/img/logo.png",
"footer": "Mango Automation",
"footer_icon": "https://i.vimeocdn.com/portrait/19103374_640x640",
"ts": 123456789
}
]
notifyToSlackWithAttachments("This is a Test", attachments);
return UNCHANGED;
You have to setup the event handler as Set point type and you will need to add a target, but if you don’t want to change the target value, at the end of the script you have to add return UNCHANGED; line.
You can customize Slack messages and test how will be showed following the Slack documentation.
Finally, you should receive a notification in Slack when an event is triggered.