Communicating with Alexa (Amazon Echo) - Using Session Attributes (JavaScript - AWS Lambda)

it might be easy, but I couldn't figure it out in a few days.

I want Alex to have a conversation, for example:

β†’ Alexa, run testSkill.

A: Test proficiency has begun. Tell me the number.

β†’ One.

A: Alright, tell me the color now.

β†’ Blue.

A: And finally, tell me the name of the animal.

β†’ Chicken.

A: You told me one, blue and chicken.

I found out that I have to process the attributes of a skill session, which is JSON, and passes information between intentions.

I use such functions;

function testConversation(intent, session, callback) { var cardTitle = intent.name; var repromptText = ""; var sessionAttributes = { // I don't know how to handle this nameOfPairOne: "", nameOfPairTwo: "", }; var shouldEndSession = false; var speechOutput = ""; var color= convertToASCII(intent.slots.color.value); sessionAttributes.nameOfPairOne = color; speechOutput = "You said "+sessionAttributes.nameOfPairOne+". Please say another thing. "; callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); } function testConversation2(intent, session, callback) { var cardTitle = intent.name; var repromptText = ""; var sessionAttributes = session.attributes; var shouldEndSession = false; var speechOutput = ""; var number = convertToASCII(intent.slots.number.value); sessionAttributes.nameOfPairTwo = number; speechOutput = "You first said "+sessionAttributes.nameOfPairOne+", and now said "+sessionAttributes.nameOfPairTwo; callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); } //------Helpers that build all of the responses ---------// function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { return { outputSpeech: {type: "PlainText", text: output}, card: {type: "Simple", title: "SessionSpeechlet - " + title, content: "SessionSpeechlet - " + output}, reprompt: {outputSpeech: {type: "PlainText", text: repromptText}}, shouldEndSession: shouldEndSession }; } function buildResponse(sessionAttributes, speechletResponse) { return {version: "1.0", sessionAttributes: sessionAttributes, response: speechletResponse}; } 

The part of the code from the onIntent () function, where I call the above functions. (I know this is wrong, but could not figure out the right way)

  else if ("getColorNum" == intentName) { if (session.attributes.nameOfPairOne === "") { testConversation(intent, session, callback); } else { testConversation2(intent, session, callback); } } 

And the JSON intent scheme is as follows:

  "intents": [ { "intent": "getColorNum", "slots": [ { "name": "Color", "type": "ColorSlot" }, { "name": "Number", "type": "NumberSlot" } ] } 

]}

So am I doing everything wrong? Where is the mistake? And how can I build a conversation, as I mentioned? Thanks, now.

+5
source share
2 answers

Since no one gave you an answer, I thought I would give you two cents. My choice language is Python, but I can give you some high-level ideas.

  • Use the session object to track everything related to the conversation. After you compute the β€œresponse”, serialize your session object in a json session and deserialize it in the next intent request.
  • Maintain the state of the conversation and use the end-state approach to know where you are and what callback to perform, given the intent and specific state of the conversation.
  • Share the logic of the conversation with the ASK interface as much as you can. Try writing tests that you can run locally.

If you want to see how I implemented them, check:

+2
source

There are several ways to approach this, depending on the complexity of what you are trying to save.

Use slots first. If one, blue, chicken, etc. Located in classified categories, you can store them in slots and access them in the JSON way, getting the value and saving the variable in your JavaScript code. Example (sudo):

 var something1 = event.request.intent.slots.assetName.value; var something2 = event.request.intent.slots.assetName.value; buildSpeechletResponse(`my ${something1} likes ${something2} and blah blah blah`) 

Secondly, using attributes to store something and use it at the end of execution, Alexa does not remember this after the skill has ended. Example:

 'BreakfastIntent': function () { var restaurant = randomArrayElement(getRestaurantsByMeal('breakfast')); this.attributes['restaurant'] = restaurant.name; var say = 'For breakfast, try this, ' + restaurant.name + '. Would you like to hear more?'; this.response.speak(say).listen(say); this.emit(':responseReady'); }, 

Finally, storing them in a database is highly recommended using DynamoDB, and there are many tutorials for storing and accessing Dynamo from the Alexa Skill Kit. Checkout: https://github.com/alexa/alexa-cookbook/tree/master/aws/Amazon-DynamoDB/read

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1262212/


All Articles