question

April L. Hamilton avatar image
April L. Hamilton asked

onIntent in AWS Lambda (javascrpt)

I've got my first Echo app working and submitted, but it's limited to four steps: 1. Launch App 2. Prompt Alexa to Fetch a String 3. Alexa Reads the String 4. App Closes Now I'm working on a more complex app, and I'm struggling with how to insert more back-and-forth between steps 3 and 4. Specifically, what if I want Alexa to repeat what was said in step 3? What if I want Alexa to go back to step 2 and fetch a different string based on a different user prompt? There's an onIntent function in the AWS Lambda Favorite Colors sample app, but I'm not sure if it's just calling an existing Alexa Service method (which I could then call anywhere in my code to get that back-and-forth going), or if I have to write a custom onIntent() function every time I want to accept input from the user and have Alexa give a reply. In other words, if we assume there's no onIntent() function anywhere in my code but that my Intents/Utterances map includes "Repeat", "GoBack" and "ThirdThing", could I do this within one of my functions: if ("Repeat" === intentName) { speechOutput = "[last thing Alexa said]; } else if ("GoBack" === intentName) { [call function to go back to step 2]; } else if ("ThirdThing" === intentName) { speechOutput = "Some third thing"; } else { throw "Invalid intent"; } } And regarding that "last thing Alexa said", is there any direct way to reference it, or do I have to set a session variable of my own to store speechOutput for later recall? Thanks in advance for any guidance on this - for the most part, I'm really [b]loving[/b] AWS Lambda for Echo dev! :)
alexa skills kit
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Greg Crawford avatar image
Greg Crawford answered
The 'onIntent' function in the Favorite Colors sample app is called by the 'handler' function. This Lambda 'handler' function is called automatically for every interaction between Alexa and your code. If the request type (carried in the event object which is passed to 'handler') is an "IntentRequest" then the 'handler' calls the 'onIntent' function. The 'onIntent' function should then determine the intent and respond accordingly. So yes, if you are using the template provided by that example. you want to have an 'onIntent' function and in that function you would test the 'intentName' and run the proper code. In the example you provide, you might call a 'repeat' function you have written in the 'intentName' is 'Repeat', or a 'goBack' function you have written if 'intentName' is 'GoBack', and etc. This is very similar to what you said except you *would* have a onIntent function in your code. For example: function onIntent((intentRequest, session, callback) { var intent = intentRequest.intent, var intentName = intentRequest.intent.name; if ("Repeat" === intentName) { repeat(); // This is a function you write which handles the "Repeat" intent } else if ("GoBack" === intentName) { goBack(); // This is a function you write which handles the "GoBack" intent } else if ("ThirdThing" === intentName) { thirdThing(); // This is a function you write which handles the "ThirdThing" intent } else { throw "Invalid intent"; } } As for referencing the last thing Alexa said, you will have to save the last response text on your own. This will require using a data store (such as DynamoDB) since the Lambda function loses state between calls from Alexa. There are parameters passed with each call to your Lambda function which indicate a sessionid and a session userid. The sessionid changes once the app closes but will be same between app launch and app closing (i.e. for the duration of the session). The userid, as far as I can tell remains constant day in and day out and I am assuming it is somehow tied to the user account mapped to an Echo device. You could use these ids to keep track of things via a database. I am in total agreement that AWS Lambda is a very nice fit for Echo/Alexa apps!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

April L. Hamilton avatar image
April L. Hamilton answered
G. Crawford - thanks for your detailed reply. But regarding the "repeat" function, given that the Favorite Color sample app stores the user's stated color for the duration of the session as a session attribute, is there any reason I couldn't store the first onSpeech event I'd like to have available for recall as a session attribute and simply update it each time a new onSpeech output I want available for recall occurs? I looked into DynamoDB as an alternative to the five arrays I have in my app because each array holds just over 350 items. However, the items are static, neither the app nor user can alter them. Given that, and that I'm calling each array within its respective function rather than pulling globals from all 5 right off the top, I decided I don't actually need DynamoDB. Employing it solely to store one session variable seems like overkill, but there may be something I'm missing here. If so, you're welcome to enlighten me. :)
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Greg Crawford avatar image
Greg Crawford answered
April - That is correct! I had forgotten about the "session attributes" which can be set and then are passed to subsequent calls during any given session. As long as the session doesn't close, that would be a great way for saving values between intents requests. My interest is in applications which need to preserve values between closing and re-launching of the app - so that if I make a request an hour later, I can know what the prior state was. This is where solutions such as DynamoDB (or even S3) are handy.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.