├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Respoke 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # example-webhook-nodejs-server 2 | 3 | ## Processing Webhooks from Respoke 4 | 5 | Webhooks are a powerful, yet little known feature of Respoke. 6 | 7 | When events happen in your app - like endpoints joining or leaving a group - you can 8 | get notified via webhook. 9 | 10 | ## Prerequesites 11 | 12 | * [Node.js](https://nodejs.org/download/) 13 | 14 | ## 0. Download ngrok (optional) 15 | 16 | For fast local development, this example will use [ngrok](https://ngrok.com/download) to tunnel a port on your localhost to a subdomain of ngrok. This lets Respoke send you webhooks on https://your-custom-subdomain.ngrok.com and the requests are proxied to your local machine. 17 | 18 | [Download ngrok](https://ngrok.com/download) and take note of where you put the `ngrok` file. 19 | 20 | ## 1. Configure your webhook 21 | 22 | To get started, go the the [Respoke dev portal](https://portal.respoke.io) and create a new app. 23 | 24 | Then navigate down to the *Webhooks* section. 25 | 26 | ![respoke dev portal webhook URL input](http://i.imgur.com/vCJUWED.png) 27 | 28 | Put in your webhook endpoint and hit *Update*. 29 | 30 | If you're using `ngrok`, just make up something like `https://actual-cannibal-shia-labeouf.ngrok.com`. 31 | 32 | ## 2. Clone the repo and install 33 | 34 | An example webhook server is on the Respoke GitHub: 35 | 36 | ```bash 37 | git clone https://github.com/respoke/example-webhook-nodejs-server 38 | cd example-webhook-nodejs-server 39 | npm install 40 | ``` 41 | 42 | ## 3. Run the webhook server 43 | 44 | ```bash 45 | npm start 46 | ``` 47 | 48 | The server will run on http://localhost:3009 and print incoming webhooks. 49 | 50 | 51 | 52 | ## 4. Fire up the ngrok tunnel (optional) 53 | 54 | ```bash 55 | cd path/to/ngrok/folder 56 | ./ngrok --subdomain=actual-cannibal-shia-labeouf 3009 57 | ``` 58 | 59 | This tells `ngrok` to tunnel all requests to `https://actual-cannibal-shia-labeouf.ngrok.com` to your `localhost:3009`. 60 | 61 | ## 5. Receive webhooks 62 | 63 | Next, do something that initiates some Respoke activity. You could clone the [web examples](https://github.com/respoke/web-examples) and set them up to use your `appId`. 64 | 65 | ![example webhook traffic](http://i.imgur.com/B0EYgBi.png) 66 | 67 | That's really all it takes to start responding to webhooks. 68 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var connect = require('connect'); 3 | var http = require('http'); 4 | var debug = require('debug')('wh'); 5 | var app = connect(); 6 | var format = function (input) { return JSON.stringify(input, null, 2); }; 7 | var port = 3009; 8 | 9 | // Parse JSON requests into `req.body`. 10 | var bodyParser = require('body-parser'); 11 | app.use(bodyParser.json()); 12 | 13 | // Print all incoming requests, then respond to them. 14 | app.use(function (req, res) { 15 | debug('incoming webhook', req.method, req.url, format(req.body)); 16 | res.end('OK\n'); 17 | }); 18 | 19 | // create node.js http server and listen on port 3009 20 | var server = http.createServer(app); 21 | server.on('error', function (err) { 22 | debug('server error', err); 23 | }); 24 | server.listen(port, function () { 25 | debug('Respoke webhook server is listening on port', port); 26 | }); 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "respoke-webhook-example-server", 3 | "version": "1.0.1", 4 | "description": "An example webhook server for Respoke.js", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "DEBUG=wh* node index.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "@ruffrey", 11 | "license": "MIT", 12 | "dependencies": { 13 | "body-parser": "^1.12.0", 14 | "connect": "^3.3.4", 15 | "debug": "^2.1.3" 16 | } 17 | } 18 | --------------------------------------------------------------------------------