-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
176 lines (155 loc) · 6.52 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
const ApiAiApp = require('actions-on-google').ApiAiApp
var moment = require('moment');
var request = require('request');
const WELCOME_INTENT = 'input.welcome'; //this is the name rom the API.AI intent. check the API.AI event console.
const LOCATION_INTENT = 'input.loc';
const DEST_INTENT = 'input.dest';
const GO_TIME_INTENT = 'input.go'
const GET_BACK_TIME_INTENT = 'input.comeback'
const GET_EMAIL = 'input.email'
const GET_PARENT_KID = 'input.people'
const NO_TRAVEL_NEEDED = 'input.doneNoArrangement'
const GET_PEOPLE_NUMBER = 'input.peopleNum'
const GET_PLANE_OR_CAR = 'input.planeCar'
exports.ricetrav = (req, res) => {
const app = new ApiAiApp({ request: req, response: res});
function welcomeIntent(app){
app.ask('Welcome to HackRice. We\'ll help you find some fun and cheap places to go. Please say your current city to continue.')
}
function locationIntent(app){
let city = app.getArgument('city')
app.data.city = city;
//127.0.0.1:5000/api/set_current_location?current_location="Houston"
var options = { method: 'GET',
url: 'http://127.0.0.1:5000/api/set_current_location',
qs: { current_location: app.data.city },
headers:
{ 'postman-token': 'c4740545-7ffb-04db-db5a-c3602afca037',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
app.ask('We got your city.' + app.data.city + '. Where would you like to go?')
});
}
function destinationIntent(app){
let destCity = app.getArgument('destCity')
app.data.destCity = destCity;
//127.0.0.1:5000/api/set_destination?destination="Austin"
var options = { method: 'GET',
url: 'http://127.0.0.1:5000/api/set_destination',
qs: { destination: app.data.destCity },
headers:
{ 'postman-token': '13f3741e-f365-c952-4c48-555dcfb1552f',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
app.ask('So you\'d like to go from ' + app.data.city + ' to ' + app.data.destCity + '. When would you like to go there?')
});
}
function leaveTime(app){
let goTime = app.getArgument('goTime');
app.data.rawGoTimeMoment = goTime;
app.data.goHour = moment(app.data.goTime).utc().hours()
app.data.goDay = moment(app.data.goTime).utc().date()
app.data.goMonth = parseInt(moment(app.data.goTime).local().month().toString()) + 1
app.data.goYear = moment(app.data.goTime).local().year()
var options = { method: 'GET',
url: 'http://127.0.0.1:5000/api/set_departure_time',
qs: { year: app.data.goYear, month: app.data.goMonth, day: app.data.goDay, hour: app.data.goHour },
headers:
{ 'postman-token': 'd5bccb80-28c6-b79e-2481-9faa3478f5dd',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
app.ask('Cool, when would you like to get back?');
});
}
function comeBack(app){
let comeBack = app.getArgument('comeBack')
app.data.rawComeBackMoment = comeBack;
app.data.comeHour = moment(app.data.comeBack).utc().hours()
app.data.comeDay = moment(app.data.comeBack).utc().date()
app.data.comeMonth = parseInt(moment(app.data.comeBack).local().month().toString()) + 1
app.data.comeYear = moment(app.data.comeBack).local().year()
var options = { method: 'GET',
url: 'http://127.0.0.1:5000/api/set_return_time',
qs: { year: app.data.comeYear, month: app.data.comeMonth, day: app.data.comeDay, hour: app.data.comeDay },
headers:
{ 'postman-token': 'aa5e1b78-1f81-add8-b00a-67eab134ad46',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
app.ask('Please type in your email.');
});
}
function getEmail(app){
let email = app.getArgument('email')
app.data.email = email
var options = { method: 'GET',
url: 'http://localhost:5000/api/set_email',
qs: { email: app.data.email },
headers:
{ 'postman-token': '9c83b51d-e437-67bc-497a-1f7bc4d2dd98',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
app.ask("Great! Do you need travel accomodations or would you like for us to arrange them?")
});
}
function needArrangement(app){
app.ask("Sounds good. How many Children (Eighteen or below) and adults will be going on this trip?")
}
function peopleNum(app){
let kids = app.getArgument('kids')
let adults = app.getArgument('adults')
app.data.kids = kids
app.data.adults = adults
var options = { method: 'GET',
url: 'http://localhost:5000/api/set_number_of_passengers',
qs: { adults: app.data.adults, children: app.data.kids },
headers:
{ 'postman-token': '91045d48-20a9-5bb3-9dcc-ef6919385e18',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
app.ask("Would you like to go by plane or car or would you like to look at both options?")
});
}
function getPlaneOrCar(app){
let choice = app.getArgument('choice')
app.data.choice = choice
var options = { method: 'GET',
url: 'http://localhost:5000/api/set_travel_option',
qs: { option: 'Plane' },
headers:
{ 'postman-token': '3807ed1f-aa45-d275-d5f2-7ee73111409c',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
var options = { method: 'GET',
url: 'http://localhost:5000/api/send_email',
headers:
{ 'postman-token': '28a5f2f4-3d64-4b02-9c8e-374d604b88a5',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
app.tell("Cool, we've got your details. Check your email for a full trip itinerary for you trip from " + app.data.city + " to " + app.data.destCity + ". Have a good time!")
});
});
}
function noArrangement(app){
var options = { method: 'GET',
url: 'http://localhost:5000/api/send_email',
headers:
{ 'postman-token': '28a5f2f4-3d64-4b02-9c8e-374d604b88a5',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
app.tell("Cool, we've got your details. Check your email for a full trip itinerary for you trip from " + app.data.city + " to " + app.data.destCity + ". Have a good time!")
});
}
let actionMap = new Map();
actionMap.set(GET_PLANE_OR_CAR, getPlaneOrCar)
actionMap.set(GET_PEOPLE_NUMBER, peopleNum)
actionMap.set(NO_TRAVEL_NEEDED, noArrangement)
actionMap.set(GET_PARENT_KID, needArrangement)
actionMap.set(GET_EMAIL, getEmail)
actionMap.set(GET_BACK_TIME_INTENT, comeBack);
actionMap.set(GO_TIME_INTENT, leaveTime)
actionMap.set(DEST_INTENT, destinationIntent)
actionMap.set(LOCATION_INTENT, locationIntent)
actionMap.set(WELCOME_INTENT, welcomeIntent)
app.handleRequest(actionMap);
}