Skip to content

Instantly share code, notes, and snippets.

@jamesmorgan
Created May 14, 2017 12:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmorgan/07611d96916e7c048840865ca3b65c89 to your computer and use it in GitHub Desktop.
Save jamesmorgan/07611d96916e7c048840865ca3b65c89 to your computer and use it in GitHub Desktop.
Pushes Data from a Firebase DB ref to Slack
'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