What Json response format to use for v2beta1 fulfi

2019-02-21 05:18发布

问题:

I have successfully tested my agent intents in the Dialogflow console where my fulfillment webhook gives responses such as:

{
  speech: 'You have 4 items, aaaa, bbbb, cccc, dddd ',
  displayText: 'You have 4 items, aaaa, bbbb, cccc, dddd ',
  data: {},
  contextOut: [],
  source: 'xxx:' 
}

Now I have set Dialogflow V2 API (v2beta1, I think).

I get a response:

"Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: speech in message google.cloud.dialogflow.v2beta1.WebhookResponse".

What JSON response structure/format do I need to use? The documentation on this is not at all clear. Can someone point me to the correct page - or describe it here.

If I do a search for "dialogflow v2beta1 fulfillment json response format", one entry seems to be promising: Dialog Flow WebhookResponse

But I cannot seem to find any reference to the field named "speech" (as per the error message).

回答1:

It would be helpful to look at the reference documentation for this API.

{
  "fulfillmentText": string,
  "fulfillmentMessages": [
    {
      object(Message)
    }
  ],
  "source": string,
  "payload": {
    object
  },
  "outputContexts": [
   {
      object(Context)
   }
  ],
  "followupEventInput": {
    object(EventInput)
  },
}

Instead of "speech", you probably are looking for fulfillmentText. It also seems like other fields in your response would not match either, so you should refer to the reference docs above to determine how to restructure your payload to match the required API.



回答2:

Hi So I was facing a similar problem while trying to setup a webhook for my dialogueflow v2 beta . It took me hours of google search to understand the error. The error means that the field you have sent donot match the possible Message Fields you can pass inside fullfillment messages.

This is my cloud function that I m using for webhook and it works perfectly for v2. Follow the below link for the format of messages that can be part of the fulfillmentMessages array

Message Format for fulfillmentMessages

app.post('/v2/Hello',(req,res)=>{
let response = "This is a sample response from your webhook!";//Default response from the webhook to show it’s working
let responseObj={
     "fulfillmentText":response
    ,"fulfillmentMessages":[
        {
            "text": {
                "text": [
                    "Hello I m Responding to intent"
                ]
            }
        }
    ]
    ,"source":""
}
return res.json(responseObj);});

The Below code works for v1

app.post('/Hello',(req,res)=>{

let response = 'This is a sample response from your webhook!' //Default response from the webhook to show it’s working

res.setHeader('Content-Type', 'application/json'); //Requires application/json MIME type

return res.send(JSON.stringify({ "speech": response, "displayText": response}));});

This works perfectly for v1



回答3:

Some things have changed for v2, and they're very particular about the format to return to dialogflow because they take it and insert it in the RAW API response json. I ran into this issue and found the template responses in the documentation here very useful.