This repository has been archived by the owner on Jul 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
95 lines (78 loc) · 2.73 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
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
85
86
87
88
89
90
91
92
93
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const fs = require('fs');
const entitydetector = require('./helpers/entitydetection')
const skyscanner = require('./helpers/skyscanner')
const twist = require('./helpers/twist');
const main = require('./helpers/main')
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.get('/', function (req, res) {
console.log('Hello World');
res.send("Hello, world!");
})
//
// TWIST ROUTES
//
app.post('/command', function (req, res) {
var command = req.body.command_argument;
var thread_id = Number(req.body.thread_id);
twist.getCommentById(req.body.comment_id, (comment)=>{
comment = comment.comment;
if(comment.attachments && comment.attachments.length > 0){
twist.storeAttachments(comment.attachments);
}
});
if(command){
console.log(command);
if(command.length > 0) {
if(command.toLowerCase().includes("show me")){
twist.showTravelDocuments(res);
} else if(command == 'upload'){
return;
} else {
entitydetector.getCity(command, (cities) => {
if (cities && cities.length > 0) {
main.sm.flightToDest(cities[0])
//res.sendStatus(200)
} else {
twist.postComment(thread_id, 'Without destination I\'m afraid I cannot help you. :(');
res.sendStatus(400)
console.log("No destination specified!")
}
})
}
}
}else{
twist.postComment(thread_id, 'Don\'t hesitate to ask me something!');
console.log('No command recognized!');
}
})
app.post('/commentadd', function(req, res){
var comment = req.body;
if(comment.thread_id && comment.thread_id == twist.thread.thread_id && twist.thread.awaitingResponse && comment.creator != 57018){
twist.processResponse(comment.content);
}
res.send(200);
})
//
// Skyscanner routes
//
app.get('/flight/:destination/:dateStart', function(req, res) {
const params = req.params
skyscanner.getFlightSuggestion([params.dateStart], params.destination, (result) => {
res.json(result)
})
})
app.get('/flight/:destination/:dateStart/:dateEnd', function (req, res) {
const params = req.params
skyscanner.getFlightSuggestion([params.dateStart, params.dateEnd], params.destination, (result) => {
res.json(result)
})
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})