Home > OS >  DialogFlow Cx webhook response results in "ErrorCode": "INVALID_ARGUMENT", "
DialogFlow Cx webhook response results in "ErrorCode": "INVALID_ARGUMENT", "

Time:09-27

I'm struggling to get my dialogflow Cx webhook response accepted by dialogflow. I am testing my agent from the dialogflow console and am successfully reaching my webhook endpoint and extracting the intent and switching to the correct place in my PHP code. I then build a simple text response and echo it. My response is always met with an error in the console debug window: "ErrorCode": "INVALID_ARGUMENT", "ErrorMessage": "Failed to parse webhook response:" My webhook code is:

header('Content-Type: application/json');
$jsonRequest = file_get_contents('php://input');
$data = json_decode($jsonRequest, true);
$intent = $data["intentInfo"]["displayName"];

file_put_contents("response1.log", print_r(json_encode($data), true));

switch ($intent){
      
case "myname":
$data = array (
  "fulfillment_response" => 
  array (
    "messages" => 
    array (
      0 => 
      array (
        "text" => 
        array (
          "text" => "Intent -> My name is Harry"
        ),
      ),
    ),
  )
);
}      
   echo json_encode($data);
   file_put_contents("response1.log", print_r(json_encode($data), true));
   die();

My response message json which is sent looks clean and is:

{
    "fulfillment_response": {
        "messages": [
            {
                "text": {
                    "text": "Intent -> My name is Harry"
                }
            }
        ]
    }
}

The error shown in the diloagflow console debug window is the following:

"Status": {
                "ErrorMessage": "Failed to parse webhook response: [{\"fulfillment_response\":{\"messages\":[{\"text\":{\"text\":\"Intent My name is Harry\"}}]}}]",
                "ErrorCode": "INVALID_ARGUMENT"              }
            }

OK, I have made some progress with this and the error has cleaned up considerably but it still looks like the quote characters " are being escaped \ for some reason. I have tried several flags with json_encode but to no avail. All suggestions are welcome...

CodePudding user response:

The main reason for this error is that you are returning the payload in wrong format please use the following and I hope it will work:

{
  "fulfillment_response": {
        "messages": [
            {
                "text": {
                    "text": [
                        "Webhook Response"
                    ]
                }
            }
        ]
    }
} 
  • Related