Thursday, 25 May 2017

How to send firebase data to slack using cloud functions?

I cloned this sample minimal-webhook and got this working. I wanted to extend this functionality by sending the writes to slack.

I am using the incoming webhook of slack. I guess the issue is the way how I add the data payload in nodejs. Can anyone help? Thanks in advance. Attached my code and error log from firebase.

const functions = require('firebase-functions');
const request = require('request-promise');
const WEBHOOK_URL = 'https://hooks.slack.com/services/abc'
var headers = {
    'Content-type': 'application/json'
};
exports.webhook = functions.database.ref('/hooks/{hookId}').onWrite(event => {
  return request({
    uri: WEBHOOK_URL,
    method: 'POST',
    headers: headers,
     body: event.data.toJSON,
    resolveWithFullResponse: true
  }).then(response => {
    if (response.statusCode >= 400) {
      throw new Error(`HTTP Error: ${response.statusCode}`);
    }
    console.log('SUCCESS! Posted', event.data.ref);
  });
});`

Also attached the error log from Firebase



via vira

No comments:

Post a Comment