Thi's avatar
HomeAboutNotesBlogTopicsToolsReading
About|My sketches |Cooking |Cafe icon Support Thi
πŸ’Œ [email protected]

Google Dialogflow APIs and other services of Google

Anh-Thi Dinh
GoogleAPI & ServicesJavaScript
Left aside
Google's documentation is like an ocean. It's not easy to find a right one to start. This note contains only basic things that I've already worked with. Try your own hand at Google's APIs will help you understand more.
πŸ‘‰ Github repo: dinhanhthi/google-api-playground
☝️
This note is mainly for Dialogflow APIs. However, I mention also the other services of Google.

Official documentation

Some services

  • Dialogflow: SDK (other official site) | REST API
  • Manage projects: Resource Manager SDK (other official site) | REST API
    • Note: From version 2.0.0, resource-manager change the package to @google-cloud/resource-manager. And from version 3.0,0, the "folder" was introduced!
  • Active API on some projects: Service Usage SDK | REST API
    • Note: APIs can only be activated for Projects, neither folder nor organization!
⚠️
When you use any NodeJS service, for example,
The opts is described here.

Wanna run the Node.js samples?{:#run-samples}

πŸ‘‰ Link of all (dialogflow nodejs) samples on github.
⚠️
The old version uses dialogflow and @type/dialogflow. The new version uses only one @google-cloud/dialogflow!
⚠️
On SDK documentation, they don't mention about the location in the parent property. For example, they say "parent can be projects/<Project ID or '-'>", but you can add the location information inside it like that
☝️
You have to enable the Dialogflow API in the current project. Other wise, we meet below problem.
You can use Service Usage to enable a service on some project programmatically or go to beflow url to active it visually,

Wanna try gapi (JS client)?

⚠️
Google has announced that they will be discontinuing the Google Sign-In JavaScript Platform Library for web. You will have to switch to using Google Identity Services (or Sign In With Google or gsi). The old service will be completely discontinued on March 31, 2023.
What's this gapi? You can use it completely inside an HTML file without using any backend.
πŸ‘‰ List of samples (google apis javascript client).
πŸ‘‰ You have to use REST API in this case.
☝️
Tip: If you are using VSCode, you can install the Live Server extension to quickly create a server (port 5500 by default). Just click the button at the bottom right of the file and a website will appear.

The corresponding between REST API and Node.js clients

πŸ‘‰ REST API.
πŸ‘‰ Node.js SDK.
⚠️
Sometimes, the location infotmation is mentionned in the REST API but not in the SDK. For example, load the list of agents w.r.t. some location
  • REST (different with the general case): this link.
  • SDK (the same as the general case), in this, we just need to add "location" into the agent property, like

Using REST API in Node.js

  • Normally, it's easier if we use Node.js SDK.
  • First, you need to create a Service Account, then create a key and download the JSON file. Follow these steps.
  • Check an example code.
  • Remark: the code above use request which has been already deprecated! You can choose any alternative.
⚠️
Remark: With keys generated from a service account (stored in JSON), we can only get 1 agent for methods like projects.agent.search although it says that we can get a list of agents. In this case, try to get access token via OAuth 2.0. With this, we can access to all projects instead of only 1 (which is used to generate the json file).

Dialogflow REST APIs with Postman

πŸ‘‰ Check this official guide.

Location problem

πŸ‘‰ List of endpoints (containing location inside).
πŸ‘‰ LIst of location information you can use with Dialogflow.
You have to use the same endpoint and location information in the API/SDK. If you use them differently, for example,
You will meet an error like below,
In the SDK documention, they don't mention about the location you need to use in the agent property. For example, they say "parent can be projects/<Project ID or '-'>", but you can add the location information inside it like that
πŸ’‘They are the same (for endpoints):

Custom roles

πŸ‘‰ Create here (then you need to choose a project / a folder / an organization)
πŸ‘‰Β Understanding roles.
  • If you choose a project, there are some roles cannot be added because they belongs to folders/organization (eg. resourcemanager.projects.list)
  • (ref) You have to pay (using Google Workspace or Google Identity) to create an organization or a folder.
☝️
If you already have a domain, you can try to create a Google Workspace and use it in 1 month of trial. After that, you can deactivate the Google Workspace account but the things on the Google Cloud Platform are still there and the organization you have created in Google Workspace is still working!
In case you wanna some tasks + don't wanna create a custom role (don't forget to check this list):

Service Account w.r.t Organization / Folder

πŸ‘‰ Create a service account. (and also the key for it > then download the JSON file)
  • By default, a service account can only access to the current project (even if you choose the roles which created in the organization/folder).
  • If you want that service account can perform tasks in the organization/folder, you have to go to IAM & Admin > IAM > choose the organization / folder (yes, IAM is different for different project/organization/folder) > ADD > add the email of the service account to New principals and choose a role for it.
☝️
If you use SDK listProjectsAsync to list all projects in the organization, you can only list the projects in that organization, not the ones inside a folder even if that folder belongs to the organization.
☝️
Quota: For each Service Account, the limit of number of projects you can create is 22. You can ask more but the better idea is to create another service account (and don't forget to remove the old one with its credentials).

Types for TypeScript

⚠️
The old version uses dialogflow and @type/dialogflow. The new version uses only one @google-cloud/dialogflow!
For example, the returned type for getAgent() method can be defined as,

Play with Intents, Examples and Entities

🎯 Aim: Suppose that we want to create a new entities via API (including the system entities).
  • List all intents from some agent: just put in the parent something like projects/<projectId>/agent and then click Execute. Don't forget to choose the intentView as INTENT_VIEW_FULL (without this one, you cannot see the examples or "trainingPhrases") πŸ‘ˆ Copy and remember the output of type Intent.
  • Create a new entities using this API. Again, put in parent as projects/<projectId>/agnet and then in the Request body, paste an object which looks like the output in the previous step (remember to remove "name" sections, this section will be created by DF), then Execute.
β—†Official documentationβ—†Some servicesβ—†Wanna run the Node.js samples?{:#run-samples}β—†Wanna try gapi (JS client)?β—†The corresponding between REST API and Node.js clientsβ—†Using REST API in Node.jsβ—†Dialogflow REST APIs with Postmanβ—†Location problemβ—†Custom rolesβ—†Service Account w.r.t Organization / Folderβ—†Types for TypeScriptβ—†Play with Intents, Examples and Entities
About|My sketches |Cooking |Cafe icon Support Thi
πŸ’Œ [email protected]
1const client = new library.SampleClient(opts?: ClientOptions);
1const parent = (location) => "projects/-" + "/locations/" + location;
17 PERMISSION_DENIED: Dialogflow API has not been used in project 284022294153 before or it is disabled. Enable it by visiting <https://console.developers.google.com/apis/api/dialogflow.googleapis.com/overview?project=284022294153> then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
1<https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com/overview?project=><projectId>
1<!-- OLD -->
2<script src="<https://apis.google.com/js/platform.js>" async defer></script>
3
4<!-- NEW -->
5<script src="<https://accounts.google.com/gsi/client>" async defer></script>
1const parent = (location) => "projects/-" + "/locations/" + location;
1<https://global-dialogflow.googleapis.com/v2/projects/-/locations/europe-west1/agent:search>
1"Please switch to 'europe-west1-dialogflow.googleapis.com' to access resources located in 'europe-west1'."
1const parent = (location) => "projects/-" + "/locations/" + location;
1dialogflow.googleapis.com
2us-dialogflow.googleapis.com
3global-dialogflow.googleapis.com
1export type Agent = dialogflow.protos.google.cloud.dialogflow.v2.Agent;