85
« Last post by jitterjames on August 18, 2022, 12:57:58 PM »
Here is my lambda code. Note that there are a bunch of environment variables that need to be set or you can just replace them with your own values directly in the code.
Look in the code for process.env.VARNAME
/*
****** Pulls speech input and passes on to Voxcommando for custom events *****
*/
var http = require('http');
var Echo_App_ID = "amzn1.ask.skill.3bee3d22-3eb3-4282-4df7-925fc8111b2e";
exports.handler = function (event, context) {
try {
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) {}
/**
* Called when the user launches the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, callback) {
// Dispatch to your skill's launch.
getWelcomeResponse(callback);
}
/**
* Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback) {
var intent = intentRequest.intent,
intentName = intentRequest.intent.name;
callEchoToVC(intent, session, callback); // Whatever the Intent is, pass it straight through to VC
}
/**
* Called when the user ends the session.
* Is not called when the skill returns shouldEndSession=true.
*/
function onSessionEnded(sessionEndedRequest, session) {
}
// --------------- Main Intent Processing Function ----------------
function callEchoToVC(intent, session, callback)
{
var sessionAttributes = "{}";
var cardTitle = "VoxCommando";
var shouldEndSession = true;
var payload = "";
var speechOutput = "error maybe?";
var repromptText = "...";
var i = 0;
var slots = intent.slots;
//Pull the spoken text and format
var actionSlot = intent.slots.Action;
var setAction = actionSlot.value.toLowerCase();
if (setAction.endsWith(" only"))
{
shouldEndSession=true;
setAction = setAction.replace(" only","")
}
if(process.env.phrases_cancel.includes(setAction))
{
speechOutput ="Cancelling";
shouldEndSession=true;
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
return;
}
else if (setAction.length<5)
{
speechOutput =process.env.tts_tooshort;
shouldEndSession=false;
callback(sessionAttributes, buildSpeechletResponse("Action string too short", speechOutput, repromptText, shouldEndSession));
return;
}
setAction = setAction.replace("1st","first")
.replace("2nd","second")
.replace("3rd","third")
.replace("4th","fourth")
.replace("5st","fifth")
.replace("6th","sixth")
.replace("7th","seventh")
.replace("8th","eighth")
.replace("9th","nineth")
.replace("10th","tenth")
;
var setActionURI = require('querystring').escape(setAction);
var VC_uri = '/api/VC.TriggerEvent&&EchoToVC&&' + setActionURI;
var get_options = {
host: process.env.ip,
port: process.env.port,
path: VC_uri
};
if (process.env.username !== '')
{
get_options = {
host: process.env.ip,
port: process.env.port,
path: VC_uri,
headers: {
'Authorization': 'Basic ' + new Buffer.from(process.env.username + ':' + process.env.pwd).toString('base64')
}
};
}
// Set up the request
var get_req = http.request(get_options, function(res) {
var VC_results = "";
res.setEncoding('utf8');
res.on('data', function (chunk) {
VC_results += chunk;
});
res.on('end', function () {
//console.log(VC_results);
cardTitle = VC_results;
console.log(VC_results);
if (res.statusCode === 200) {
//Parse the Body results from VC, if the command was unknown we will repromt
if(VC_results.indexOf('<Success>False') > -1) {
cardTitle = "Vc error: ";
speechOutput = "VoxCommando API error: "+setAction;
repromptText = "Try again or say cancel.";
shouldEndSession=false;
}
else
{
var regex = /<WebResponse>([\s\S]*?)</;
var reMatches = VC_results.match(regex);
if (reMatches)
{
var speechOutput = reMatches[1];
}
else speechOutput = "I suck";
}
}
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); //Pass stingResult instead of speechOutput for debugging if needed
});
});
get_req.on('error', function (e) {
shouldEndSession=true;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
get_req.end();
}
// --------------- WelcomResponse Function ----------------
function getWelcomeResponse(callback) {
// If we wanted to initialize the session to have some attributes we could add those here.
var sessionAttributes = {};
var cardTitle = "Welcome to Echo To Voxcommando";
var speechOutput = "You see a smaller Jarvis in there.";
// If the user either does not reply to the welcome message or says something that is not
// understood, they will be prompted again with this text.
var repromptText = "Do you speak English?";
var shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
// --------------- Helper Functions ----------------
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
return {
outputSpeech: {
type: "PlainText",
text: output
},
card: {
type: "Simple",
title: "EchoToVoxcommando - " + title,
content: output
},
reprompt: {
outputSpeech: {
type: "PlainText",
text: repromptText
}
},
shouldEndSession: shouldEndSession
};
}
function buildResponse(sessionAttributes, speechletResponse) {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
};
}