forked from rhildred/ChatbotStart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (32 loc) · 1.14 KB
/
index.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
const express = require('express');
const bodyParser = require("body-parser");
const ShwarmaOrder = require("./assignment1Shwarma");
// Create a new express application instance
const app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("www"));
app.get("/users/:uname", (req, res) => {
res.end("Hello " + req.params.uname);
});
let oOrders = {};
app.post("/sms", (req, res) =>{
let sFrom = req.body.From || req.body.from;
if(!oOrders.hasOwnProperty(sFrom)){
oOrders[sFrom] = new ShwarmaOrder();
}
let sMessage = req.body.Body|| req.body.body;
let aReply = oOrders[sFrom].handleInput(sMessage);
if(oOrders[sFrom].isDone()){
delete oOrders[sFrom];
}
res.setHeader('content-type', 'text/xml');
let sResponse = "<Response>";
for(let n = 0; n < aReply.length; n++){
sResponse += "<Message>";
sResponse += aReply[n];
sResponse += "</Message>";
}
res.end(sResponse + "</Response>");
});
var port = process.env.PORT || parseInt(process.argv.pop()) || 3002;
app.listen(port, () => console.log('Example app listening on port ' + port + '!'));