使用Dialogflow和WebhookClient(v2 API)保存Google动作的会话数据

时间:2018-10-31 14:06:00

标签: actions-on-google dialogflow

我一直将会话变量另存为contexts,并且在以前的发行版中它可以正常工作。

这一次,当我检查时,此代码已停止工作。

我的package.json具有依赖项

"dependencies": {
    "actions-on-google": "^2.4.0",
    .
    .
    .
    "dialogflow-fulfillment": "^0.6.0"
  }

然后,我的代码就像

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    const agent = new WebhookClient({ request, response });


    // Handle the Dialogflow intent named 'welcome'.
    function welcome(agent) {
          const sessionVars = {'userLang': 'en',  // possibilites handled - 'en', 'hi'
                          'words': [],
                          'questions': [],
                          'currentIndexPosition': 0,
                          'score': 0,
                        };

          const sessionContext = {'name': KEY_SESSION, 'lifespanCount': 10000, 'parameters': sessionVars};
          agent.setContext(sessionContext);
        .
        .
        .
        //Rest of the code
        }


        // Start a new round of learning by selecting words
        function startLearning(agent) {

        let sessionContext = agent.getContext(KEY_SESSION);
        let sessionParams = sessionContext.parameters;
        let conv = agent.conv();

        conv.ask('Awesome! Let\'s learn a new word. ');
        database_path = DATABASE_PATH_LEARNING_EN;

        return firebaseAdmin.database().ref(database_path)
          .once('value')
          .then( (data) => {

              // --- Code that reads data from database and stores in variable words ---

              // Update session data
              sessionParams.words = utils.getRandomItemList(words, QUESTIONS_PER_PRACTICE);
              let currentIndexPosition = 0;
              sessionParams.currentIndexPosition = currentIndexPosition;

              // --- printing sessionContext value shows correctly updated data here ---    

              // Update session data
              agent.setContext(sessionContext);
            } else {
              conv.close('No words to learn.');
            }
            agent.add(conv);
            return agent;
        });
       }

      // Repeat the word for learning
      function repeatWord(agent) {
        let sessionContext = agent.context.get(KEY_SESSION);
        let sessionParams = sessionContext.parameters;
        agent.add(JSON.stringify(sessionContext));
      }

上下文已在welcome()中设置。我可以在startLearning()中检索到它,并在更新后将sessionContext打印到对话中,这也向我显示了数据。 然后,我将代理上下文更新为sessionContext。

在流程的下一个意图中-repeatWord(当前,仅具有调试所需的代码),我没有获得更新的上下文值。

我已经尝试了agent.setContext(sessionContext)和agent.context.set(sessionContext)。两者似乎都一样,对我不起作用。

2 个答案:

答案 0 :(得分:0)

尽管他们在文档中使用术语lifespanCount,但在使用lifespan时库仅调用了agent.context.set()

答案 1 :(得分:0)

尝试

git rm --cached

设置变量并以其他目的使用conv.data访问它们。对话结束时,数据将丢失。如果您需要在会话之间持久保存数据,请使用conv.user.storage。