如何获取 Alexa 用户 ID?

How to get an Alexa userId?

我正在构建 Alexa 技能,它需要我存储用户的 userId。我试图用 event.session.user.userId 检索它。但是,当我调用 console.log(event.session.user.userId) 时,输出实际上是 amzn1.ask.account.[unique-value-here]。我看过几个类似的问题,其中 none 个为我提供了足够明确的答案。

我不确定这是一个错误、仅限开发人员的问题,还是 userId 只是匿名化了。如果是这样,有没有办法获得实际的 userId?我想会有,因为亚马逊在这里写了完整的指南:

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

然而,经过一整天的调试,我不确定什么是真实的,什么不是。

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
}

以下是 CloudWatch 日志中的输出:


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

你做得对。这个 amzn1.ask.account.[unique-value-here] 实际上是完整的 userId。您可以通过从 Echo 启用您的技能,将多个请求记录到您的 alexa 技能,并观察这些请求之间的 userId 是相同的值来自己观察这一点。

根据 JSON Reference:

userId: A string that represents a unique identifier for the user who made the request. The length of this identifier can vary, but is never more than 255 characters. The userId is automatically generated when a user enables the skill in the Alexa app.

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

如果您只需要在会话之间保留用户属性,这个值就足够了,只要他们启用了技能,您就可以使用它来唯一标识此用户。

如果您需要 link 一个帐户,您正在寻找的值是 accessToken 并且存在于成功帐户 link 之后的同一个 user 对象中。按照与上面相同的 JSON 参考:

accessToken: a token identifying the user in another system. This is only provided if the user has successfully linked their account. See Linking an Alexa User with a User in Your System for more details.

好吧,事实证明我做的每件事都是正确的(一次)。 userId 字面意思是 amzn1.ask.account.[unique-value-here] 的原因是因为我在 AWS Lambda 控制台的“Alexa Start Session”测试事件中对其进行了测试。当我要求我的 Echo Dot 启动该技能时,它生成了实际的密钥。问题已解决。

您应该尝试阅读显示在 ASK 测试控制台中的请求。从那里,您可以访问发送到您的 lambda 函数的不同变量。此外,您可以根据需要操作或使用它们。