Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var https = require('https');
- function elicitSlot(sessionAttributes, intentName, slots, slotToElicit, message) {
- return {
- sessionAttributes,
- dialogAction: {
- type: 'ElicitSlot',
- intentName,
- slots,
- slotToElicit,
- message,
- },
- };
- }
- function close(forward, sessionAttributes, fulfillmentState, message) {
- if (forward === true) {
- return {
- sessionAttributes,
- dialogAction: {
- type: 'Close',
- fulfillmentState,
- message,
- },
- };
- }
- return {
- sessionAttributes: {
- CloseType: 'EscalateToRep',
- },
- dialogAction: {
- type: 'Close',
- fulfillmentState,
- message,
- }
- }
- }
- function delegate(sessionAttributes, slots) {
- return {
- sessionAttributes,
- dialogAction: {
- type: 'Delegate',
- slots,
- },
- };
- }
- function buildValidationResult(isValid, violatedSlot, messageContent) {
- const resultType = 'Validation';
- if (messageContent == null) {
- return {
- resultType,
- isValid,
- violatedSlot,
- };
- }
- return {
- resultType,
- isValid,
- violatedSlot,
- message: { contentType: 'PlainText', content: messageContent },
- };
- }
- function buildEscalationResult(noEscalation, violatedSlot, messageContent) {
- const resultType = 'Escalation';
- if (messageContent == null) {
- return {
- resultType,
- noEscalation,
- violatedSlot,
- };
- }
- return {
- resultType,
- noEscalation,
- violatedSlot,
- message: {contentType: 'PlainText', content: messageContent}
- }
- }
- function checkIfTicketExists(ticketNumber) {
- try {
- var options = {
- host: 'trackerbeyond.onesourcebt.com',
- port: 443,
- path: '/trackerbeyond/api/incident/search?searchConditions=[{"ConditionType":"Exact","FieldName":"IncidentID","TableName":"z_Indident","Lookup":"","SearchValue":"999999","EndingSearchValue":"","OpenActive":"OpenOrActive"}]&fields=["IncidentID","Description"]',
- headers: {
- 'Authorization': 'redacted'
- }
- }
- var request = https.get(options, function(response) {
- var body = '';
- response.setEncoding('utf8');
- response.on('data', function(chunk) {
- body += chunk;
- });
- response.on('end', function() {
- var ticketSearchResult = JSON.parse(body)
- return ticketSearchResult;
- });
- });
- } catch (err) {
- console.log(err.message);
- }
- }
- function validateCheckIn(ticketNumber, laptop, airMagnet, software, zip, callback) {
- const confirmations = ['Yes', 'Yeah', 'Yes I do', 'Yep', 'I do', 'a'];
- if (ticketNumber) {
- if (ticketNumber.length != 6) {
- 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.'));
- }
- ticketExists = checkIfTicketExists(ticketNumber);
- if (!ticketExists) {
- return this.callback(buildValidationResult(false, 'Ticket_Number', 'That ticket number does not exist.'));
- }
- }
- if (laptop && confirmations.indexOf(laptop) === -1) {
- return buildEscalationResult(false, 'Laptop', 'I\'ll need to transfer you to a representative who can better assist you. One moment.');
- }
- if (airMagnet && confirmations.indexOf(airMagnet) === -1) {
- return buildEscalationResult(false, 'Air_Magnet_Kit', 'I\'ll need to transfer you to a representative who can better assist you. One moment.');
- }
- if (software && confirmations.indexOf(software) === -1) {
- return buildEscalationResult(false, 'Air_Magnet_Kit', 'I\'ll need to transfer you to a representative who can better assist you. One moment.');
- }
- if (zip && zip.length != 5) {
- return buildValidationResult(false, 'Zip_Code', 'That\'s not a valid zip code. I need the five digit zip code listed on your work order.');
- }
- return buildValidationResult(true, null, null);
- }
- function checkIn(intentRequest, callback) {
- const ticketNumber = intentRequest.currentIntent.slots.Ticket_Number;
- const laptop = intentRequest.currentIntent.slots.Laptop;
- const airMagnet = intentRequest.currentIntent.slots.Air_Magnet_Kit;
- const software = intentRequest.currentIntent.slots.Software_Installed;
- // const date = intentRequest.currentIntent.slots.Date_Of_Work_Order;
- const zip = intentRequest.currentIntent.slots.Zip_Code;
- const source = intentRequest.invocationSource;
- if (source === 'DialogCodeHook') {
- // Perform basic validation on the supplied input slots. Use the elicitSlot dialog action to re-prompt for the first violation detected.
- const slots = intentRequest.currentIntent.slots;
- const validationResult = validateCheckIn(ticketNumber, laptop, airMagnet, software, zip, callback);
- if (validationResult.resultType == 'Validation') {
- if (!validationResult.isValid) {
- slots[validationResult.violatedSlot] = null;
- callback(elicitSlot(intentRequest.sessionAttributes, intentRequest.currentIntent.name, slots, validationResult.violatedSlot, validationResult.message));
- return;
- }
- }
- if (validationResult.resultType == 'Escalation') {
- if (!validationResult.noEscalation) {
- callback(close(true, intentRequest.sessionAttributes, 'Fulfilled', validationResult.message));
- }
- }
- const outputSessionAttributes = intentRequest.sessionAttributes;
- callback(delegate(outputSessionAttributes, intentRequest.currentIntent.slots));
- return;
- }
- callback(close(false, intentRequest.sessionAttributes, 'Fulfilled',
- { contentType: 'PlainText', content: 'Thanks, you have been checked in for ticket number ' + ticketNumber + '.' }));
- }
- // --------------- Intents -----------------------
- /**
- * Called when the user specifies an intent for this skill.
- */
- function dispatch(intentRequest, callback) {
- const intentName = intentRequest.currentIntent.name;
- // Dispatch to your skill's intent handlers
- if (intentName === 'CheckIn') {
- return checkIn(intentRequest, callback);
- }
- throw new Error('Intent with name ${intentName} not supported');
- }
- // --------------- Main handler -----------------------
- // Route the incoming request based on intent.
- // The JSON body of the request is provided in the event slot.
- exports.handler = function (event, context, callback) {
- dispatch(event, (response) => callback(null, response));
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement