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

后端 未结 6 417
时光说笑
时光说笑 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:55

    https://dialogflow.com/docs/reference/v2-auth-setup

    After following through the page above and created service keys, you must have a json file like this:

    {
        "type": "",
        "project_id": "",
        "private_key_id": "",
        "private_key": "",
        "client_email": "",
        "client_id": "",
        "auth_uri": "",
        "token_uri": "",
        "auth_provider_x509_cert_url": "",
        "client_x509_cert_url": ""
      }  
    

    With this file, execute these two commands (you need to install gcloud CLI if have not yet):

    gcloud auth activate-service-account --key-file=
    gcloud auth print-access-token
    

    After those, copy the access token and supply it to your program as an environment variable (or hardcode if you don't care). Then, you just make an HTTP post request. I use axios in my React project, here is a sample code for detectIntent:

    import axios from "axios";
    
    const DIALOG_FLOW_TOKEN = process.env.REACT_APP_DIALOG_FLOW_TOKEN;
    const DIALOG_FLOW_API_ROOT_URL = "https://dialogflow.googleapis.com/v2";
    const YOUR_PROJECT_ID = ;
    const SESSION_ID = ;
    const URL = `${DIALOG_FLOW_API_ROOT_URL}/projects/${YOUR_PROJECT_ID}/agent/sessions/${SESSION_ID}:detectIntent`;
    
    var config = {
        headers: {
            "Authorization": "Bearer " + DIALOG_FLOW_TOKEN,
            "Content-Type": "application/json"
        }
    };
    
    export function sendText(text) {
        var bodyParameters = {
            "queryInput": { "text": { "text": text, "languageCode": "en" } },
        };
    
        const request = axios.post(
            URL,
            bodyParameters,
            config
        );
    
        return request;
    } 
    

提交回复
热议问题