Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
this is a nodejs script that listens to a slack channel receiving newrelic alerts and forwards them to google chat
#!/usr/bin/env node
// npm install @slack/client hangouts-chat
const { RTMClient } = require('@slack/client');
const IncomingWebHook = require('hangouts-chat').IncomingWebHook
const incomingWebHook = new IncomingWebHook('<webhook_url>') // Add a webhook in gchat
const imagePerSituation = function (situation) {
switch (situation) {
case "a30200":
case "opened":
return "https://i.imgur.com/ax3thcf.png?1";
case "2eb886":
case "closed":
return "https://i.imgur.com/vME8eoT.png?1";
default:
return "https://i.imgur.com/T7Zp38r.png?1";
}
};
// real time messages slack api client
// You need an incoming webhook to receive the alert messages from newrelic to a specific channel using https://slack.com/apps/A0F7XDUAZ-incoming-webhooks and, with that webhook, configure a notification channel in nr as a slack channel type
const rtm = new RTMClient('<slack_bot_token>'); // Add a bot https://my.slack.com/services/new/bot listening from the newrelic channel and get the token from there
rtm.start();
rtm.on('message', function (message) {
// Skip messages that are from my own user ID
if ((!message.subtype && message.user === rtm.activeUserId)) {
return;
}
// Handle the message and forward it to gchat webhook
if (message.type === "message" && message.subtype !== 'file_share') {
let msjObjText;
if (message.text) {
msjObjText = message.text;
}
if (message.attachments != null && message.attachments.length > 0) {
let headerObj;
if (message.attachments[0].pretext) {
let incidentMsg = message.attachments[0].pretext.split("|")[1].replace("*", "");
let situation = incidentMsg.split(">");
incidentMsg = situation[0];
situation = situation[1];
headerObj = {
"title": incidentMsg,
"subtitle": situation,
"imageUrl": imagePerSituation(message.attachments[0].color || situation)
};
}
let sections = [{ "widgets": [] }];
let msjObjForG = { "cards": [{ "header": headerObj }, { "sections": sections }], "text": msjObjText }
for (let i in message.attachments[0].fields) {
let field = message.attachments[0].fields[i];
let value = field.value.replace(/(<|>)/g, "");
sections[0].widgets.push({
"keyValue":
{
"topLabel": field.title,
"content": value.indexOf("|") >= 0 ? value.split("|")[1] : value,
"contentMultiline": true
}
}
)
}
if (message.attachments[0].image_url) {
sections[0].widgets.push({
"image": { "imageUrl": message.attachments[0].image_url }
})
}
let buttonsArr = [];
for (let i in message.attachments[0].actions) {
let act = message.attachments[0].actions[i];
buttonsArr.push({
"textButton": {
"text": act.text,
"onClick": {
"openLink": {
"url": act.url
}
}
}
})
}
if (buttonsArr.length > 0) {
sections[0].widgets.push({ "buttons": buttonsArr });
}
let response = incomingWebHook.send(msjObjForG);
response.then(function (data) {
console.log(data);
}).catch(function (err) {
console.log(err);
});
} else {
incomingWebHook.sendText(msjObjText);
}
} else {
console.log("Generic Message: ", message);
}
return;
});
@aortmann

This comment has been minimized.

Copy link
Owner Author

@aortmann aortmann commented Aug 7, 2019

I recommend running it with forever, since the rtm client is "expelled" randomly by slack and I don't have time to handle this case.

These are some examples of the messages that this bot generates:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.