提问者:小点点

DialogFlow v2错误:[ResourceName error]路径“”与模板不匹配


我制作了几个函数来帮助我改变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
    }
);

这些是我的代码需要的一些consts和包,下面是我的代码:

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.

谢谢,

乔纳森·普列托


共1个答案

匿名用户

我是一个小笨蛋,所以对此持保留态度:

看起来您的连接请求失败了(第一个错误显示路径为''),这导致promise被拒绝,这就是为什么您包含的第二个错误是抱怨使用。catch()的原因。 也许使用that或try/catch块会对此有用,但我认为问题的根源可能是使用的路径不正确。

可能会注销您试图请求的URI,以确保路径匹配