Created
May 14, 2017 12:52
-
-
Save jamesmorgan/07611d96916e7c048840865ca3b65c89 to your computer and use it in GitHub Desktop.
Pushes Data from a Firebase DB ref to Slack
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
'use strict'; | |
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const IncomingWebhook = require('@slack/client').IncomingWebhook; | |
const url = 'https://hooks.slack.com/services/${SLACK_API}'; | |
const functions = require('firebase-functions'); | |
/** | |
* Pushes from firebase ref /exception to slack chanel | |
*/ | |
exports.pushExceptionToSlack = functions.database.ref('/exception/{exceptionId}').onWrite(event => { | |
if (event.data.exists()) { | |
let webhook = new IncomingWebhook(url); | |
let message = | |
`ERROR - ${event.timestamp} - ${event.params.exceptionId} | |
\n ${event.resource} | |
\n ${JSON.stringify(event.data.val())}`; | |
webhook.send(message, function (err, res) { | |
if (err) { | |
console.log('Error:', err); | |
} else { | |
console.log('Message sent: ', res); | |
event.data.ref.remove(); // delete ref after being successfully posted to slack | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment