Nicolas Garnier
9 years ago
4 changed files with 99 additions and 0 deletions
@ -0,0 +1,41 @@ |
|||||
|
# Webhook upon Database writes |
||||
|
|
||||
|
This template shows how a Database write can trigger a request to a hardcoded callback URL (a Webhook). The content of the modified Data is sent to the Webhook. |
||||
|
|
||||
|
## Cloud Function Code |
||||
|
|
||||
|
See file [index.js](index.js) for the code. |
||||
|
|
||||
|
This is done by sending a request to a given URL. As a sample we're using a Request Bin from [requestb.in](http://requestb.in) that will receive the Data so you can visualize it easily. make sure you create your own Request Bin and update the sample with it. |
||||
|
|
||||
|
## Sample Database Structure |
||||
|
|
||||
|
As an example we'll be using a database structure where adding or updating an element under `/hooks` will trigger the Webhook: |
||||
|
|
||||
|
``` |
||||
|
/functions-project-12345 |
||||
|
/hooks |
||||
|
/key-123456 |
||||
|
stuff: "Whatever" |
||||
|
more_stuff: "Cool" |
||||
|
/key-123457 |
||||
|
things: "A car" |
||||
|
more_things: "A truck" |
||||
|
``` |
||||
|
|
||||
|
## Trigger rules |
||||
|
|
||||
|
Below is the trigger rule for the `webhook` function making sure it's triggered when a new object is added/modified to the `hooks` attribute. |
||||
|
|
||||
|
``` |
||||
|
"functions": { |
||||
|
".source": "functions", |
||||
|
"webhook": { |
||||
|
"triggers": { |
||||
|
"database": { |
||||
|
"path": "/hooks/$hookId", |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
``` |
@ -0,0 +1,47 @@ |
|||||
|
/** |
||||
|
* Copyright 2015 Google Inc. All Rights Reserved. |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
'use strict'; |
||||
|
|
||||
|
// Create reference to the database authorized as an admin.
|
||||
|
var Firebase = require('firebase'); |
||||
|
var env = require('./env'); |
||||
|
var ref = new Firebase(env.get('firebase.database.url'), 'admin'); |
||||
|
ref.authWithCustomToken(env.get('firebase.database.secret')); |
||||
|
|
||||
|
// Import the request URL.
|
||||
|
var request = require('request'); |
||||
|
|
||||
|
// This is the URL that we will callback and send the content fo the updated data node.
|
||||
|
// As an example we're using a Request Bin from http://requestb.in
|
||||
|
// TODO: Make sure you create your own Request Bin and change this URL to try this sample.
|
||||
|
var WEBHOOK_URL = 'http://requestb.in/1mqw97l1'; |
||||
|
|
||||
|
// Reads the content of the node that triggered the function and sends it to the registered Webhook
|
||||
|
// URL.
|
||||
|
exports.webhook = function(context, data) { |
||||
|
ref.child(data.path).once('value', function(snap) { |
||||
|
request({ |
||||
|
url: WEBHOOK_URL, |
||||
|
method: 'POST', |
||||
|
json: true, |
||||
|
body: snap.val() |
||||
|
}, function(err, response) { |
||||
|
if (err) { context.done(err); } |
||||
|
else if (response.statusCode >= 400) { context.done('HTTP Error:', response.statusCode) } |
||||
|
console.log('SUCCESS! Posted', data.path); |
||||
|
}); |
||||
|
}); |
||||
|
}; |
@ -0,0 +1,7 @@ |
|||||
|
{ |
||||
|
"main": "index.js", |
||||
|
"dependencies": { |
||||
|
"firebase": "^2.4.0", |
||||
|
"request": "^2.67.0" |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue