Created
July 6, 2018 15:50
-
-
Save josephktcheung/a44ae3d5b7d1169c3f27230d8a045fe1 to your computer and use it in GitHub Desktop.
aws lambda with x-hub
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
import * as awsServerlessExpress from 'aws-serverless-express'; | |
import * as express from 'express'; | |
import * as xhub from 'express-x-hub'; | |
import * as bodyParser from 'body-parser'; | |
import * as AWS from 'aws-sdk'; | |
import { customDomainReroute } from '@turinggroup/serverless-express-custom-domain-middleware'; | |
const sns = new AWS.SNS(); | |
const app = express(); | |
app.use( | |
xhub({ | |
algorithm: 'sha1', | |
secret: process.env.FB_APP_SECRET, | |
}), | |
); | |
app.use(bodyParser.json()); | |
app.use(customDomainReroute); | |
const token = process.env.XHUB_TOKEN; | |
app.get('/webhook', (req, res) => { | |
if ( | |
req.params['hub.mode'] === 'subscribe' && | |
req.params['hub.verify_token'] === token | |
) { | |
res.send(req.params['hub.challenge']); | |
} else { | |
res.status(400).end(); | |
} | |
}); | |
app.post('/webhook', async (req, res) => { | |
if (!req['isXHubValid']()) { | |
res.status(401).end(); | |
return; | |
} | |
console.log('webhook body', JSON.stringify(req.body, null, 2)); | |
res.status(200).end(); | |
}); | |
const server = awsServerlessExpress.createServer(app); | |
export const handler = (event, context, callback) => { | |
return awsServerlessExpress.proxy(server, event, context); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment