问题
I am using dialogflow
NPM module and I want to send input/output context
but I am not sure how to do.
I know I can do in google-assistant
NPM with
I can set contexts
with parameter
using below method,
const parameters = { // Custom parameters to pass with context
welcome: true,
};
conv.contexts.set('welcome-context', 5, parameters);
回答1:
In order to send a context using the Dialogflow NPM module, you have to first create a context, using dialogflow.ContextsClient
and then send it on the query.
To convert the parameters to the format required by Dialogflow you will need to use this module: pb-util
const dialogflow = require('dialogflow');
const { struct } = require('pb-util');
const projectId = 'projectId';
const contextsClient = new dialogflow.ContextsClient();
const sessionClient = new dialogflow.SessionsClient();
async function createContext(sessionId, contextId, parameters, lifespanCount = 5) {
const sessionPath = contextsClient.sessionPath(projectId, sessionId);
const contextPath = contextsClient.contextPath(
projectId,
sessionId,
contextId
);
const request = {
parent: sessionPath,
context: {
name: contextPath,
parameters: struct.encode(parameters)
lifespanCount
}
};
const [context] = await contextsClient.createContext(request);
return context;
}
function sendQuery(sessionId, query, context) {
const session = sessionClient.sessionPath(projectId, sessionId);
const request = {
session,
queryInput: {
text: {
text: query
}
},
queryParams: {
contexts: [context] // You can pass multiple contexts if you wish
}
};
return sessionClient.detectIntent(request);
}
(async() => {
const parameters = { // Custom parameters to pass with context
welcome: true
};
const sessionId = 'my-session';
const context = await createContext(sessionId, 'welcome-context', parameters);
const response = await sendQuery(sessionId, 'Hi', context);
console.log(response);
})();
Have in mind that you send an input context. The output context is generated in the intent.
If you have trouble authenticating the clients SessionClient
& ContextClient
you can check this other question: Dialogflow easy way for authorization
回答2:
First, some clarification on packages
- The google-assistant package is one person's implementation/wrapper for the Assistant SDK, allowing you to embed the Assistant in any program or device. This is like building your own Google Home. (This package isn't from Google, it appears, but does wrap the official protobuf API, so you don't have to compile it yourself.)
- The actions-on-google package is for making a webhook that works either with the Action SDK or Dialogflow. This is for making a program that works with the Assistant and sends back responses. (ie - it is for writing the code that responds to "Hey Google, talk to my app")
- The dialogflow package lets you work with Dialogflow agents, including configuring them and sending queries to them to see which Intents match the queries. This is a very rough equivalent of the "google-assistant" package, but does a lot more.
- The dialogflow-fulfillment package is for making a webhook that provides logic and responses for any of the platforms that Dialogflow supports. It is the equivalent of the actions-on-google package.
The code fragment you provided looks like it is from the "actions-on-google" package, and is setting the output context as part of the reply.
The equivalent of your code using the "dialogflow-fulfillment" package would be
const parameters = { // Custom parameters to pass with context
welcome: true,
};
agent.context.set('welcome-context', 5, parameters);
Note that it is "context", without the "s" in this package.
Similarly, to get an input context, you would use
agent.context.get('name-of-context;);
(You may see some examples that use something like agent.setContext()
. These methods have been deprecated in favor of the above.)
来源:https://stackoverflow.com/questions/54380112/set-input-or-output-context-dialogflow-nodejs-v2