我做了几个函数,可以帮助我更改DialogFlow中的上下文。由于某种原因,上下文会按原样进行更改,但是只有一次,当我查看日志时,会看到此错误消息。
(node:4) UnhandledPromiseRejectionWarning: Error: 3 INVALID_ARGUMENT: com.google.apps.framework.request.BadRequestException: [ResourceName error] Path '' does not match template 'projects/{project_id=*}/locations/{location_id=*}/agent/environments/{environment_id=*}/users/{user_id=*}/sessions/{session_id=*}/contexts/{context_id=*}'.
我将在这里留下我的代码:
const dialogflow = require('dialogflow');
const { struct } = require('pb-util');
const credentials = {
client_email: config.GOOGLE_CLIENT_EMAIL,
private_key: config.GOOGLE_PRIVATE_KEY,
};
const sessionClient = new dialogflow.SessionsClient(
{
projectId: config.GOOGLE_PROJECT_ID,
credentials
}
);
const contextsClient = new dialogflow.ContextsClient(
{
projectId: config.GOOGLE_PROJECT_ID,
credentials
}
);
这些是我的代码需要的一些const和包,这是我的代码:
async function createContext(sender, contextId, parameters, lifespanCount = 1) {
console.log("Sender antes: "+sender+" "+sessionIds.get(sender));
if (!sessionIds.has(sender)) {
sessionIds.set(sender, uuid.v1());
}
console.log("Sender despues: "+sender+" "+sessionIds.get(sender));
const sessionPath = contextsClient.sessionPath(
config.GOOGLE_PROJECT_ID,
sessionIds.get(sender)
);
const contextPath = contextsClient.contextPath(
config.GOOGLE_PROJECT_ID,
sessionIds.get(sender),
contextId
);
const request = {
parent: sessionPath,
context: {
name: contextPath,
parameters: struct.encode(parameters),
lifespanCount
}
};
const [context] = await contextsClient.createContext(request);
return context;
}
function sendQuery(sender, query, context) {
const session = sessionClient.sessionPath(
config.GOOGLE_PROJECT_ID,
sessionIds.get(sender)
);
const request = {
session:session,
queryInput: {
text: {
text: query,
languageCode: config.DF_LANGUAGE_CODE
}
},
queryParams: {
contexts: [context] // You can pass multiple contexts if you wish
}
};
return sessionClient.detectIntent(request);
}
第二次我想更改上下文,将错误更改为此:
(node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2020-06-06T12:31:33.177523+00:00 app[web.1]: (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
谢谢,
乔纳森·普列托
我有点菜鸟,所以要加一点盐:
看来您的连接请求失败(第一个错误说路径为'),这导致承诺被拒绝,这就是为什么您包含的第二个错误抱怨使用.catch()的原因。为此,也许使用try或catch / catch块可能会有用,但是我think问题的根源可能是所使用的路径不正确。
也许注销您要从中请求的URI以确保路径匹配