Browse Source

Added the minimal-webhook sample.

ryanpbrewster-patch-1
Nicolas Garnier 9 years ago
parent
commit
6528d6956c
  1. 4
      README.md
  2. 41
      minimal-webhook/README.md
  3. 47
      minimal-webhook/index.js
  4. 7
      minimal-webhook/package.json

4
README.md

@ -19,6 +19,10 @@ How to moderate user input text for bad words. For example this can be used to m
Tracking when the Firebase DB (or a subset) was last modified.
### [Webhook upon Database writes](/minimal-webhook)
Writing to the Database triggers a request to a callback URL (a Webhook). The content of the modified Data is sent to the Webhook.
### [Automatic message translation](/message-translation) TODO import from go/hearth-codesamples
Integrates the Google Translate API to perform automatic text translation across any number of languages. Language codes can be stored in Firebase for on the fly changes.

41
minimal-webhook/README.md

@ -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",
}
}
}
}
```

47
minimal-webhook/index.js

@ -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);
});
});
};

7
minimal-webhook/package.json

@ -0,0 +1,7 @@
{
"main": "index.js",
"dependencies": {
"firebase": "^2.4.0",
"request": "^2.67.0"
}
}
Loading…
Cancel
Save