Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. var https = require('https');
  3.  
  4. function elicitSlot(sessionAttributes, intentName, slots, slotToElicit, message) {
  5.     return {
  6.         sessionAttributes,
  7.         dialogAction: {
  8.             type: 'ElicitSlot',
  9.             intentName,
  10.             slots,
  11.             slotToElicit,
  12.             message,
  13.         },
  14.     };
  15. }
  16.  
  17. function close(forward, sessionAttributes, fulfillmentState, message) {
  18.     if (forward === true) {
  19.         return {
  20.             sessionAttributes,
  21.             dialogAction: {
  22.                 type: 'Close',
  23.                 fulfillmentState,
  24.                 message,
  25.             },
  26.         };
  27.     }
  28.     return {
  29.  
  30.         sessionAttributes: {
  31.             CloseType: 'EscalateToRep',
  32.         },
  33.         dialogAction: {
  34.             type: 'Close',
  35.             fulfillmentState,
  36.             message,
  37.         }
  38.     }
  39.  
  40. }
  41.  
  42. function delegate(sessionAttributes, slots) {
  43.     return {
  44.         sessionAttributes,
  45.         dialogAction: {
  46.             type: 'Delegate',
  47.             slots,
  48.         },
  49.     };
  50. }
  51.  
  52.  
  53. function buildValidationResult(isValid, violatedSlot, messageContent) {
  54.     const resultType = 'Validation';
  55.     if (messageContent == null) {
  56.         return {
  57.             resultType,
  58.             isValid,
  59.             violatedSlot,
  60.         };
  61.     }
  62.     return {
  63.         resultType,
  64.         isValid,
  65.         violatedSlot,
  66.         message: { contentType: 'PlainText', content: messageContent },
  67.     };
  68. }
  69.  
  70. function buildEscalationResult(noEscalation, violatedSlot, messageContent) {
  71.     const resultType = 'Escalation';
  72.     if (messageContent == null) {
  73.         return {
  74.             resultType,
  75.             noEscalation,
  76.             violatedSlot,
  77.         };
  78.     }
  79.     return {
  80.         resultType,
  81.         noEscalation,
  82.         violatedSlot,
  83.         message: {contentType: 'PlainText', content: messageContent}
  84.     }
  85. }
  86.    
  87.  
  88. function checkIfTicketExists(ticketNumber) {
  89.     try {
  90.         var options = {
  91.             host: 'trackerbeyond.onesourcebt.com',
  92.             port: 443,
  93.             path: '/trackerbeyond/api/incident/search?searchConditions=[{"ConditionType":"Exact","FieldName":"IncidentID","TableName":"z_Indident","Lookup":"","SearchValue":"999999","EndingSearchValue":"","OpenActive":"OpenOrActive"}]&fields=["IncidentID","Description"]',
  94.             headers: {
  95.                 'Authorization': 'redacted'
  96.             }
  97.         }
  98.  
  99.         var request = https.get(options, function(response) {
  100.             var body = '';
  101.             response.setEncoding('utf8');
  102.             response.on('data', function(chunk) {
  103.                 body += chunk;
  104.             });
  105.             response.on('end', function() {
  106.                 var ticketSearchResult = JSON.parse(body)
  107.                 return ticketSearchResult;
  108.             });  
  109.         });
  110.     } catch (err) {
  111.         console.log(err.message);
  112.     }
  113.            
  114. }
  115.  
  116. function validateCheckIn(ticketNumber, laptop, airMagnet, software, zip, callback) {
  117.     const confirmations = ['Yes', 'Yeah', 'Yes I do', 'Yep', 'I do', 'a'];
  118.    
  119.    
  120.    
  121.     if (ticketNumber) {
  122.        
  123.         if (ticketNumber.length != 6) {
  124.             return this.callback(buildValidationResult(false, 'Ticket_Number', 'Ticket number ' + ticketNumber + ' is not valid. I need the six digit ticket number for the job you\'re checking in for.'));
  125.         }
  126.         ticketExists = checkIfTicketExists(ticketNumber);
  127.  
  128.         if (!ticketExists) {
  129.             return this.callback(buildValidationResult(false, 'Ticket_Number', 'That ticket number does not exist.'));
  130.        
  131.         }
  132.     }
  133.  
  134.        
  135.            
  136.        
  137.     if (laptop && confirmations.indexOf(laptop) === -1) {
  138.         return buildEscalationResult(false, 'Laptop', 'I\'ll need to transfer you to a representative who can better assist you. One moment.');
  139.     }
  140.  
  141.     if (airMagnet && confirmations.indexOf(airMagnet) === -1) {
  142.         return buildEscalationResult(false, 'Air_Magnet_Kit', 'I\'ll need to transfer you to a representative who can better assist you. One moment.');
  143.     }
  144.  
  145.     if (software && confirmations.indexOf(software) === -1) {
  146.         return buildEscalationResult(false, 'Air_Magnet_Kit', 'I\'ll need to transfer you to a representative who can better assist you. One moment.');
  147.     }
  148.  
  149.     if (zip && zip.length != 5) {
  150.         return buildValidationResult(false, 'Zip_Code', 'That\'s not a valid zip code. I need the five digit zip code listed on your work order.');
  151.     }
  152.  
  153.     return buildValidationResult(true, null, null);
  154. }
  155.  
  156.  
  157. function checkIn(intentRequest, callback) {
  158.     const ticketNumber = intentRequest.currentIntent.slots.Ticket_Number;
  159.     const laptop = intentRequest.currentIntent.slots.Laptop;
  160.     const airMagnet = intentRequest.currentIntent.slots.Air_Magnet_Kit;
  161.     const software = intentRequest.currentIntent.slots.Software_Installed;
  162.     // const date = intentRequest.currentIntent.slots.Date_Of_Work_Order;
  163.     const zip = intentRequest.currentIntent.slots.Zip_Code;
  164.  
  165.     const source = intentRequest.invocationSource;
  166.  
  167.     if (source === 'DialogCodeHook') {
  168.         // Perform basic validation on the supplied input slots.  Use the elicitSlot dialog action to re-prompt for the first violation detected.
  169.         const slots = intentRequest.currentIntent.slots;
  170.         const validationResult = validateCheckIn(ticketNumber, laptop, airMagnet, software, zip, callback);
  171.         if (validationResult.resultType == 'Validation') {
  172.             if (!validationResult.isValid) {
  173.                 slots[validationResult.violatedSlot] = null;
  174.                 callback(elicitSlot(intentRequest.sessionAttributes, intentRequest.currentIntent.name, slots, validationResult.violatedSlot, validationResult.message));
  175.                 return;
  176.             }
  177.         }
  178.         if (validationResult.resultType == 'Escalation') {
  179.             if (!validationResult.noEscalation) {
  180.                 callback(close(true, intentRequest.sessionAttributes, 'Fulfilled', validationResult.message));
  181.             }
  182.         }
  183.  
  184.         const outputSessionAttributes = intentRequest.sessionAttributes;
  185.         callback(delegate(outputSessionAttributes, intentRequest.currentIntent.slots));
  186.         return;
  187.     }
  188.  
  189.  
  190.     callback(close(false, intentRequest.sessionAttributes, 'Fulfilled',
  191.     { contentType: 'PlainText', content: 'Thanks, you have been checked in for ticket number ' + ticketNumber + '.' }));
  192.  
  193. }
  194.  // --------------- Intents -----------------------
  195.  
  196. /**
  197.  * Called when the user specifies an intent for this skill.
  198.  */
  199. function dispatch(intentRequest, callback) {
  200.  
  201.     const intentName = intentRequest.currentIntent.name;
  202.  
  203.     // Dispatch to your skill's intent handlers
  204.     if (intentName === 'CheckIn') {
  205.         return checkIn(intentRequest, callback);
  206.     }
  207.     throw new Error('Intent with name ${intentName} not supported');
  208. }
  209.  
  210. // --------------- Main handler -----------------------
  211.  
  212. // Route the incoming request based on intent.
  213. // The JSON body of the request is provided in the event slot.
  214. exports.handler = function (event, context, callback) {
  215.     dispatch(event, (response) => callback(null, response));
  216. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement