-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathFindAFlight.js
124 lines (115 loc) · 4.27 KB
/
FindAFlight.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
var botId = "st-48d9b4a2-b544-5cda-8313-31fbe0e3a8b9";
var botName = "Flight Search";
var sdk = require("./lib/sdk");
var Promise = sdk.Promise;
var { makeHttpCall } = require("./makeHttpCall");
var qs = require('qs');
var findFlightServiceUrl = 'https://test.api.amadeus.com/v2/shopping/flight-offers';
var findAirportServiceUrl = 'https://test.api.amadeus.com/v1/reference-data/locations';
var getTokenUrl = 'https://test.api.amadeus.com/v1/security/oauth2/token';
var amdAccessToken;
// Make a request to get the access token
function getAccessToken(clientId, clientSecret) {
var url = getTokenUrl;
return new Promise(function(resolve, reject) {
let formData = {
client_id: clientId,
client_secret: clientSecret,
grant_type: 'client_credentials'
}
let data = qs.stringify(formData);
let headers = {
'Content-type': 'application/x-www-form-urlencoded'
}
makeHttpCall(
'post',
url,
data,
headers
)
.then(function(res){
// console.log(res)
var temp = res.data;
var token = temp['access_token'];
resolve(token);
})
.catch(function(err){
return reject(err);
})
});
}
//Make request to service app to get the flight between source and destination
function findFlights(origin, destination, departureDate, currency) {
var url = findFlightServiceUrl+'?originLocationCode='+origin+'&destinationLocationCode='+destination+'&departureDate='+departureDate+'&adults=1&max=5¤cyCode='+currency;
return new Promise(function(resolve, reject) {
let headers = {
'Authorization': 'Bearer ' + amdAccessToken
}
makeHttpCall('get', url, null, headers)
.then(function(response) {
resolve(response.data);
})
.catch(function(err) {
return reject(err);
})
});
}
//Make request to service app
async function findAirports(searchTerm) {
var url = findAirportServiceUrl+"?keyword="+searchTerm+"&subType=AIRPORT";
let headers = {
'Authorization': 'Bearer ' + amdAccessToken
};
return new Promise(function(resolve, reject) {
makeHttpCall('get', url, null, headers)
.then(function(response) {
resolve(response.data);
})
.catch(function(err) {
console.log(err);
return reject(err);
})
});
}
module.exports = {
botId : botId,
botName : botName,
on_user_message : function(requestId, data, callback) {
sdk.sendBotMessage(data, callback);
},
on_bot_message : function(requestId, data, callback) {
sdk.sendUserMessage(data, callback);
},
on_webhook : async function(requestId, data, componentName, callback)
{
var context = data.context;
var currency = context.session.BotUserSession.currency;
getAccessToken(context.session.BotUserSession.clientId, context.session.BotUserSession.clientSecret).then(function(token) {
amdAccessToken = token;
if (componentName === 'FlightsInfo') {
var origin = context.entities.Source;
var destination = context.entities.Dest;
var departureDate = context.entities.Date;
findFlights(origin,destination,departureDate,currency)
.then(function(flightResults) {
data.context.flightResults = flightResults;
callback(null, data);
});
} else if(componentName === 'GetSourceAirports'){
var searchTerm = context.entities.SourceName;
findAirports(searchTerm)
.then(function(airportResults) {
data.context.sourceAirports = airportResults;
callback(null, data);
});
} else if(componentName === 'GetDestAirports'){
var searchTerm = context.entities.DestName;
findAirports(searchTerm)
.then(function(airportResults) {
data.context.destAirports = airportResults;
callback(null, data);
});
}
});
}
};