Last active
May 16, 2017 01:59
-
-
Save AnshumanTripathi/0b3ee8eeed03bf3acd727589137c5196 to your computer and use it in GitHub Desktop.
UIPro Alexa Interaction Model and Lambda Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
exports.handler = function(event, context, callback) { | |
try { | |
if (event.session.new) { | |
console.log('Hello from Lambda'); | |
} | |
switch (event.request.type) { | |
case "LaunchRequest": | |
context.succeed( | |
generateResponse({}, | |
buildSpeechletResponse("Hello, Welcome to UIPro!", true) | |
) | |
); | |
console.log("Launch Request"); | |
break; | |
case "IntentRequest": | |
var data = { | |
"uid": "123456", | |
"isNewPage": false, | |
"template": "", | |
"element": "", | |
"elementName": "elename", | |
"elementId": "eleID", | |
"elementPosition": "", | |
"elementValue": "", | |
"isSaveRequest": false, | |
"isRemoveLast": false | |
}; | |
console.log("Intent Req from intent name:" + event.request.intent.name); | |
//dialog directive testing sample | |
if (event.request.dialogState === "STARTED") { | |
console.log("in started"); | |
console.log(" current request: " + JSON.stringify(event.request)); | |
var updatedIntent = event.request.intent; | |
//optionally pre-fill slots: update the intent object with slot values for which | |
//you have defaults, then return Dialog.Delegate with this updated intent | |
// in the updatedIntent property | |
context.succeed( | |
buildResponse({}, | |
buildSpeechletResponseWithDirectiveNoIntent()) | |
); | |
} else if (event.request.dialogState !== "COMPLETED") { | |
console.log("in not completed"); | |
console.log(" current request: " + JSON.stringify(event.request)); | |
// return a Dialog.Delegate directive with no updatedIntent property. | |
context.succeed( | |
buildResponse({}, | |
buildSpeechletResponseWithDirectiveNoIntent()) | |
); | |
} else { | |
//BUILD THE JSON INPUT - IMPORTANT | |
if (event.request.intent.name == "AddTemplate") { | |
var template = { | |
"contact us": "contactus", | |
"header": "header", | |
"footer": "footer", | |
"login": "login" | |
}; | |
if (!template[event.request.intent.slots.template.value]) { | |
context.succeed(generateResponse({}, | |
buildSpeechletResponse("You can ask for login, header, footer or contact us template. Please try again", true))); | |
} | |
data.template = template[event.request.intent.slots.template.value]; | |
} else if (event.request.intent.name == "ClearScreen") { | |
data.isNewPage = true; | |
} else if (event.request.intent.name == "RemoveLastIntent") { | |
data.isRemoveLast = true; | |
} else if (event.request.intent.name == "AddElement") { | |
var alignment = { | |
"left": "LEFT", | |
"right": "RIGHT", | |
"center": "CENTER" | |
}; | |
var elementTypesAllowed = ["textfield", "checkbox", "button", "label"]; | |
var elementType = event.request.intent.slots.element.value; | |
elementType = elementType.replace(/\s/g, ''); | |
if (elementTypesAllowed.indexOf(elementType) < 0) { | |
context.succeed( | |
generateResponse({}, | |
buildSpeechletResponse("I cannot process that. You can ask for textfield, checkbox, button or label.", true) | |
)); | |
} | |
if (!alignment[event.request.intent.slots.position.value]) { | |
context.succeed( | |
generateResponse({}, buildSpeechletResponse("Refer to the Manual for the correct alignment.", true))); | |
} | |
if (!event.request.intent.slots.value.value) { | |
context.succeed( | |
generateResponse({}, buildSpeechletResponse("You must say the value for the element.", true))); | |
} | |
data.element = elementType; | |
data.elementPosition = alignment[event.request.intent.slots.position.value]; | |
data.elementValue = event.request.intent.slots.value.value; | |
} else if (event.request.intent.name === "SaveIntent") { | |
data.isSaveRequest = true; | |
} else { | |
context.succeed( | |
generateResponse({}, | |
buildSpeechletResponse("UI Pro can add Element, create template or clear screen.", true)) | |
); | |
} | |
var params = JSON.stringify(data); | |
console.log(data); | |
var options = { | |
host: 'uipro-vaadinapp-ui.mybluemix.net', | |
path: '/uiprolistner', | |
// host: '104.154.89.221', | |
// port: '3000', | |
// path: '/test', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json; charset=utf-8', | |
'Content-Length': params.length | |
} | |
}; | |
callback = function(response) { | |
console.log('In callback!'); | |
var str = ''; | |
response.on('data', function(chunk) { | |
str += chunk; | |
console.log(chunk); | |
}); | |
response.on('end', function() { | |
var speechletResponse = ""; | |
var responseJSON = JSON.parse(str); | |
if (!responseJSON.statusCode) { | |
speechletResponse = "There was a problem processing the request. Please try again."; | |
} else if (responseJSON.statusCode !== 200) { | |
speechletResponse = "There was a problem processing the request. Please try again."; | |
} | |
if (responseJSON.statusCode === 200) { | |
speechletResponse = "Done"; | |
} | |
context.succeed( | |
generateResponse({}, | |
buildSpeechletResponse(speechletResponse, true) | |
) | |
); | |
}); | |
}; | |
var postReq = http.request(options, callback); | |
postReq.on('error', function(error) { | |
console.log("Error occured while connecting to the server: " + error); | |
context.succeed( | |
generateResponse({}, | |
buildSpeechletResponse("UI Pro cannot connect to the server", true))); | |
}); | |
postReq.write(params); | |
postReq.end(); | |
} | |
console.log("done Ended Request"); | |
break; | |
case "SessionEndedRequest": | |
console.log("Session Ended Request"); | |
break; | |
default: // ERROR CASE WHERE IT WHAT USER SAID IS NOT CORRECT | |
// context.fail("Invalid request type: $(event.request.type)"); | |
context.succeed( | |
generateResponse({}, | |
buildSpeechletResponse("Sorry, I cannot understand ", true) | |
) | |
); | |
} | |
} catch (error) { | |
context.fail('Exception Occured' + error); | |
} | |
}; | |
var buildSpeechletResponse = function(outputText, shouldEndSession) { | |
return { | |
"outputSpeech": { | |
"type": "PlainText", | |
"text": outputText | |
}, | |
"shouldEndSession": shouldEndSession | |
} | |
}; | |
var generateResponse = function(sessionAttributes, speechletResponse) { | |
return { | |
"version": "1.0", | |
"sessionAttributes": sessionAttributes, | |
"response": speechletResponse | |
} | |
}; | |
function buildResponse(sessionAttributes, speechletResponse) { | |
console.log("Responding with " + JSON.stringify(speechletResponse)); | |
return { | |
"version": "1.0", | |
"sessionAttributes": sessionAttributes, | |
"response": speechletResponse, | |
}; | |
} | |
function buildSpeechletResponseWithDirectiveNoIntent() { | |
console.log("in buildSpeechletResponseWithDirectiveNoIntent"); | |
return { | |
"outputSpeech": null, | |
"card": null, | |
"directives": [{ | |
"type": "Dialog.Delegate" | |
}], | |
"reprompt": null, | |
"shouldEndSession": false | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"intents": [ | |
{ | |
"name": "AddElement", | |
"samples": [ | |
"add an {element}", | |
"add a {element}", | |
"add {element} " | |
], | |
"slots": [ | |
{ | |
"name": "element", | |
"type": "LIST_OF_ELEMENT", | |
"samples": [] | |
}, | |
{ | |
"name": "position", | |
"type": "LIST_OF_POS", | |
"samples": [ | |
"{position} ", | |
"at {position} ", | |
"place it at the {position} of the page" | |
] | |
}, | |
{ | |
"name": "value", | |
"type": "LIST_OF_WORDS", | |
"samples": [ | |
"{value} ", | |
"the element will be called {value} ", | |
"value is {value} " | |
] | |
} | |
] | |
}, | |
{ | |
"name": "AddTemplate", | |
"samples": [ | |
"create a {template} {uttertemp}", | |
"create an {template} {uttertemp}", | |
"create {template} {uttertemp} ", | |
"create {uttertemp} ", | |
"create a {uttertemp} ", | |
"create an {uttertemp} " | |
], | |
"slots": [ | |
{ | |
"name": "template", | |
"type": "LIST_OF_TEMP", | |
"samples": [ | |
"{template} " | |
] | |
}, | |
{ | |
"name": "uttertemp", | |
"type": "LIST_OF_UTTERTEMP", | |
"samples": [] | |
} | |
] | |
}, | |
{ | |
"name": "AMAZON.CancelIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AMAZON.HelpIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "AMAZON.StopIntent", | |
"samples": [] | |
}, | |
{ | |
"name": "ClearScreen", | |
"samples": [ | |
"clear screen", | |
"clear page", | |
"clear ", | |
"clean page", | |
"clean screen", | |
"clear all", | |
"exit", | |
"delete", | |
"delete all" | |
], | |
"slots": [] | |
}, | |
{ | |
"name": "SaveIntent", | |
"samples": [ | |
"save", | |
"save design", | |
"save this design" | |
], | |
"slots": [] | |
} | |
], | |
"types": [ | |
{ | |
"name": "LIST_OF_ELEMENT", | |
"values": [ | |
{ | |
"name": { | |
"value": "check box" | |
} | |
}, | |
{ | |
"name": { | |
"value": "text field" | |
} | |
}, | |
{ | |
"name": { | |
"value": "button" | |
} | |
}, | |
{ | |
"name": { | |
"value": "label" | |
} | |
} | |
] | |
}, | |
{ | |
"name": "LIST_OF_POS", | |
"values": [ | |
{ | |
"name": { | |
"value": "bottom center" | |
} | |
}, | |
{ | |
"name": { | |
"value": "bottom left" | |
} | |
}, | |
{ | |
"name": { | |
"value": "bottom right" | |
} | |
}, | |
{ | |
"name": { | |
"value": "middle center" | |
} | |
}, | |
{ | |
"name": { | |
"value": "middle left" | |
} | |
}, | |
{ | |
"name": { | |
"value": "middle right" | |
} | |
}, | |
{ | |
"name": { | |
"value": "top center" | |
} | |
}, | |
{ | |
"name": { | |
"value": "top left" | |
} | |
}, | |
{ | |
"name": { | |
"value": "top right" | |
} | |
} | |
] | |
}, | |
{ | |
"name": "LIST_OF_TEMP", | |
"values": [ | |
{ | |
"name": { | |
"value": "login" | |
} | |
}, | |
{ | |
"name": { | |
"value": "header" | |
} | |
}, | |
{ | |
"name": { | |
"value": "footer" | |
} | |
}, | |
{ | |
"name": { | |
"value": "contact us" | |
} | |
} | |
] | |
}, | |
{ | |
"name": "LIST_OF_UTTERTEMP", | |
"values": [ | |
{ | |
"name": { | |
"value": "screen" | |
} | |
}, | |
{ | |
"name": { | |
"value": "page" | |
} | |
}, | |
{ | |
"name": { | |
"value": "template" | |
} | |
} | |
] | |
}, | |
{ | |
"name": "LIST_OF_WORDS", | |
"values": [ | |
{ | |
"name": { | |
"value": "random" | |
} | |
}, | |
{ | |
"name": { | |
"value": "anything" | |
} | |
}, | |
{ | |
"name": { | |
"value": "name" | |
} | |
}, | |
{ | |
"name": { | |
"value": "anything random" | |
} | |
}, | |
{ | |
"name": { | |
"value": "one two" | |
} | |
}, | |
{ | |
"name": { | |
"value": "two three" | |
} | |
}, | |
{ | |
"name": { | |
"value": "one" | |
} | |
}, | |
{ | |
"name": { | |
"value": "two" | |
} | |
}, | |
{ | |
"name": { | |
"value": "three" | |
} | |
} | |
] | |
} | |
], | |
"prompts": [ | |
{ | |
"id": "Elicit.Intent-AddElement.IntentSlot-position", | |
"promptVersion": "1.0", | |
"definitionVersion": "1.0", | |
"variations": [ | |
{ | |
"type": "PlainText", | |
"value": "Where shoud it be placed" | |
} | |
] | |
}, | |
{ | |
"id": "Elicit.Intent-AddElement.IntentSlot-value", | |
"promptVersion": "1.0", | |
"definitionVersion": "1.0", | |
"variations": [ | |
{ | |
"type": "PlainText", | |
"value": "what is the value for this element" | |
} | |
] | |
}, | |
{ | |
"id": "Elicit.Intent-AddTemplate.IntentSlot-template", | |
"promptVersion": "1.0", | |
"definitionVersion": "1.0", | |
"variations": [ | |
{ | |
"type": "PlainText", | |
"value": "which template do you need" | |
} | |
] | |
} | |
], | |
"dialog": { | |
"version": "1.0", | |
"intents": [ | |
{ | |
"name": "AddElement", | |
"confirmationRequired": false, | |
"prompts": {}, | |
"slots": [ | |
{ | |
"name": "element", | |
"type": "LIST_OF_ELEMENT", | |
"elicitationRequired": false, | |
"confirmationRequired": false, | |
"prompts": {} | |
}, | |
{ | |
"name": "position", | |
"type": "LIST_OF_POS", | |
"elicitationRequired": true, | |
"confirmationRequired": false, | |
"prompts": { | |
"elicit": "Elicit.Intent-AddElement.IntentSlot-position" | |
} | |
}, | |
{ | |
"name": "value", | |
"type": "LIST_OF_WORDS", | |
"elicitationRequired": true, | |
"confirmationRequired": false, | |
"prompts": { | |
"elicit": "Elicit.Intent-AddElement.IntentSlot-value" | |
} | |
} | |
] | |
}, | |
{ | |
"name": "AddTemplate", | |
"confirmationRequired": false, | |
"prompts": {}, | |
"slots": [ | |
{ | |
"name": "template", | |
"type": "LIST_OF_TEMP", | |
"elicitationRequired": true, | |
"confirmationRequired": false, | |
"prompts": { | |
"elicit": "Elicit.Intent-AddTemplate.IntentSlot-template" | |
} | |
}, | |
{ | |
"name": "uttertemp", | |
"type": "LIST_OF_UTTERTEMP", | |
"elicitationRequired": false, | |
"confirmationRequired": false, | |
"prompts": {} | |
} | |
] | |
}, | |
{ | |
"name": "ClearScreen", | |
"confirmationRequired": false, | |
"prompts": {}, | |
"slots": [] | |
}, | |
{ | |
"name": "SaveIntent", | |
"confirmationRequired": false, | |
"prompts": {}, | |
"slots": [] | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment