Skip to content

Instantly share code, notes, and snippets.

@dzabrzenski
Last active May 4, 2018 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzabrzenski/db6081574d1108233b19728a881d0e54 to your computer and use it in GitHub Desktop.
Save dzabrzenski/db6081574d1108233b19728a881d0e54 to your computer and use it in GitHub Desktop.
'use strict';
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const token = 'secret'; // replace to process.env.TOKEN for non development
const server = http
.createServer((req, res) => {
const parsed = url.parse(req.url);
req.url = parsed.pathname;
req.query = querystring.parse(parsed.query);
// check token in every request
if (req.query.token !== token) {
res.writeHead(401);
return res.end();
}
// verification request
if (req.method === 'GET') {
return res.end(req.query.challenge);
}
req.rawBody = '';
req.on('data', chunk => {
req.rawBody += chunk;
});
req.on('end', () => {
if (req.rawBody) {
req.body = JSON.parse(req.rawBody);
}
handler(req, res);
});
});
function handler(req, res) {
// it will contain query response
console.log(req.body);
const data = {
parameters: {
name: 'John',
surname: 'Example',
age: '30'
},
responses: [
{
type: 'text',
elements: ['Hi {{name}} {{surname}}']
}
]
};
res.end(JSON.stringify(data));
}
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment