This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
84 lines (72 loc) · 2.18 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require('dotenv').config();
const path = require('path');
const morgan = require('morgan');
const express = require('express');
const favicon = require('serve-favicon');
const bodyParser = require('body-parser');
// eslint-disable-next-line import/no-unresolved
const csml = require('../native');
const mod = require('../package.json');
const app = express();
app.use(morgan('tiny'));
app.disable('x-powered-by');
app.use(bodyParser.json({ limit: '50mb', extended: true }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false }));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
/**
* Default route
*/
app.get('/', (req, res) => res.send(`Running CSML Server v${mod.version}`));
/**
* Receive and handle incoming events
*/
app.post('/run', (req, res) => {
const { bot, event } = req.body;
if (event.payload && event.payload.content && event.payload.content.close_flows === true) {
csml.closeAllConversations(event.client);
}
const data = csml.run(event, bot);
return res.json(data);
});
/**
* Validate if a bot is valid CSML
*/
app.post('/bots/validate', (req, res) => {
try {
const data = csml.validateBot(req.body);
return res.json(data);
}
catch (err) {
return res.json({ valid: false, message: err });
}
});
/**
* Retrieve all the steps present in each flow of a given bot
*/
app.post('/get_bot_steps', (req, res) => {
const data = csml.getBotSteps(req.body);
return res.json(data);
});
/**
* Find out if the given client has an open conversation
*/
app.post('/conversations/open', (req, res) => {
const data = csml.getOpenConversation(req.body);
if (!data) return res.send();
return res.json(data);
});
/**
* Close all open conversations of a given client
*/
app.post('/conversations/close', (req, res) => {
const success = csml.closeAllConversations(req.body);
return res.json({ success });
});
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, _next) => {
console.error('CSML Engine Error:', err);
if (res.headersSent) return;
return res.status(err.status || err.statusCode || 500).send(err);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Listening on port ${PORT}`));