Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var request = require("request")
- var hotel = require('./functions_hotel.js');
- var map = require('./functions_map.js');
- // Route the incoming request based on type (LaunchRequest, IntentRequest,
- // etc.) The JSON body of the request is provided in the event parameter.
- exports.handler = function (event, context) {
- 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 !== "") {
- // context.fail("Invalid Application ID");
- // }
- if (event.session.new) {
- onSessionStarted({requestId: event.request.requestId}, event.session);
- }
- if (event.request.type === "LaunchRequest") {
- onLaunch(event.request,
- event.session,
- function callback(sessionAttributes, speechletResponse) {
- context.succeed(buildResponse(sessionAttributes, speechletResponse));
- });
- } else if (event.request.type === "IntentRequest") {
- onIntent(event.request,
- event.session,
- function callback(sessionAttributes, speechletResponse) {
- context.succeed(buildResponse(sessionAttributes, speechletResponse));
- });
- } else if (event.request.type === "SessionEndedRequest") {
- onSessionEnded(event.request, event.session);
- context.succeed();
- }
- } catch (e) {
- context.fail("Exception: " + e);
- }
- };
- /**
- * Called when the session starts.
- */
- function onSessionStarted(sessionStartedRequest, session) {
- // add any session init logic here
- }
- /**
- * Called when the user invokes the skill without specifying what they want.
- */
- function onLaunch(launchRequest, session, callback) {
- getWelcomeResponse(callback)
- }
- /**
- * Called when the user specifies an intent for this skill.
- */
- function onIntent(intentRequest, session, callback) {
- var intent = intentRequest.intent
- var intentName = intentRequest.intent.name;
- // dispatch custom intents to handlers here
- if (intentName == "HotelInfoIntent") {
- handleHotelIntentWithCard(intent, session, "description",callback)
- } else if (intentName == "HotelSearchIntent") {
- handleHotelIntentWithCard(intent, session, "search",callback)
- } else if (intentName == "HotelPriceIntent") {
- handleHotelIntentWithCard(intent, session, "priceRange", callback)
- } else if (intentName == "HotelAddressIntent") {
- handleHotelIntentWithCard(intent, session, "address", callback)
- } else if (intentName == "HotelPhoneIntent") {
- handleHotelIntentWithCard(intent, session, "phone", callback)
- } else if (intentName == "HotelRatingIntent") {
- handleHotelIntentWithCard(intent, session, "rating", callback)
- } else if (intentName == "TrailSearchIntent") {
- handleTrailSearchIntent(intent, session,"search", callback)
- } else if (intentName == "TrailDurationIntent") {
- handleTrailIntent(intent, session,"duration", callback)
- } else if (intentName == "TrailInfoIntent") {
- handleTrailIntent(intent, session,"description", callback)
- } else if (intentName == "TrailDifficultyIntent") {
- handleTrailIntent(intent, session,"difficulty", callback)
- } else if (intentName == "ActivityIntent") {
- handleActivityIntent(intent, session, "null", callback)
- }else if (intentName == "ActivityInfoIntent") {
- handleActivityIntent(intent, session, "description", callback)
- } else if(intentName == "ImageIntent") {
- handleHotelImageIntentWithCard(intent, session, "image", callback)
- }
- else {
- throw "Invalid intent"
- }
- }
- /**
- * Called when the user ends the session.
- * Is not called when the skill returns shouldEndSession=true.
- */
- function onSessionEnded(sessionEndedRequest, session) {
- }
- // ------- Skill specific logic -------
- function getWelcomeResponse(callback) {
- var speechOutput = "Willkommen! Willst du etwas über Mayrhofen erfahren?"
- var reprompt = "Willst du etwas über Mayrhofen erfahren?"
- var shouldEndSession = false
- var sessionAttributes = {
- "speechOutput" : speechOutput,
- "repromptText" : reprompt
- }
- callback(sessionAttributes, buildSpeechletResponseWithoutCard(speechOutput, reprompt, shouldEndSession))
- }
- /* makes a respone to a hotelintent with a card */
- /* for address, phone-number and description */
- function handleHotelIntentWithCard(intent, session, type, callback) {
- var speechOutput = "We have an error";
- const itemSlot = intent.slots.Hotel;
- let itemName;
- if (itemSlot && itemSlot.value) {
- itemName = itemSlot.value.toLowerCase();
- }
- if (type=="search"){
- itemName="alpenjuwel";
- }
- var type = type;//description, priceRange, ....
- if(itemName){
- hotel.data.hotelInformation(function(title, content, data) {
- if(data != "ERROR") {
- var title = title;
- var content = content;
- var speechOutput = data
- } else {
- var speechOutput = "Fehler bei der Informationsbeschaffung";
- }
- callback(session.attributes, buildSpeechletResponse(title, content, speechOutput, speechOutput, false));
- }, itemName, type);
- }
- else {
- //no card when response doesn't return an answer
- callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, speechOutput, false));
- }
- }
- /* makes a respone to a imageIntent with a card */
- function handleHotelImageIntentWithCard(intent, session, type, callback) {
- var speechOutput = "We have an error";
- const itemSlot = intent.slots.Hotel;
- let itemName;
- if (itemSlot && itemSlot.value) {
- itemName = itemSlot.value.toLowerCase();
- }
- var type = type;//description, priceRange, ....
- if(itemName){
- hotel.data.hotelInformation(function(title, content, imageUrl, data) {
- if(data != "ERROR") {
- var title = title;
- var content = content;
- var speechOutput = data;
- var imageUrl = imageUrl;
- } else {
- var speechOutput = "Fehler bei der Informationsbeschaffung";
- }
- if(title != "Fehler") {
- callback(session.attributes, speechletImage(title, content, imageUrl,"Schau in deine Alexa App", speechOutput, false));
- } else {
- callback(session.attributes, buildSpeechletResponseWithoutCard("Konnte keine Bilder finden", "Kein Repromt vorhanden", false));
- }
- }, itemName, type);
- }
- else {
- //no card when response doesn't return an awnser
- callback(session.attributes, buildSpeechletResponseWithoutCard("Konnte keine Bilder finden", "Kein Repromt vorhanden", false));
- }
- }
- function handleTrailIntent(intent, session, type, callback) {
- var speechOutput = "noch nicht implementiert"
- const itemSlot = intent.slots.Trail;
- let itemName;
- if (itemSlot && itemSlot.value) {
- itemName = itemSlot.value.toLowerCase();
- }
- if(itemName){
- map.data.getTrail(function(data) {
- if(data != "ERROR") {
- var speechOutput = data
- } else {
- var speechOutput = "ERROR123";
- }
- callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "Kein Repromt vorhanden", false));
- }, type, itemName, false);
- }
- else{
- callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "Kein Repromt vorhanden", false));
- }
- }
- function handleTrailSearchIntent(intent, session, type, callback) {
- var speechOutput = "Ein Fehler ist aufgetreten"
- const itemSlot = intent.slots.Trail;
- if(type=="search"){
- map.data.getTrail(function(data) {
- if(data != "ERROR") {
- var speechOutput = data
- } else {
- var speechOutput = "ERROR123";
- }
- callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "Kein Repromt vorhanden", false));
- }, "asdf", "asdf", true);
- }
- else{
- callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "Kein Repromt vorhanden", false));
- }
- }
- function handleActivityIntent(intent, session, type, callback) {
- var speechOutput = "We have an error";
- const itemSlot = intent.slots.Activity;
- let itemName;
- if (itemSlot && itemSlot.value) {
- itemName = itemSlot.value.toLowerCase();
- }
- var type = type;//description,..
- }
- // ------- Helper functions to build responses for Alexa -------
- function speechletImage(title, content, imageUrl, output, repromptText, shouldEndSession) {
- return {
- outputSpeech: {
- type: "PlainText",
- text: output
- },
- card: {
- type: "Standard",
- title: title,
- text: content,
- image: {
- "largeImageUrl": imageUrl
- }
- },
- reprompt: {
- outputSpeech: {
- type: "PlainText",
- text: repromptText
- }
- },
- shouldEndSession: shouldEndSession
- };
- }
- function buildSpeechletResponse(title, content, output, repromptText, shouldEndSession) {
- return {
- outputSpeech: {
- type: "PlainText",
- text: output
- },
- card: {
- type: "Simple",
- title: title,
- content: content
- },
- reprompt: {
- outputSpeech: {
- type: "PlainText",
- text: repromptText
- }
- },
- shouldEndSession: shouldEndSession
- };
- }
- function buildSpeechletResponseWithoutCard(output, repromptText, shouldEndSession) {
- return {
- outputSpeech: {
- type: "PlainText",
- text: output
- },
- reprompt: {
- outputSpeech: {
- type: "PlainText",
- text: repromptText
- }
- },
- shouldEndSession: shouldEndSession
- };
- }
- function buildResponse(sessionAttributes, speechletResponse) {
- return {
- version: "1.0",
- sessionAttributes: sessionAttributes,
- response: speechletResponse
- };
- }
- function capitalizeFirst(s) {
- return s.charAt(0).toUpperCase() + s.slice(1)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement