My Lambda function succeed with the test for "IntentRequest", when conducted on Lambda console. But when I test the Intent from the developer console through utterances, its not working. The Lambda function itself is not getting called, The strange thing is, this works for Launch request. But not for any Intent request. I have created skills, configured the ARN and Skill Endpoint as per current document.
I can see logs for both Launch and Intent request when I make test from the Lambda console. But when I do it from the developer console ,I can see logs only for Launch request.
Below is my code,
Invocation model :
{ "interactionModel": { "languageModel": { "invocationName": "apex orders", "intents": [ { "name": "AMAZON.FallbackIntent", "samples": [] }, { "name": "AMAZON.CancelIntent", "samples": [] }, { "name": "AMAZON.HelpIntent", "samples": [] }, { "name": "AMAZON.StopIntent", "samples": [] }, { "name": "AMAZON.NavigateHomeIntent", "samples": [] }, { "name": "NumberOfOrders", "slots": [], "samples": [ "how many orders are there", "how many orders are there in the database" ] } ], "types": [] } } }
My Node.JS Lambda function :
'use strict'; exports.handler = function (event, context) { try { console.log("event.session.application.applicationId=" + event.session.application.applicationId); console.log("Started"); if (event.session.new) { console.log("New session created"); onSessionStarted(event.request, event.session); } if (event.request.type == "LaunchRequest") { console.log("Skill Launched"); onLaunch(event.request, event.session, function callback(sessionAttributes, speechletResponse) { context.succeed(buildResponse(sessionAttributes, speechletResponse)); }); } else if (event.request.type == "IntentRequest") { console.log("Intent Called"); onIntent(event.request, event.session, function callback(sessionAttributes, speechletResponse) { console.log("Intent called stepped in"); context.succeed(buildResponse(sessionAttributes, speechletResponse)); }); } else if (event.request.type == "SessionEndedRequest") { console.log("Session End"); onSessionEnded(event.request, event.session); context.succeed(); } } catch (e) { console.log("Exception: " + e); context.fail("Exception: " + e); } }; function onSessionStarted(sessionStartedRequest, session) { console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId + ", sessionId=" + session.sessionId); } function onLaunch(launchRequest, session, callback) { console.log("onLaunch requestId=" + launchRequest.requestId + ", sessionId=" + session.sessionId); var cardTitle = "Customer Orders" var speechOutput = "You can ask APEX how many orders are in the orders table by saying. Alexa, ask APEX how many orders there are." callback(session.attributes, buildSpeechletResponse(cardTitle, speechOutput, "", true)); } function onIntent(intentRequest, session, callback) { console.log("onIntent requestId=" + intentRequest.requestId + ", sessionId=" + session.sessionId); var intent = intentRequest.intent, intentName = intentRequest.intent.name; console.log(intentName); if (intentName == 'NumberOfOrders') { handleChartRequest(intent, session, callback); } else { throw "Invalid intent"; } } function onSessionEnded(sessionEndedRequest, session) { console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId + ", sessionId=" + session.sessionId); } const https = require('https'); function handleChartRequest(intent, session, callback) { callAPEX( function (result, error) { if (error) { console.log('error') } else { console.log("Final result is"+JSON.stringify(result)) callback(null,buildSpeechletResponseWithoutCard(result.speech,"sample re-prompt",true)) } } ); } var callAPEX = function (callback) { var url = "https://apex.oracle.com/pls/apex/csykes_dev/hr/orders/"; var req = https.get(url, (res) => { var body = ""; res.on("data", (chunk) => { body += chunk; }); res.on("end", () => { var result = JSON.parse(body); callback({"speech":result.items[0].message }); }); }).on("error", (error) => { console.log('error'); }); }; function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { return { outputSpeech: { type: "PlainText", text: output }, card: { type: "Simple", title: title, content: output }, reprompt: { outputSpeech: { type: "PlainText", text: repromptText } }, shouldEndSession: shouldEndSession }; } function buildSpeechletResponseWithoutCard(output, repromptText, shouldEndSession) { return { outputSpeech: { type: "PlainText", text: output }, reprompt: { outputSpeech: { type: "PlainText", text: repromptText } }, shouldEndSession: shouldEndSession }; } function buildResponse(sessionAttributes, speechletResponse) { return { version: "1.0", sessionAttributes: sessionAttributes, response: speechletResponse }; }
----------------------------------