首页 > 解决方案 > DialogFlow v2 错误:[ResourceName 错误] 路径 '' 与模板不匹配

问题描述

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

这些是我的代码需要的一些常量和包,这是我的代码:

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.

谢谢,

乔纳森·普列托

标签: javascriptnode.jsdialogflow-esfacebook-messenger-botfacebook-chatbot

解决方案


我有点菜鸟,所以把这个和一粒盐一起吃:

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

也许注销您尝试请求的 URI 以确保路径匹配


推荐阅读