0

I have made a couple of functions that help me change the context in DialogFlow. For some reason, the context is changed as it should, but only one time, and when I look at my logs, is see this error message.

(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=*}'.

Here I will leave my code:

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
    }
);

These are some consts and packages need for my code, and here is my code:

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);
}

The second time I want to change the context the error change to this one:

(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.

Thank you,

Jonathan Prieto

Jonathan Prieto
  • 233
  • 4
  • 14

1 Answers1

0

I'm a bit of a noob so take this with a grain of salt:

It looks like your connection request is failing (first error says path is ' ') which is causing the promise to reject, which is why the second error you included is complaining about using .catch(). Perhaps using that or a try/catch block would be useful to that end, but I think the root of the issue might be that the path used isn't correct.

Perhaps log out the URI you are attempting to request from to ensure the path matches

stoneb
  • 53
  • 1
  • 5
  • I am agree with your comment @stoneb, but which path is empty ? because both sessionclient and context cliente are here (I logged them): SessionPath: projects/cli*/agent/sessions/d*, ContextPath: projects/cli*/agent/sessions/d*/contexts/telefono – Jonathan Prieto Jun 06 '20 at 13:26