@@ -42,6 +42,8 @@ npm install fb-messenger-bot-api
42
42
* [ Page Image Posts] ( #page-image-posts )
43
43
* [ Page Link Posts] ( #page-link-posts )
44
44
* [ Validating Facebook Webhook] ( #validating-facebook-webhook )
45
+ * [ Server Validation] ( #server-validation )
46
+ * [ Lambda Validation] ( #lambda-validation )
45
47
* [ Complete Examples] ( #complete-example )
46
48
* [ Creating Facebook App] ( #creating-facebook-app )
47
49
@@ -278,37 +280,52 @@ pageClient.postUrl(`<URL>`).postMessage(`<MESSAGE>`).sendPost(`<CALLBACK>`);
278
280
` <CALLBACK> ` is optional callback otherwise promise is returned
279
281
280
282
## Validating Facebook Webhook
283
+ ### Server Validation
281
284
``` javascript
282
285
const facebook = require (' fb-messenger-bot-api' );
283
286
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) );
285
288
```
286
289
Example based on usage with Express Router, can use any other middleware which passes in the req and response objects.
287
290
Assumes verification token set under ` process.env.FB_VERIFICATION_TOKEN ` .
288
291
289
292
Alternatively, if you want to pass in your set token in a different manner or under different name you can use
290
293
``` javascript
291
- ValidateWebhook .validateWithToken (req, res, < TOKEN > );
294
+ ValidateWebhook .validateServer (req, res, < TOKEN > );
292
295
```
293
296
294
297
This allows you to obtain the value as you wish and still use it as above with the help of currying.
295
298
``` javascript
296
299
...
297
300
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);
299
302
}
300
303
const validator = validateWebhook (< TOKEN > );
301
304
router .get (' /api/webhook/' ,validator);
302
305
```
303
306
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.
304
321
## Complete example
305
322
``` javascript
306
323
const router = require (' express' ).Router ();
307
324
const facebook = require (' fb-messenger-bot-api' );
308
325
const messagingClient = new facebook.FacebookMessagingAPIClient (process .env .PAGE_ACCESS_TOKEN );
309
326
const messageParser = facebook .FacebookMessageParser ;
310
327
...
311
- router .get (' /api/webhook' ,facebook .ValidateWebhook .validate );
328
+ router .get (' /api/webhook' ,facebook .ValidateWebhook .validateServer );
312
329
router .post (' /api/webhook' , (req , res ) => {
313
330
const incomingMessages = messageParser .parsePayload (req .body );
314
331
...
332
349
import {FacebookMessagingAPIClient , ValidateWebhook , FacebookMessageParser } from ' fb-messenger-bot-api' ;
333
350
import {Router } from ' express' ;
334
351
...
335
- router .get (' /api/webhook' ,facebook .ValidateWebhook .validate );
352
+ router .get (' /api/webhook' ,facebook .ValidateWebhook .validateServer );
336
353
router .post (' /api/webhook' , (req , res ) => {
337
354
try {
338
355
const incomingMessages = messageParser .parsePayload (req .body );
0 commit comments