0
votes

I have a dialogflow intent and I want to write a javascript code to get json response of this intent and show it in an html file. I used webhook fulfillment for this intent and now I want to show chat history between dialogflow and user in an html file. I used this javascript code to extract and have dialogflow response:

baseUrl = "https://api.dialogflow.com/v1/",
$.ajax({
    type: "POST",
    url: baseUrl + "query",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    headers: {
      "Authorization": "Bearer " + accessToken
    },
    data: JSON.stringify({query: text, lang: "en", sessionId: "b1973977-20be-4cdc-85a5-7f4225cfdb5a"}),

    success: function(data) {
      console.log("data",data);
      prepareResponse(data);
    },
    error: function() {
      respond(messageInternalError);
    }
  });
}

But the json it returns me is incorrect and has no fulfillment part in order to extract correct data and show response.

The returned json is :

id: "a0114c2a-afdb-415e-af08-e7a2c5b7d925"
lang: "en"
result: {…}
action: ""
metadata: Object { intentName: "Edit.Attributes", intentId: "9601923d-596b-44df-80de-92dff61869cf", webhookUsed: "true", … }
parameters: Object { VarName: […], percentage: "20%", currncy: "", … }
resolvedQuery: "Change the attribute vacancy rate to 20%"
score: 1
source: "agent"
speech: ""
__proto__: Object { … }
sessionId: "b1973977-20be-4cdc-85a5-7f4225cfdb5a"
status: Object { code: 200, errorType: "success", webhookTimedOut: false }
timestamp: "2018-01-24T12:35:46.342Z"
2
Have you tried anything yet? This is a pretty broad question - narrowing down what you're trying to do, what you've tried, and what isn't working would help us help you greatly. - Prisoner
Please update your question. Trying to read through code in comments is very difficult. - Prisoner
And explain what you mean by "didn't respond correctly". What did it respond with? What did you expect? - Prisoner
I have updated the main question now.Sorry for being unclear. - we.are
That doesn't look like a JSON. More like an object dump. In your code, try printing this with something like console.log('data',JSON.stringify(data,null,1)) - Prisoner

2 Answers

0
votes

In my case, this was the solution:

Putting :

url: baseUrl + "query?v=20170712",

instead of :

url: baseUrl + "query",

in ajax call,solved my problem.

0
votes

The dialogflow response is contained in speech and displayText propery.