简体   繁体   English

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

[英]Saving session data for Google Actions with Dialogflow and WebhookClient (v2 API)

I had been saving my session variables as contexts , and it had been working fine in previous releases. 我一直将会话变量保存为context ,并且在以前的发行版中一直运行良好。

This time around, when I checked, this code had stopped working. 这次,当我检查时,此代码已停止工作。

My package.json has dependencies 我的package.json有依赖关系

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

Then, my code goes like 然后,我的代码就像

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));
      }

The context is getting set in welcome(). 上下文在welcome()中设置。 I am able to retrieve it in startLearning(), and printing the sessionContext into the conversation after update also shows me the data was there. 我可以在startLearning()中检索到它,并在更新后将sessionContext打印到对话中,这也向我显示了数据。 Then, I update the agent context as sessionContext. 然后,我将代理上下文更新为sessionContext。

In the next intent of the flow - repeatWord (currently, having only code required for debugging), I do not get the updated context value. 在流程的下一个意图中-repeatWord(当前,仅具有调试所需的代码),我没有获得更新的上下文值。

I have tried both agent.setContext(sessionContext) and agent.context.set(sessionContext). 我已经尝试了agent.setContext(sessionContext)和agent.context.set(sessionContext)。 Both, seem to work the same, and do not work for me. 两者似乎都一样,对我不起作用。

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

try 尝试

conv.data = sessionVars;

to set the variables and access them using conv.data in other intents. 设置变量并以其他目的使用conv.data访问它们。 data will be lost when the conversation ends. 对话结束时,数据将丢失。 use conv.user.storage if you need to persist data across conversations. 如果您需要在整个对话中保留数据,请使用conv.user.storage。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM