0

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.

Bahaeldin0
  • 63
  • 8
  • 1
    Hi @Bahaeldin0, For your requirement, you can try using self-signed [JSON Web Tokens](https://cloud.google.com/docs/authentication/token-types#self-signed). You can follow the steps provide in this [document](https://cloud.google.com/iam/docs/create-short-lived-credentials-direct#sa-credentials-jwt) to create a self-signed JWT. Let me know if that helps. – Shipra Sarkar Dec 26 '22 at 10:53
  • I am going to test it and let you know thanks – Bahaeldin0 Dec 26 '22 at 12:41
  • So I was able to get a jwt to work using google-auth-library, and got the bearer token working Thanks – Bahaeldin0 Dec 26 '22 at 15:13

2 Answers2

1

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);
Bahaeldin0
  • 63
  • 8
0

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,
});
Ridham Tarpara
  • 5,970
  • 4
  • 19
  • 39
  • yeah but this method requires me to remove the old dialogflow-fulfillment library which if removed I will have to redo all the intent handling functions. Or is there a method to let them both work together? force install it via npm and then use it ? will it cause errors? – Bahaeldin0 Dec 26 '22 at 12:41
  • Both can work together. But you should switch to latest as soon as possible to avoid security and other issues. – Ridham Tarpara Dec 27 '22 at 11:34