如何获取 Alexa 用户 ID? [英] How to get an Alexa userId?

查看:21
本文介绍了如何获取 Alexa 用户 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building an Alexa Skill, and it requires that I store the userId of a user. I've tried to retrieve it with 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've looked at several similar questions, and none of them provide a clear enough answer for me.

我不确定这是否是错误、仅限开发人员的事情,或者 userId 是否只是匿名的.如果是这样,有没有办法获得实际的用户 ID?我想会有,因为亚马逊在这里写了一个完整的指南:

I'm not sure if this is a bug, a developer-only thing, or if the userId is simply anonymized. If so, is there a way to get the actual userId? I imagine there would be, since Amazon has written an entire guide on it here:

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

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

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

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 日志的输出:

The following is the output on the 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

推荐答案

好吧,事实证明我做的一切都是正确的(一次).userId 实际上是 amzn1.ask.account.[unique-value-here] 的原因是因为我在Alexa 启动会话"上测试它.AWS Lambda 控制台中的测试事件.当我让 Echo Dot 启动该技能时,它生成了实际的密钥.问题解决了.

Well, turns out I was doing everything correctly (for once). The reason why the userId was literally amzn1.ask.account.[unique-value-here] was because I was testing it on a "Alexa Start Session" test event in the AWS Lambda console. When I asked my Echo Dot to launch the skill, it generated the actual key. Problem solved.

这篇关于如何获取 Alexa 用户 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆