Nik*_*iya 1 node.js dialogflow-es
我正在使用dialogflowNPM 模块,我想发送,input/output context但我不知道该怎么做。我知道我可以在做google-assistantNPM与我可以设置contexts与parameter使用以下方法,
const parameters = { // Custom parameters to pass with context
welcome: true,
};
conv.contexts.set('welcome-context', 5, parameters);
Run Code Online (Sandbox Code Playgroud)
首先,对包的一些说明
您提供的代码片段看起来像是来自“actions-on-google”包,并将输出上下文设置为回复的一部分。
使用“dialogflow-fulfillment”包的代码相当于
const parameters = { // Custom parameters to pass with context
welcome: true,
};
agent.context.set('welcome-context', 5, parameters);
Run Code Online (Sandbox Code Playgroud)
请注意,它是“上下文”,此包中没有“s”。
同样,要获取输入上下文,您可以使用
agent.context.get('name-of-context;);
Run Code Online (Sandbox Code Playgroud)
(您可能会看到一些使用类似 的示例agent.setContext()。这些方法已被弃用以支持上述方法。)
为了使用Dialogflow NPM 模块发送上下文,您必须首先创建一个上下文,使用dialogflow.ContextsClient然后在查询中发送它。
要将参数转换为 Dialogflow 所需的格式,您需要使用此模块: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);
})();
Run Code Online (Sandbox Code Playgroud)
请记住,您发送了一个输入上下文。输出上下文是在意图中生成的。
如果您在对客户端进行身份验证时遇到问题SessionClient,ContextClient您可以查看另一个问题:Dialogflow easy way for authorization
| 归档时间: |
|
| 查看次数: |
5113 次 |
| 最近记录: |