Node.js:如何获得Alexa会话ID和用户ID



我正在构建Alexa技能,它需要将userID和sessionID保存到数据库中。对于数据库,我使用MongoDb。到目前为止,我已经使用handlerInput.requestEnvelope.session.sessionID来获取sessionID,并且要获取userID,我正在应用handlerInput.requestEnclose.session.userID。但是,我收到了会话和用户ID的未定义值。以下是代码:

const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
async handle(handlerInput) {
const speakOutput = 'Hi, I am Nao. I am here to give you counseling on your anxiety issues. Can I have your name, please? Note: We are not professional therapists or counselors. ';

//These two lines of code is for testing purpose//
const useri=handlerInput.requestEnvelope.session.user.userID;
const g=handlerInput.requestEnvelope.session.sessionID;


const curr_session=new post.session({
alexa_sessionid:handlerInput.requestEnvelope.session.sessionID
});

let user=await post.findOne({userID:handlerInput.requestEnvelope.session.user.userID});
if(!user){
user=new post({
userID:handlerInput.requestEnvelope.session.user.userID
});

}
user.session_list.push(curr_session);
user.save();



return handlerInput.responseBuilder
.speak(speakOutput+g)
.reprompt(speakOutput)
.getResponse();
}
};

如何使用handlerInput获取Alexa会话和UserId?

如果您将代码与实际的JSON请求进行比较,可能不容易发现您的技能,但错误是您的大写拼写为"ID";以元素的名称("userID"、"sessionID"(。该请求使用骆驼式案例:";userId";以及";sessionId";。

如果您相应地替换代码中的引用,则应该获得值。

最新更新