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

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

    The below snippet explains how we can communicate from a webapp to dialogflow NLU. To get access token we can make use of Google cloud SDK to obtain the token, but it has one hour validity so having this in a different service and obtain it before making call to dialogflow will give a workaround.

    $(document).ready(function(){

    $("button").click(function(){
    
        $.ajax({
            type: "POST",
            url: "https://dialogflow.googleapis.com/v2/projects/YOUR-PROJECT-NAME/agent/sessions/SESSIONIDOFYOURCHOICE:detectIntent",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            headers: {
                "Authorization": "Bearer " + "YOUR ACCESS TOKEN GOES HERE",
            },
            data: JSON.stringify({ "queryInput":{
                "text":{
                    "text":"YOUR QUERY TO NLU",
                    "languageCode":"en-US"
                }
            } }),
            success: function(data) {
                $("#div").html(data.queryResult.fulfillmentText);
            },
            error: function() {
                console.log("Internal Server Error");
            }
        });     
    
    });
    

    });

提交回复
热议问题