Skip to content

Instantly share code, notes, and snippets.

@humannus

humannus/node.js Secret

Created March 11, 2021 14:55
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 humannus/f79590954cafdca42aa48f81edf4e673 to your computer and use it in GitHub Desktop.
Save humannus/f79590954cafdca42aa48f81edf4e673 to your computer and use it in GitHub Desktop.
Webhook example with bodyParser and express
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const app = express().use(bodyParser.json()); // creates http server
const token = 'YOUR_TOKEN_HERE'; // type here your verification token
app.get('/', (req, res) => {
// check if verification token is correct
if (req.query.token !== token) {
return res.sendStatus(401);
}
// return challenge
return res.end(req.query.challenge);
});
app.post('/', (req, res) => {
// check if verification token is correct
if (req.query.token !== token) {
return res.sendStatus(401);
}
// print request body
console.log(req.body);
// return responses and actions
const data = {
// return responses
responses: [
{
"type": "cards",
"elements": [
{
"title": "Event One",
"subtitle": "foo bar baz",
"imageUrl": "https://via.placeholder.com/300x220",
"buttons": [
{
"type": "postback",
"title": "foo",
"value": "bar"
},
{
"type": "postback",
"title": "baz",
"value": "qux"
}
]
},
{
"title": "Event Two",
"subtitle": "foo bar baz",
"imageUrl": "https://via.placeholder.com/300x220",
"buttons": [
{
"type": "postback",
"title": "foo",
"value": "bar"
},
{
"type": "postback",
"title": "baz",
"value": "qux"
}
]
},
{
"title": "Event Three",
"subtitle": "foo bar baz",
"imageUrl": "https://via.placeholder.com/300x220",
"buttons": [
{
"type": "postback",
"title": "foo",
"value": "bar"
},
{
"type": "postback",
"title": "baz",
"value": "qux"
}
]
}
]
}
],
// return attributes
attributes: {
attribute: 'value'
}
};
res.json(data);
});
app.listen(3000, () => console.log('[ChatBot] Webhook is listening'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment