Skip to content

Instantly share code, notes, and snippets.

@humannus
Created May 21, 2021 10:56
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/39a4456afa97ade927be80aa0ce43fc0 to your computer and use it in GitHub Desktop.
Save humannus/39a4456afa97ade927be80aa0ce43fc0 to your computer and use it in GitHub Desktop.
Working hours detector
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const app = express().use(bodyParser.json()); // creates http server
const token = 'TOKEN'; // 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);
// get server local time
var today = new Date();
var hours = today.getHours();
var minutes = today.getMinutes();
var afterHours;
// Check if working hours are OK
if (hours >= 8 && hours <= 16) {
if (hours == 16) {
if (minutes > 30) {
afterHours = true
} else afterHours = false
} else afterHours = false;
} else afterHours = true;
if (!afterHours) {
const data = {
// return responses
responses: [
{
"type": "text",
"message": `Working hours OK, time is ${hours}:${minutes}`
}
],
// return attributes
attributes: {
workingHours: 'yes'
}
};
res.json(data);
} else {
const data = {
// return responses
responses: [
{
"type": "text",
"message": `Working hours NOT OK, time is ${hours}:${minutes}`
}
],
// return attributes
attributes: {
workingHours: 'no'
}
};
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