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 SESSION 16: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 stuff 16: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