【发布时间】:2021-07-13 20:11:04
【问题描述】:
我正在构建一个 Alexa 技能,它需要将用户 ID 和会话 ID 保存到数据库中。对于数据库,我使用的是 MongoDb。到目前为止,我已经使用 handlerInput.requestEnvelope.session.sessionID 来获取 sessionID,并且为了获取 userID,我正在应用 handlerInput.requestEnvelope.session.user.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?
【问题讨论】:
标签: node.js mongodb alexa-skill