Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.40 KB | None | 0 0
  1.  
  2. var request = require("request")
  3.  
  4.  
  5. var hotel = require('./functions_hotel.js');
  6. var map = require('./functions_map.js');
  7.  
  8.  
  9.  
  10. // Route the incoming request based on type (LaunchRequest, IntentRequest,
  11. // etc.) The JSON body of the request is provided in the event parameter.
  12. exports.handler = function (event, context) {
  13. try {
  14. console.log("event.session.application.applicationId=" + event.session.application.applicationId);
  15.  
  16. /**
  17. * Uncomment this if statement and populate with your skill's application ID to
  18. * prevent someone else from configuring a skill that sends requests to this function.
  19. */
  20.  
  21. // if (event.session.application.applicationId !== "") {
  22. // context.fail("Invalid Application ID");
  23. // }
  24.  
  25. if (event.session.new) {
  26. onSessionStarted({requestId: event.request.requestId}, event.session);
  27. }
  28.  
  29. if (event.request.type === "LaunchRequest") {
  30. onLaunch(event.request,
  31. event.session,
  32. function callback(sessionAttributes, speechletResponse) {
  33. context.succeed(buildResponse(sessionAttributes, speechletResponse));
  34. });
  35. } else if (event.request.type === "IntentRequest") {
  36. onIntent(event.request,
  37. event.session,
  38. function callback(sessionAttributes, speechletResponse) {
  39. context.succeed(buildResponse(sessionAttributes, speechletResponse));
  40. });
  41. } else if (event.request.type === "SessionEndedRequest") {
  42. onSessionEnded(event.request, event.session);
  43. context.succeed();
  44. }
  45. } catch (e) {
  46. context.fail("Exception: " + e);
  47. }
  48. };
  49.  
  50. /**
  51. * Called when the session starts.
  52. */
  53. function onSessionStarted(sessionStartedRequest, session) {
  54. // add any session init logic here
  55. }
  56.  
  57. /**
  58. * Called when the user invokes the skill without specifying what they want.
  59. */
  60. function onLaunch(launchRequest, session, callback) {
  61. getWelcomeResponse(callback)
  62. }
  63.  
  64. /**
  65. * Called when the user specifies an intent for this skill.
  66. */
  67. function onIntent(intentRequest, session, callback) {
  68.  
  69. var intent = intentRequest.intent
  70. var intentName = intentRequest.intent.name;
  71.  
  72. // dispatch custom intents to handlers here
  73. if (intentName == "HotelInfoIntent") {
  74. handleHotelIntentWithCard(intent, session, "description",callback)
  75. } else if (intentName == "HotelSearchIntent") {
  76. handleHotelIntentWithCard(intent, session, "search",callback)
  77. } else if (intentName == "HotelPriceIntent") {
  78. handleHotelIntentWithCard(intent, session, "priceRange", callback)
  79. } else if (intentName == "HotelAddressIntent") {
  80. handleHotelIntentWithCard(intent, session, "address", callback)
  81. } else if (intentName == "HotelPhoneIntent") {
  82. handleHotelIntentWithCard(intent, session, "phone", callback)
  83. } else if (intentName == "HotelRatingIntent") {
  84. handleHotelIntentWithCard(intent, session, "rating", callback)
  85. } else if (intentName == "TrailSearchIntent") {
  86. handleTrailSearchIntent(intent, session,"search", callback)
  87. } else if (intentName == "TrailDurationIntent") {
  88. handleTrailIntent(intent, session,"duration", callback)
  89. } else if (intentName == "TrailInfoIntent") {
  90. handleTrailIntent(intent, session,"description", callback)
  91. } else if (intentName == "TrailDifficultyIntent") {
  92. handleTrailIntent(intent, session,"difficulty", callback)
  93. } else if (intentName == "ActivityIntent") {
  94. handleActivityIntent(intent, session, "null", callback)
  95. }else if (intentName == "ActivityInfoIntent") {
  96. handleActivityIntent(intent, session, "description", callback)
  97. } else if(intentName == "ImageIntent") {
  98. handleHotelImageIntentWithCard(intent, session, "image", callback)
  99. }
  100. else {
  101. throw "Invalid intent"
  102. }
  103. }
  104.  
  105. /**
  106. * Called when the user ends the session.
  107. * Is not called when the skill returns shouldEndSession=true.
  108. */
  109. function onSessionEnded(sessionEndedRequest, session) {
  110.  
  111. }
  112.  
  113. // ------- Skill specific logic -------
  114.  
  115. function getWelcomeResponse(callback) {
  116. var speechOutput = "Willkommen! Willst du etwas über Mayrhofen erfahren?"
  117.  
  118. var reprompt = "Willst du etwas über Mayrhofen erfahren?"
  119.  
  120. var shouldEndSession = false
  121.  
  122. var sessionAttributes = {
  123. "speechOutput" : speechOutput,
  124. "repromptText" : reprompt
  125. }
  126.  
  127. callback(sessionAttributes, buildSpeechletResponseWithoutCard(speechOutput, reprompt, shouldEndSession))
  128.  
  129. }
  130.  
  131. /* makes a respone to a hotelintent with a card */
  132. /* for address, phone-number and description */
  133. function handleHotelIntentWithCard(intent, session, type, callback) {
  134. var speechOutput = "We have an error";
  135.  
  136. const itemSlot = intent.slots.Hotel;
  137.  
  138. let itemName;
  139.  
  140. if (itemSlot && itemSlot.value) {
  141. itemName = itemSlot.value.toLowerCase();
  142. }
  143.  
  144. if (type=="search"){
  145. itemName="alpenjuwel";
  146. }
  147.  
  148. var type = type;//description, priceRange, ....
  149.  
  150.  
  151. if(itemName){
  152. hotel.data.hotelInformation(function(title, content, data) {
  153. if(data != "ERROR") {
  154. var title = title;
  155. var content = content;
  156. var speechOutput = data
  157. } else {
  158. var speechOutput = "Fehler bei der Informationsbeschaffung";
  159. }
  160. callback(session.attributes, buildSpeechletResponse(title, content, speechOutput, speechOutput, false));
  161. }, itemName, type);
  162. }
  163. else {
  164. //no card when response doesn't return an answer
  165. callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, speechOutput, false));
  166. }
  167. }
  168.  
  169. /* makes a respone to a imageIntent with a card */
  170. function handleHotelImageIntentWithCard(intent, session, type, callback) {
  171. var speechOutput = "We have an error";
  172.  
  173. const itemSlot = intent.slots.Hotel;
  174.  
  175. let itemName;
  176.  
  177. if (itemSlot && itemSlot.value) {
  178. itemName = itemSlot.value.toLowerCase();
  179. }
  180.  
  181. var type = type;//description, priceRange, ....
  182.  
  183. if(itemName){
  184. hotel.data.hotelInformation(function(title, content, imageUrl, data) {
  185.  
  186. if(data != "ERROR") {
  187. var title = title;
  188. var content = content;
  189. var speechOutput = data;
  190. var imageUrl = imageUrl;
  191. } else {
  192. var speechOutput = "Fehler bei der Informationsbeschaffung";
  193. }
  194. if(title != "Fehler") {
  195. callback(session.attributes, speechletImage(title, content, imageUrl,"Schau in deine Alexa App", speechOutput, false));
  196. } else {
  197. callback(session.attributes, buildSpeechletResponseWithoutCard("Konnte keine Bilder finden", "Kein Repromt vorhanden", false));
  198. }
  199.  
  200. }, itemName, type);
  201. }
  202. else {
  203. //no card when response doesn't return an awnser
  204. callback(session.attributes, buildSpeechletResponseWithoutCard("Konnte keine Bilder finden", "Kein Repromt vorhanden", false));
  205. }
  206. }
  207.  
  208.  
  209. function handleTrailIntent(intent, session, type, callback) {
  210. var speechOutput = "noch nicht implementiert"
  211.  
  212. const itemSlot = intent.slots.Trail;
  213. let itemName;
  214.  
  215. if (itemSlot && itemSlot.value) {
  216. itemName = itemSlot.value.toLowerCase();
  217. }
  218.  
  219. if(itemName){
  220. map.data.getTrail(function(data) {
  221. if(data != "ERROR") {
  222. var speechOutput = data
  223. } else {
  224. var speechOutput = "ERROR123";
  225. }
  226. callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "Kein Repromt vorhanden", false));
  227. }, type, itemName, false);
  228. }
  229. else{
  230. callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "Kein Repromt vorhanden", false));
  231. }
  232.  
  233.  
  234. }
  235.  
  236. function handleTrailSearchIntent(intent, session, type, callback) {
  237. var speechOutput = "Ein Fehler ist aufgetreten"
  238.  
  239. const itemSlot = intent.slots.Trail;
  240.  
  241. if(type=="search"){
  242. map.data.getTrail(function(data) {
  243. if(data != "ERROR") {
  244. var speechOutput = data
  245. } else {
  246. var speechOutput = "ERROR123";
  247. }
  248. callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "Kein Repromt vorhanden", false));
  249. }, "asdf", "asdf", true);
  250. }
  251. else{
  252. callback(session.attributes, buildSpeechletResponseWithoutCard(speechOutput, "Kein Repromt vorhanden", false));
  253. }
  254.  
  255.  
  256. }
  257.  
  258. function handleActivityIntent(intent, session, type, callback) {
  259. var speechOutput = "We have an error";
  260.  
  261. const itemSlot = intent.slots.Activity;
  262.  
  263. let itemName;
  264. if (itemSlot && itemSlot.value) {
  265. itemName = itemSlot.value.toLowerCase();
  266. }
  267.  
  268. var type = type;//description,..
  269.  
  270.  
  271.  
  272. }
  273.  
  274.  
  275. // ------- Helper functions to build responses for Alexa -------
  276. function speechletImage(title, content, imageUrl, output, repromptText, shouldEndSession) {
  277. return {
  278. outputSpeech: {
  279. type: "PlainText",
  280. text: output
  281. },
  282. card: {
  283. type: "Standard",
  284. title: title,
  285. text: content,
  286. image: {
  287. "largeImageUrl": imageUrl
  288. }
  289. },
  290. reprompt: {
  291. outputSpeech: {
  292. type: "PlainText",
  293. text: repromptText
  294. }
  295. },
  296. shouldEndSession: shouldEndSession
  297. };
  298. }
  299.  
  300. function buildSpeechletResponse(title, content, output, repromptText, shouldEndSession) {
  301. return {
  302. outputSpeech: {
  303. type: "PlainText",
  304. text: output
  305. },
  306. card: {
  307. type: "Simple",
  308. title: title,
  309. content: content
  310. },
  311. reprompt: {
  312. outputSpeech: {
  313. type: "PlainText",
  314. text: repromptText
  315. }
  316. },
  317. shouldEndSession: shouldEndSession
  318. };
  319. }
  320.  
  321. function buildSpeechletResponseWithoutCard(output, repromptText, shouldEndSession) {
  322. return {
  323. outputSpeech: {
  324. type: "PlainText",
  325. text: output
  326. },
  327. reprompt: {
  328. outputSpeech: {
  329. type: "PlainText",
  330. text: repromptText
  331. }
  332. },
  333. shouldEndSession: shouldEndSession
  334. };
  335. }
  336.  
  337. function buildResponse(sessionAttributes, speechletResponse) {
  338. return {
  339. version: "1.0",
  340. sessionAttributes: sessionAttributes,
  341. response: speechletResponse
  342. };
  343. }
  344.  
  345. function capitalizeFirst(s) {
  346. return s.charAt(0).toUpperCase() + s.slice(1)
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement