Skip to content

Commit 9931acf

Browse files
committed
edit the script
1 parent d0fedd1 commit 9931acf

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

skill/src/conversation.js

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const handlers = {
5454
var make = this.event.request.intent.slots.make.value;
5555
var model = this.event.request.intent.slots.model.value;
5656
var year = this.event.request.intent.slots.year.value;
57-
speechOutput += " from " + fromCity + " to " + address + " , " + toCity + " on " + travelDate + ".";
57+
speechOutput += `from ${fromCity} to ${address} , ${toCity} on ${travelDate}`
5858

5959
// Calling API
6060
httpsGet_Geocode(address, (geocode) => {
@@ -65,7 +65,7 @@ const handlers = {
6565
var distancetext = matrix[1]
6666
var durationvalue = matrix[2] // int
6767
var durationtext = matrix[3]
68-
httpsGet_CarStats(make, model, year, (stats) => {
68+
httpsGetStats(make, model, year, (stats) => {
6969
var ctympg = stats[0] // int
7070
var hwympg = stats[1] // int
7171
httpsGet_CarTheft('ma', (car) => {
@@ -78,7 +78,7 @@ const handlers = {
7878
var gasPrice = get_price(lat, long)
7979
var gasCost = Math.ceil((gasPrice * distancevalue) / hwympg)
8080
if (myCar.toLowerCase() == theftCar.toLowerCase()) {
81-
speechOutput += " The total traveling distance is " + distancetext + " However, your car is on top of the car stolen list in this state. Becareful, I would recommend to find a garage to park.";
81+
speechOutput += `Based on the information that you've given, the total distance of will be ${distancetext}. For gas expense, our estimation based on the traveling distance is ${gasPrice}`
8282
var activity = isSlotValid(this.event.request, "activity");
8383
if (activity) {
8484
speechOutput += " to go " + activity;
@@ -88,7 +88,7 @@ const handlers = {
8888
this.response.speak(speechOutput);
8989
this.emit(":responseReady");
9090
}
91-
speechOutput += " The total traveling distance is " + distancetext + ", it will take around " + durationtext + " to go there. Your car ";
91+
speechOutput += `The total traveling distance is ${distancetext} , and based on the traveling distance, the estimate time that you'll arrive to your destination is ${durationtext} `;
9292
var activity = isSlotValid(this.event.request, "activity");
9393
if (activity) {
9494
speechOutput += " to go " + activity;
@@ -272,42 +272,36 @@ function httpsGet_Matrix(lat, long, callback) {
272272

273273
}
274274
// Shine Car Stats
275-
function httpsGet_CarStats(make, model, year, callback) {
276-
// Update these options with the details of the web service you would like to call
277-
var options = {
278-
host: 'apis.solarialabs.com',
279-
port: 443,
280-
path: `/shine/v1/vehicle-stats/specs?make=${make}&model=${model}&year=${year}&full-data=true&apikey=` + shine_key,
281-
method: 'GET',
275+
function httpsGetStats(make, model, year, callback){
276+
var stats_options = {
277+
host: 'apis.solarialabs.com',
278+
path: '/shine/v1/vehicle-stats/specs?make=' + make + '&model=' + model + '&year=' + year + '&full-data=true&apikey=' + shine_key,
279+
method: 'GET'
280+
}
282281

283-
// if x509 certs are required:
284-
// key: fs.readFileSync('certs/my-key.pem'),
285-
// cert: fs.readFileSync('certs/my-cert.pem')
286-
};
282+
var req = https.request(stats_options, function(res) {
283+
res.setEncoding('utf-8');
287284

288-
var req = https.request(options, res => {
289-
res.setEncoding('utf8');
290-
var returnData = "";
285+
var responseString = '';
291286

292-
res.on('data', chunk => {
293-
returnData = returnData + chunk;
294-
});
287+
res.on('data', function(data) {
288+
responseString += data;
289+
});
295290

296-
res.on('end', () => {
297-
// we have now received the raw return data in the returnData variable.
298-
// We can see it in the log output via:
299-
// console.log(JSON.stringify(returnData))
300-
// we may need to parse through it to extract the needed data
301-
var data = JSON.parse(returnData);
302-
var citygas = data[0].City_Conventional_Fuel
303-
var hwygas = data[0].Hwy_Conventional_Fuel
304-
callback([citygas, citygas]);
305-
// this will execute whatever function the caller defined, with one argument
306-
});
291+
res.on('end', function() {
292+
var response = JSON.parse(responseString);
293+
var stats_make = response[0].Make
294+
var stats_model = response[0].Model
295+
var stats_car_year = response[0].Model_Year
296+
var stats_car_mpg = response[0].City_Conventional_Fuel
297+
298+
console.log( "Model Year of the " + stats_make + " " + stats_model + " is: " + stats_car_year + ". The combined highway and city MPG is " + stats_car_mpg + ".");
299+
callback([stats_car_year, stats_car_mpg]);
300+
});
307301
});
308-
req.end();
309302

310-
}
303+
req.end();
304+
}
311305
// Shine Car Theft
312306
function httpsGet_CarTheft(state, callback) {
313307
// Update these options with the details of the web service you would like to call

0 commit comments

Comments
 (0)