Correct PHP JSON Setup for DialogFlow

Paul Konstanski
Paul Konstanski used Ask the Experts™
on
I am working with DialogFlow and attempting to set up a JSON object in PHP for use with DialogFlow fulfillment. The process is explained on this page.
https://dialogflow.com/docs/reference/api-v2/rest/Shared.Types/WebhookResponse

My php script looks like this:

$myObj->fulfillmentText = "This is my test message";
$myObj->fulfillmentMessages = json_encode(array("text" => "text response")); 
$myJSON = json_encode($myObj);
echo $myJSON;
exit;

Open in new window


If I send only the first parameter, "fulfillmentText" it works fine because that is sent as a string.
But the second parameter, "fulfillmentMessages" is supposed to be sent as an object, but I'm not doing something right.  I get back this message from DialogFlow.


Fulfillment Text
{
  "fulfillmentText": "Searchby Choice is searchby|theme :: email| :: ",
  "fulfillmentMessages": "{\"text\":\"text response\"}"
}

Fullfillment Response (the error code)
Webhook call failed. Error: Failed to parse webhook JSON response: Expect an array but found: "{\"text\":\"text response\"}".

So I am clearly not setting up the array/object correctly in my php code to be recognized as an "array" in DialogFlow.  So how do I send the parameter "text" as an object array? What do I need to change in my PHP.

This reference page shows the correct setup for this option in dialogflow.
https://dialogflow.com/docs/reference/api-v2/rest/Shared.Types/Message#Text

Thanks in advance for the assistance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Provide your actual/full code. The code above is only your test code version.

Provide the version where you set fulfillmentText + fulfillmentMessages (or that's what it sounds like your code is doing).

Although... seems like fulfillmentMessages will be returned from the call, rather than passed through the call.
Paul KonstanskiProject Specialist

Author

Commented:
Providing additional code will only confuse the issue. The rest of my code works just fine.  The issue is with how to set up a JSON response for an object to be read correctly within DialogFlow.  

DialogFlow wants the JSON to be represented this way:
{
  "fulfillmentText": string,
  "fulfillmentMessages": [
    {
      object(Message)
    }
  ]
}

Open in new window


So when I just send the first part of the JSON object that is to be sent as a string, it works fine.
	$myObj->fulfillmentText = $msg;
	
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window


But when I try to send the second part that is supposed to be sent as an object, it fails.

	$myObj->fulfillmentText = $msg;
	$myObj->fulfillmentMessages = json_encode(array("text" => "text response")); 
	
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window


So my problem is how I am setting up the "object" to be sent to dialog flow.  It is this part of my code that is not set up correctly.
myObj->fulfillmentMessages = json_encode(array("text" => "text response")); 

Open in new window


So the core question how do I use PHP to end the correct format as indicated in this image below:

The part of the JSON code I need help setting up in PHP
Expert of the Year 2008
Top Expert 2008

Commented:
Instead of:
$myObj->fulfillmentMessages = json_encode(array("text" => "text response"));

try:
$myObj->fulfillmentMessages = [ array("text" => "text response") ];
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Paul KonstanskiProject Specialist

Author

Commented:
That gives me an error message of:

Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: "text response".
Expert of the Year 2008
Top Expert 2008

Commented:
How about:
$myObj->fulfillmentMessages = [ json_encode(array("text" => "text response")) ];
Paul KonstanskiProject Specialist

Author

Commented:
In a dialog with another programmer, I found this solution:

	$myObj->fulfillmentText = "This is my test message";
	$myObj->fulfillmentMessages = array(
		array(
			"text" => array(
			"text" => array("text response")

			) 
		)
	);
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window

Expert of the Year 2008
Top Expert 2008

Commented:
Great.  Thanks for posting back.  You can accept your comment as the solution.
Paul KonstanskiProject Specialist

Author

Commented:
The most recent comment by hielo also gave an error:

Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: "{\"text\":\"text response\"}".

The solution I offered in previous comment works...
Project Specialist
Commented:
This is the solution that worked:

	$myObj->fulfillmentText = "This is my test message";
	$myObj->fulfillmentMessages = array(
		array(
			"text" => array(
			"text" => array("text response")

			) 
		)
	);
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window

Paul KonstanskiProject Specialist

Author

Commented:
Thank you all for your comments and questions. It helped me arrive at the solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial