How to get Alexa userId?

I am creating an Alexa skill and I need me to save the userId of the user. I tried to restore it using event.session.user.userId . However, when I call console.log(event.session.user.userId) , the output is literally amzn1.ask.account.[unique-value-here] . I looked at a few of these questions, and none of them gave me a clear enough answer.

I'm not sure if this is a bug, only for the developer, or if userId is just an anonymous. If so, is there a way to get the actual userId? I assume that since Amazon has written a whole guide here:

https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/linking-an-alexa-user-with-a-user-in-your-system .

However, after a long day of debugging, I'm not sure what is real and what is not.

  var request = require('request'); var firebase = require('firebase'); var config = { apiKey: "my-api-key", authDomain: "stuff...", databaseURL: "more stuff...", storageBucket: "even more stuff...", }; firebase.initializeApp(config); // Get a reference to the database var database = firebase.database(); exports.handler = (event, context) => { try { // New session if (event.session.new) { // New Session console.log("NEW SESSION"); } // Launch Request switch (event.request.type) { case "LaunchRequest": var url = "https://api.random.org/json-rpc/1/invoke"; var myRequest = { "jsonrpc": "2.0", "method": "generateStrings", "params": { "apiKey": "another-api-key", "n": "1", "length": "3", "characters": "abcdefghijklmnopqrstuvwxyz0123456789" }, "id": 24 } var pin; request.post( url, {json: myRequest}, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(event.session.user.userId); // **Here**, output is literally amzn1.ask.account.[unique-value-here] pin = body.result.random.data[0]; writeUserPin(pin); var welcome = "Welcome"; var pinStatement = "Your 3 letter or number pin is: " + processPinForSpeech(pin); context.succeed( generateResponse( buildSpeechletReponse(welcome + pinStatement, true), {} ) ); console.log(pin); } else { console.log(error); } } ); console.log("LAUNCH REQUEST"); break; // Intent Request case "IntentRequest": console.log("INTENT REQUEST"); break; // Session Ended Request case "SessionEndedRequest": console.log("SESSION ENDED REQUEST"); break; default: context.fail(`INVALID REQUEST TYPE: ${event.request.type}`); } } catch (error) { context.fail(`Exception: ${error}`); } } // Helpers buildSpeechletReponse = (outputText, shouldEndSession) => { return { outputSpeech : { type: "PlainText", text: outputText }, shouldEndSession: shouldEndSession }; } generateResponse = (speechletResponse, sessionAttributes) => { return { version: "1.0", sessionAttributes: sessionAttributes, response: speechletResponse }; } function writeUserPin(pin) { console.log("writing stuff"); firebase.database().ref('newPins/' + pin).set({ num : "" }); } function processPinForSpeech(pin) { var wordNumArr = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; processedPin = ""; for (i = 0; i < pin.length; i++){ var currentChar = pin.charAt(i); if (isNaN(Number(currentChar))){ processedPin += currentChar + ". "; } else { processedPin += wordNumArr[Number(currentChar)] + ". "; } } return processedPin } 

The following are the results in CloudWatch logs:

  16:16:19 START RequestId: 48e335c5-d819-11e6-bc01-a939911adc24 Version: $LATEST  16:16:19 2017-01-11T16:16:19.639Z 48e335c5-d819-11e6-bc01-a939911adc24 NEW SESSION16:16:19 2017-01-11T16:16:19.758Z 48e335c5-d819-11e6-bc01-a939911adc24 LAUNCH REQUEST  16:16:20 2017-01-11T16:16:20.457Z 48e335c5-d819-11e6-bc01-a939911adc24 amzn1.ask.account.[unique-value-here]  16:16:20 2017-01-11T16:16:20.457Z 48e335c5-d819-11e6-bc01-a939911adc24 writing stuff16:16:20 2017-01-11T16:16:20.520Z 48e335c5-d819-11e6-bc01-a939911adc24 dd2  16:16:20 END RequestId: 48e335c5-d819-11e6-bc01-a939911adc24  16:16:20 REPORT RequestId: 48e335c5-d819-11e6-bc01-a939911adc24 Duration: 1005.48 ms Billed Duration: 1100 ms Memory Size: 128 MB Max Memory Used: 38 MB 
+13
source share
3 answers

Well, it turns out I did everything right (this time). The reason userId was literally amzn1.ask.account. [Unique-value-here] was because I tested it on the "Alexa Start Session" test event in the AWS Lambda console. When I asked Echo Dot to launch the skill, it generated the actual key. The problem is solved.

+2
source

You are doing it right. This amzn1.ask.account.[unique-value-here] actually the full identifier of the user. You can observe this for yourself by turning on your skill from Echo, completing several requests to your alexa skill, and noting that the userid between these requests has the same meaning.

In the JSON Reference :

userId: a string representing the unique identifier of the user who made the request. The length of this identifier can vary, but never more than 255 characters. UserId is automatically generated when the user allows the use of the skill in the Alexa application.

Note. Disabling and re-enabling the skill generates a new identifier.

If you only need to save user attributes between sessions, this value will be enough, and you can use it to uniquely identify this user, if they have the skill.

If you need to associate an account, then the value you are looking for is accessToken and lives in the same user object after a successful link to the account. According to the same JSON Reference as above:

accessToken: a token that identifies a user on another system. This is only provided if the user has successfully linked their account. See Linking an Alexa user to a user in your system for more information.

+10
source

You should try to read the request that appears in the ASK test console. From there, you can access the various variables that are sent to your lambda function. In addition, you can manipulate or use them according to your requirements.

0
source

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


All Articles