What Json response format to use for v2beta1 fulfillment webhook?

半腔热情 提交于 2019-12-04 02:27:42

问题


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.




回答4:


v1

var chat="success response";
  response.setHeader('Content-Type','application/json');
  response.send(JSON.stringify({"speech":chat,"displayText":chat}));

v2

var chat="success response";
  response.setHeader('Content-Type','application/json');
  response.send(JSON.stringify({"fulfillmentText":chat}));

speech---->fulfillmentText

displayText--->fulfillmentMessages

Dialog Flow Response has been changed from v1 to v2 Please refer the Dialog flow documentation for more details click here



来源:https://stackoverflow.com/questions/48211405/what-json-response-format-to-use-for-v2beta1-fulfillment-webhook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!