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

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

我一直将我的会话变量保存为 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)。两者似乎都一样,但对我不起作用。

尽管他们在文档中使用了术语 lifespanCount,但图书馆在使用 agent.context.set()

时只是称其为 lifespan

尝试

conv.data = sessionVars;

设置变量并在其他意图中使用 conv.data 访问它们。对话结束时数据将丢失。如果您需要在对话中保留数据,请使用 conv.user.storage。