How to make http call on DialogFlow v2 using Javascript ajax call

后端 未结 6 441
时光说笑
时光说笑 2020-12-19 11:17

I found this example on the official site of DialogFlow using Node.js and it is working fine, but I dont know how do I integrate this into my web application.

Is it

6条回答
  •  忘掉有多难
    2020-12-19 11:47

    I've found a way to get a valid access-token entirely from frontend (browser) JavaScript.

    1) Install the npm package jsrsasign from npm.

    2) Add the files in this Gist to your project: https://gist.github.com/Venryx/bfa9b69583638dfbf611a9f292721a75

    3) Enter values for all of the constants at the start of DialogFlowExample.ts.

    Your service account information can be found at: https://console.cloud.google.com/iam-admin/serviceaccounts

    The service account's email is listed in the table, and its private-key (if you've created one) is found within the downloaded key json file. If you don't remember where you placed the key json file (or you haven't created one yet), you can use "Actions->Create key" to create a new one.

    4) Call GetAccessToken with your claim set and private key (see Gist for example). It will return a promise.

    5) Once the access-token is retrieved, the promise is resolved with the retrieved access-token. (The function caches the access-token for 60 minutes. After that period, the token is expired, so calling the function again will have it retrieve a new one.)

    Example

    The linked Gist contains an almost-complete usage example for converting a segment of audio to text using DialogFlow. The only part it doesn't show is how to obtain the "segment of audio".

    For that, see part 1 of my answer here: https://stackoverflow.com/a/57859776/2441655

提交回复
热议问题