【问题标题】:How to set dialogflow context from node.js dialogflow package如何从 node.js 对话框流包中设置对话框流上下文
【发布时间】:2020-10-31 15:45:10
【问题描述】:

我正在使用sessionClient.detectIntent() 将文本发送到 dialogflow,它工作正常,现在我也想发送 context 文本。我该怎么做?

【问题讨论】:

    标签: node.js dialogflow-es dialogflow-es-fulfillment dialogflow-cx


    【解决方案1】:

    我通常通过以下方式创建上下文:

    exports.createContext = async function(contextId, parameters, sessionPath, sessionId, lifespan = 333) {
        let contextPath;
        try {
            contextPath = contextClient.contextPath(projectId, sessionId, contextId);
    
        } catch (e) {
            console.error("Error => ");
            console.error(e)
        }
        const request = {
            parent: sessionPath,
            context: {
                name: contextPath,
                parameters: struct.encode(parameters),
                lifespanCount: lifespan
            }
        };
        contextClient.createContext(request)
    };
    

    当我必须在调用 detectIntent 方法之前创建上下文时,只需调用此方法:

    bot.createContext('CONTEXT_NAME_GOES_HERE', '{PARAMETER: VALUE}', sessionPath, session_id, lifespan = 1);
    

    【讨论】:

      【解决方案2】:

      您可以在detectIntent 上将contexts 与查询文本一起发送,方法是将其添加到queryParams 下的上下文数组字段中。请注意,这种通过 detectIntent 发送上下文的方法将在执行查询之前创建(如果未创建)并激活上下文。

      可以参考下面的代码sn-p:

      const dialogflow = require('@google-cloud/dialogflow');
      
      /**
       * Send a query and a context to the Dialogflow agent, and return the query result.
       * @param {string} projectId The project to be used
       */
      async function detectIntent(projectId, sessionId, text) {
      
        // Create a new session
        const sessionClient = new dialogflow.SessionsClient();
        const sessionPath = sessionClient.projectAgentSessionPath(projectId, sessionId);
      
        // The text query request.
        const request = {
          session: sessionPath,
          queryParams:{
              //List of context to be sent and activated before the query is executed
              contexts:[
                  {
                      // The context to be sent and activated or overrated in the session 
                      name: `projects/${projectId}/agent/sessions/${sessionId}/contexts/CONTEXT_NAME`,
                      // The lifespan of the context
                      lifespanCount: 8
                    }
              ]
          },
          queryInput: {
            text: {
              // The query to send to the dialogflow agent
              text: text,
              // The language used by the client (en-US)
              languageCode: 'en-US',
            },
          },
        };
      
        // Send request and log result
        const responses = await sessionClient.detectIntent(request);
        console.log('Detected intent');
        const result = responses[0].queryResult;
        console.log(`  Query: ${result.queryText}`);
        console.log(`  Response: ${result.fulfillmentText}`);
        console.log(`  Output Contexts: ${JSON.stringify(result.outputContexts)}`)
      }
      
      detectIntent("PROJECT_ID","SESSION_ID","TEXT_QUERY");
      

      输出:

      Detected intent
        Query: Hi
        Response: Hello! How can I help you?
        Output Contexts: [{"name":"projects/PROJECT_ID/agent/sessions/SESSION_ID/contexts/CONTEXT_NAME","lifespanCount":7,"parameters":null}]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-14
        • 1970-01-01
        • 2011-02-23
        • 2015-07-01
        • 2017-01-06
        • 1970-01-01
        • 2011-10-14
        相关资源
        最近更新 更多