Created
November 28, 2018 15:52
-
-
Save rveitch/c67a1ea370c7c0185687feb4e61f0fa8 to your computer and use it in GitHub Desktop.
fb-instagram-webhooks-node-example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // heroku callback url = https://fb-instagram-webhooks.herokuapp.com/facebook | |
| // local callback url = http://rveitch.local.coschedule.ngrok.io/facebook | |
| const dotenv = require('dotenv').load(); | |
| const bodyParser = require('body-parser'); | |
| const express = require('express'); | |
| const app = express(); | |
| const xhub = require('express-x-hub'); | |
| const _ = require('lodash'); | |
| const fbUtilities = require('./utilities/fb-utilities'); | |
| const port = Number(process.env.PORT || 3000); // FACEBOOK_APPID | |
| app.set('port', port); | |
| app.listen(app.get('port')); | |
| app.use(xhub({algorithm: 'sha1', secret: process.env.APP_SECRET})); | |
| app.use(bodyParser.json()); | |
| const token = process.env.TOKEN || 'token'; | |
| const received_updates = []; | |
| app.get('/', function (req, res) { | |
| console.log(_.pick(req, ['headers', 'body', 'params', 'query'])); | |
| res.send('<pre>' + JSON.stringify(received_updates, null, 2) + '</pre>'); | |
| }); | |
| app.get(['/facebook', '/instagram'], async (req, res) => { | |
| if ((req.param('hub.mode') === 'subscribe') && (req.param('hub.verify_token') === token)) { | |
| res.send(req.param('hub.challenge')); | |
| } | |
| res.sendStatus(400); | |
| }); | |
| app.post('/facebook', async function (req, res) { | |
| try { | |
| console.log('Facebook Request:'); | |
| console.log(JSON.stringify(_.pick(req, ['headers', 'body', 'params', 'query']))); | |
| console.log('--------------------'); | |
| if (!req.isXHubValid()) { | |
| console.log('Warning - request header X-Hub-Signature not present or invalid'); | |
| res.sendStatus(401); | |
| return; | |
| } | |
| const webhookUpdate = req.body && req.body.entry.length && req.body.entry[0]; | |
| const userID = webhookUpdate && webhookUpdate.uid; | |
| if (!userID) { | |
| console.log('No User ID Found'); | |
| return res.sendStatus(200); | |
| } | |
| const userPosts = await fbUtilities.getUserPosts(userID, process.env.FACEBOOK_ACCESS_TOKEN); | |
| console.log('userPosts', userPosts); | |
| // Process the Facebook updates here | |
| received_updates.unshift(req.body); | |
| res.sendStatus(200); | |
| } catch (error) { | |
| console.log('ERROR - /facebook:', error); | |
| return res.sendStatus(200); | |
| } | |
| }); | |
| app.post('/instagram', function (req, res) { | |
| console.log('Instagram Request:'); | |
| console.log(JSON.stringify(_.pick(req, ['headers', 'body', 'params', 'query']))); | |
| console.log('--------------------'); | |
| // Process the Instagram updates here | |
| received_updates.unshift(req.body); | |
| res.sendStatus(200); | |
| }); | |
| app.listen(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "graph-api-webhooks-samples", | |
| "version": "0.1.0", | |
| "description": "Graph API Webhooks Sample", | |
| "main": "index.js", | |
| "scripts": { | |
| "start": "node heroku/index.js" | |
| }, | |
| "dependencies": { | |
| "body-parser": "~1.15.0", | |
| "dotenv": "5.0.1", | |
| "express": "~4.13.3", | |
| "express-x-hub": "^1.0.4", | |
| "lodash": "4.17.10", | |
| "request": "2.86.0" | |
| }, | |
| "engines": { | |
| "node": "8.11.1" | |
| }, | |
| "repository": { | |
| "type": "git", | |
| "url": "https://github.com/fbsamples/graph-api-webhooks-samples" | |
| }, | |
| "keywords": [ | |
| "node", | |
| "heroku", | |
| "express", | |
| "graph api" | |
| ], | |
| "license": "https://github.com/fbsamples/graph-api-webhooks-samples/blob/master/LICENSE", | |
| "bugs": "https://github.com/fbsamples/graph-api-webhooks-samples/issues" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment