【问题标题】:Node.js : How do I get an Alexa sessionId and userID?Node.js:如何获取 Alexa sessionId 和 userID?
【发布时间】: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


    【解决方案1】:

    如果您将代码与您的技能的实际 JSON 请求进行比较,可能不容易发现,但错误是元素名称中“ID”的大写拼写(“userID”、“sessionID” )。 请求使用驼峰式大小写:“userId”和“sessionId”。

    如果您相应地替换代码中的匹配项,您应该会得到这些值。

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多