Last active
November 17, 2022 22:51
-
-
Save csepulv/7174b19c1fbe219f057f1c89b1abc806 to your computer and use it in GitHub Desktop.
Slack Notification Filter using webtask.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
function shouldNotify(data) { | |
//replace this with the appropriate check/filter for you | |
return data.text.includes("should notify"); | |
} | |
module.exports = function (context, done) { | |
if (context.data.token == context.data.EXPECTED_TOKEN) { | |
if (shouldNotify(context.data)) { | |
var url = context.data.WEBHOOK_URL; | |
var slackTeamBaseUrl = "https://" + context.data.team_domain + ".slack.com/"; | |
var originalMessageLink = slackTeamBaseUrl + "archives/" + context.data.channel_name + "/p" + context.data.timestamp.replace(".", ""); | |
var msg = { | |
attachments: [ | |
{ | |
"fallback": "<<plain text summary, used by clients that don't show attachments>> ", | |
"color": "#2992CC", | |
"author_name": context.data.user_name, | |
"author_link": slackTeamBaseUrl + "team/" + context.data.user_name, | |
"pretext": "<<your comment on the notification>>", | |
"text": context.data.text + '\n<' + originalMessageLink + '|Original Message>', | |
"ts": context.data.timestamp | |
} | |
] | |
}; | |
request({url: url, method: 'POST', json: msg}, function (error, res, body) { | |
if (error) { | |
console.error(error); | |
} | |
done(null, null); | |
}); | |
} | |
else { | |
done(null, null); | |
} | |
} | |
else { | |
console.error("invalid token:" + context.data.token); | |
done(null, null); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment