Skip to content

Commit dfbdada

Browse files
author
Cristian Boarna
committed
feat(ValidateWebhook): 1. Added AWS Lambda webhook validation 2. Renamed validate and validateWithTo
BREAKING CHANGE: Changed ValidateWebhook signatures
1 parent a8b3db8 commit dfbdada

14 files changed

+2008
-1858
lines changed

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ npm install fb-messenger-bot-api
4242
* [Page Image Posts](#page-image-posts)
4343
* [Page Link Posts](#page-link-posts)
4444
* [Validating Facebook Webhook](#validating-facebook-webhook)
45+
* [Server Validation](#server-validation)
46+
* [Lambda Validation](#lambda-validation)
4547
* [Complete Examples](#complete-example)
4648
* [Creating Facebook App](#creating-facebook-app)
4749

@@ -278,37 +280,52 @@ pageClient.postUrl(`<URL>`).postMessage(`<MESSAGE>`).sendPost(`<CALLBACK>`);
278280
`<CALLBACK>` is optional callback otherwise promise is returned
279281

280282
## Validating Facebook Webhook
283+
### Server Validation
281284
```javascript
282285
const facebook = require('fb-messenger-bot-api');
283286
const router = require('express').Router();
284-
router.get('/api/webhook',facebook.ValidateWebhook.validate);
287+
router.get('/api/webhook',(req, res) => facebook.ValidateWebhook.validateServer(req,res));
285288
```
286289
Example based on usage with Express Router, can use any other middleware which passes in the req and response objects.
287290
Assumes verification token set under `process.env.FB_VERIFICATION_TOKEN`.
288291

289292
Alternatively, if you want to pass in your set token in a different manner or under different name you can use
290293
```javascript
291-
ValidateWebhook.validateWithToken(req, res, <TOKEN>);
294+
ValidateWebhook.validateServer(req, res, <TOKEN>);
292295
```
293296

294297
This allows you to obtain the value as you wish and still use it as above with the help of currying.
295298
```javascript
296299
...
297300
const validateWebhook = function validateWebhook(token) {
298-
return (req, res) => facebook.ValidateWebhook.validateWithToken(req, res, token);
301+
return (req, res) => facebook.ValidateWebhook.validateServer(req, res, token);
299302
}
300303
const validator = validateWebhook(<TOKEN>);
301304
router.get('/api/webhook/',validator);
302305
```
303306

307+
### Lambda Validation
308+
Alternatively, you can use this when running on AWS Lambda to take advantage of the serverless paradigm as follows:
309+
310+
```typescript
311+
import {ValidateWebhook} from 'fb-messenger-bot-api';
312+
const handler = (event, context, callback: Function) => {
313+
...
314+
if(event.httpMethod === 'GET') {
315+
ValidateWebhook.validateLambda(event, callback);
316+
}
317+
...
318+
}
319+
```
320+
Both `validateLambda` and `validateServer` support passing in verification token as third parameter. Otherwise will check `process.env.FB_VERIFICATION_TOKEN` for value.
304321
## Complete example
305322
```javascript
306323
const router = require('express').Router();
307324
const facebook = require('fb-messenger-bot-api');
308325
const messagingClient = new facebook.FacebookMessagingAPIClient(process.env.PAGE_ACCESS_TOKEN);
309326
const messageParser = facebook.FacebookMessageParser;
310327
...
311-
router.get('/api/webhook',facebook.ValidateWebhook.validate);
328+
router.get('/api/webhook',facebook.ValidateWebhook.validateServer);
312329
router.post('/api/webhook', (req, res) => {
313330
const incomingMessages = messageParser.parsePayload(req.body);
314331
...
@@ -332,7 +349,7 @@ or
332349
import {FacebookMessagingAPIClient, ValidateWebhook, FacebookMessageParser} from 'fb-messenger-bot-api';
333350
import {Router} from 'express';
334351
...
335-
router.get('/api/webhook',facebook.ValidateWebhook.validate);
352+
router.get('/api/webhook',facebook.ValidateWebhook.validateServer);
336353
router.post('/api/webhook', (req, res) => {
337354
try {
338355
const incomingMessages = messageParser.parsePayload(req.body);

0 commit comments

Comments
 (0)