简体   繁体   English

如何从 node.js 对话流包设置对话流上下文

[英]How to set dialogflow context from node.js dialogflow package

I am using sessionClient.detectIntent() to send text to dialogflow and it is working fine, now I want to send context too with the text.我正在使用sessionClient.detectIntent()将文本发送到 dialogflow 并且它工作正常,现在我也想用文本发送上下文 How can I do that?我怎样才能做到这一点?

I usually create contexts in the following way:我通常通过以下方式创建上下文:

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

And simply call this method when I have to create a context before I call the detectIntent method:当我必须在调用 detectIntent 方法之前创建上下文时,只需调用此方法:

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

You can send contexts on detectIntent alongside with the Query Text by adding it on the context array field under queryParams .您可以发送 背景detectIntent通过增加它在上下文数组字段旁边与查询文本queryParams Note that this method of sending context via detectIntent will create (if not created) and activates the context before the query is executed.请注意,此通过detectIntent 发送上下文的方法将在执行查询之前创建(如果未创建)并激活上下文。

You can refer to the code snippet below:你可以参考下面的代码片段:

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

Output:输出:

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}]

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

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