'use strict';
var https = require("https");
/**
* This sample demonstrates a simple skill built with the Amazon Alexa Skills Kit.
* The Intent Schema, Custom Slots, and Sample Utterances for this skill, as well as
* testing instructions are located at http://amzn.to/1LzFrj6
*
* For additional samples, visit the Alexa Skills Kit Getting Started guide at
* http://amzn.to/1LGWsLG
*/
// --------------- Helpers that build all of the responses -----------------------
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
return {
outputSpeech: {
type: 'PlainText',
text: output,
},
card: {
type: 'Simple',
title: 'SessionSpeechlet - ${title}',
content: 'SessionSpeechlet - ${output}',
},
reprompt: {
outputSpeech: {
type: 'PlainText',
text: repromptText,
},
},
shouldEndSession,
};
}
function buildResponse(sessionAttributes, speechletResponse) {
return {
version: '1.0',
sessionAttributes,
response: speechletResponse,
};
}
/**
* called when session ended
*/
function handleSessionEndRequest(callback) {
const cardTitle = 'Session Ended';
const speechOutput = 'Thank you for using cheap flight search. Have a nice day!';
// Setting this to true ends the session and exits the skill.
const shouldEndSession = true;
callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}
/**
* function to get city codes .
*/
function getCityCodes(intent, session, callback){
var sessionAttributes = {};
let speechOutput="";
let repromptText="";
const cardTitle = intent.name;
const shouldEndSession = false;
console.log(session.attributes.originCityCode);
if(session.attributes.originCityCode!==undefined){
sessionAttributes={"originCityCode":session.attributes.originCityCode,"destinationCityCode":intent.slots.city_codes.value};
speechOutput = 'on which date you want to travel';
repromptText = "please tell me the travel date";
}
else{
sessionAttributes={"originCityCode":intent.slots.city_codes.value};
speechOutput = 'tell me destination city code';
repromptText = "tell me destination city code";
}
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
/**
* getting flight details by providing parameter like source, destination and departure date.
*/
function searchFlight(intent, session, callback) {
const cardTitle = intent.name;
const source =session.attributes.originCityCode;
const dest = session.attributes.destinationCityCode;
console.log(source+" "+dest);
const travelDate = intent.slots.date.value;
console.log(travelDate);
let repromptText = '';
let sessionAttributes = {};
const shouldEndSession = false;
let speechOutput = '';
var body="";
var endpoint="https://www.googleapis.com/qpxExpress/v1/trips/search?key=AIzaSyApFEhzPRXirKSY1ST5IMxP-_HjiBKiypY";
var jsonObject = JSON.stringify({
"request": {
"slice": [
{
"origin": source,
"destination": dest,
"date": travelDate
}
],
"passengers": {
"adultCount": 1,
"infantInLapCount": 0,
"infantInSeatCount": 0,
"childCount": 0,
"seniorCount": 0
},
"solutions": 1,
"refundable": false
}
});
var optionspost = {
host : 'www.googleapis.com',
path : '/qpxExpress/v1/trips/search?key=AIzaSyApFEhzPRXirKSY1ST5IMxP-_HjiBKiypY',
method : 'POST',
headers: {
'Content-Type': 'application/json',
},
};
var reqPost = https.request(optionspost, function(res) {
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function() {
var data = JSON.parse(body);
console.log(data);
var data1=data.trips.tripOption[0];
var from=data.trips.data.city[0].name;
var to=data.trips.data.city[1].name;
var slice=data1.slice[0];
var segment=slice.segment[0];
var carrier=segment.flight.carrier;
var number=segment.flight.number;
var leg=segment.leg[0];
var depart=leg.departureTime;
var arrival=leg.arrivalTime;
var departArray=depart.split("T");
var departDate=departArray[0];
var departTimeArray=departArray[1].split("+");
var departTime=departTimeArray[0];
var arrivalArray=arrival.split("T");
var arrivalDate=arrivalArray[0];
var arrivalTimeArray=arrivalArray[1].split("+");
var arrivalTime=arrivalTimeArray[0];
var flightNumber=""+carrier+number;
speechOutput="hi, your flight number, "+flightNumber+", will depart from, "+from+", on, "+departDate+", at "+departTime+", and, it will arrive, "+to+", on, "+arrivalDate+", at, "+arrivalTime+", .The total fare of your trip will be, "+data1.saleTotal+", anything else that i can help you with.";
repromptText=speechOutput;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
});
reqPost.write(jsonObject);
reqPost.end();
reqPost.on('error', (e) => {
console.error(e);
});
}
/**
* Welcome message when skill launched
*/
function getWelcomeResponse(callback) {
// If we wanted to initialize the session to have some attributes we could add those here.
const sessionAttributes = {};
const cardTitle = 'Welcome';
const speechOutput = 'Hi, welcome to flight search, please tell me origin city code';
// If the user either does not reply to the welcome message or says something that is not
// understood, they will be prompted again with this text.
const repromptText = "tell me origin city code";
const shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
/**
* Called when the user launches the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, callback) {
console.log(`onLaunch requestId=${launchRequest.requestId}, sessionId=${session.sessionId}`);
// Dispatch to your skill's launch.
getWelcomeResponse(callback);
}
/**
* Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback) {
console.log(`onIntent requestId=${intentRequest.requestId}, sessionId=${session.sessionId}`);
const intent = intentRequest.intent;
const intentName = intentRequest.intent.name;
// Dispatch to your skill's intent handlers
if (intentName === 'CityCodes') {
getCityCodes(intent, session, callback);
}else if (intentName === 'TravelDate') {
searchFlight(intent, session, callback);
}
else if (intentName === 'AMAZON.HelpIntent') {
getWelcomeResponse(callback);
} else if (intentName === 'AMAZON.StopIntent' || intentName === 'AMAZON.CancelIntent') {
handleSessionEndRequest(callback);
} else {
throw new Error('Invalid intent');
}
}
/**
* Called when the session starts.
*/
function onSessionStarted(sessionStartedRequest, session) {
console.log(`onSessionStarted requestId=${sessionStartedRequest.requestId}, sessionId=${session.sessionId}`);
}
/**
* Called when the user ends the session.
* Is not called when the skill returns shouldEndSession=true.
*/
function onSessionEnded(sessionEndedRequest, session) {
console.log(`onSessionEnded requestId=${sessionEndedRequest.requestId}, sessionId=${session.sessionId}`);
// Add cleanup logic here
}
exports.handler = (event, context, callback) => {
try {
console.log(`event.session.application.applicationId=${event.session.application.applicationId}`);
/**
* Uncomment this if statement and populate with your skill's application ID to
* prevent someone else from configuring a skill that sends requests to this function.
*/
if (event.session.application.applicationId !== 'amzn1.ask.skill.1f2e8dab-fe0f-474d-a775-e2750791ebb0') {
callback('Invalid Application ID');
}
if (event.session.new) {
onSessionStarted({ requestId: event.request.requestId }, event.session);
}
if (event.request.type === 'LaunchRequest') {
onLaunch(event.request,
event.session,
(sessionAttributes, speechletResponse) => {
callback(null, buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === 'IntentRequest') {
onIntent(event.request,
event.session,
(sessionAttributes, speechletResponse) => {
callback(null, buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === 'SessionEndedRequest') {
onSessionEnded(event.request, event.session);
callback();
}
} catch (err) {
callback(err);
}
};
// --------------- Functions that control the skill's behavior -----------------------
Comments