Home > Software engineering >  Update entities in dialogflow es using nodejs or reactjs
Update entities in dialogflow es using nodejs or reactjs

Time:12-26

So I am working on this project where I am using a reactjs panel to create categories. Now I am trying to make the bot use this categories as an entity, I have a nodejs server that the bot uses for webhook calls, now I have 2 options to add the category as an entity. I can use the restAPI for dialogflow batchUpdate entity, but this requires authentication, I was only able to get it to work with a bearer token but those needs to be regenerated every hour (max is 12 hours) which is not possible for deployment. I also tried to send a call to my nodejs server where I tried using the actions-on-google library but again I am using the old dialogflow-fullfilment library to handle all my replies and stuff and the 2 libraries conflict. so what are my options here? is it possible to use dialogflow-fullfilment library top update entities ? or is there a way to either have a lifetime bearer token or any other auth method that google api's accept that I can use?

here is the api I used

https://dialogflow.googleapis.com/v2/projects/<project-id>/agent/entityTypes/<entity-id>/entities:batchUpdate

here is the body I send


{
"name": "projects/\<project-id\>/agent/entityTypes/\<entity-id\>",
"displayName": "cityPanel",
"kind": "KIND_MAP",
"entities": \[

        {
            "value": "Cairo",
            "synonyms": [
                "Cairo"
            ]
        }
    ]

}

this is working fine but i need to generate a new bearer token every hour which is not possible for me.

CodePudding user response:

You can use Google's official library @google-cloud/dialogflow with service account authentication.

Once you have enabled billing and Dialogflow API in google cloud platfor project, download the service account key.

Use Entity_types.batch_update_entities for batch update. Check documentation for full detail.

Here's how you initialize the client:

const dialogflow = require('@google-cloud/dialogflow');
// Create a new session
const sessionClient = new dialogflow.SessionsClient({
    keyFilename: gcpServiceAccountKeyPath,
});

CodePudding user response:

So I was able to get it working in the following way, I refered to this document throguh this google jwt via google-auth-library which then I used the following code to get the bearer token

const { JWT, GoogleAuth } = require("google-auth-library");
const keys = require("./dialogflowKey.json");

async function main() {
  const client = new JWT({
    email: keys.client_email,
    key: keys.private_key,
    scopes: ['https://www.googleapis.com/auth/cloud-platform'],
  });
  const url = `https://dialogflow.googleapis.com/v2/projects/${keys.project_id}/agent/entityTypes`;
  const res = await client.request({url});
  console.log(client.credentials.access_token); // this is the bearer token
}

main().catch(console.error);
  • Related