-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
56 lines (42 loc) · 1.58 KB
/
app.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
const http = require('http')
const express = require('express')
const MessagingResponse = require('twilio').twiml.MessagingResponse
const bodyParser = require('body-parser')
const summarizer = require('./summarizer')
const weather = require('./weather')
const directions = require('./directions')
const wolfram = require('./wolfram');
const news = require('./news');
const define = require('./word-definition');
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/sms', (req, res) => {
const twiml = new MessagingResponse()
const userInput = req.body.Body.toLowerCase()
Promise.resolve().then(function (){
if (userInput.indexOf("define") >= 0) {
return define(userInput);
} else if (userInput.indexOf("summarize") >= 0 || userInput.indexOf("summary") >= 0) {
return summarizer(userInput);
} else if (userInput.indexOf("direct") >= 0 ) {
return directions(userInput);
} else if (userInput.indexOf("weather") >= 0) {
return weather(userInput);
} else if (userInput.indexOf("news") >= 0) {
return news();
} else {
return wolfram(userInput)
}
})
.then(result => ((result && Object.keys(result).length !== 0) ? result : "I'm afraid I can't do that."))
.catch(err => {
console.log(err)
return "I'm afraid I can't do that."
})
.then(result => twiml.message(result))
.then(() => res.writeHead(200, {'Content-Type': 'text/xml'}))
.then(() => res.end(twiml.toString()))
});
http.createServer(app).listen(process.env.PORT || 3000, () => {
console.log('Express server listening on port ' + (process.env.PORT || 3000))
})