A simple API for handling webhook requests and responses from DialogFlow in PHP.
Everything you need should be in the Webhook.php file with the Webhook class. Here are a few examples of getting set up.
To send a simple response to all platforms, use the following code
include('Webhook.php');
$args = ['projectId' => 'test-project-id'];
$wh = new Webhook($args);
$wh->respond_simpleMessage('Say this out loud', 'Display this text on screen');
Data is posted from the DialogFlow service to a location of your choosing (in the fulfillment tab). To retrieve it, use any of the below API calls.
// Instantiate
include('Webhook.php');
$args = ['projectId' => 'test-project-id'];
$wh = new Webhook($args);
// Get all data as an array
$allData = $wh->getDecodedWebhook();
// Instantiate
include('Webhook.php');
$args = ['projectId' => 'test-project-id'];
$wh = new Webhook($args);
// Get the intent name of the conversation
$intentName = $wh->get_intent();
// Instantiate
include('Webhook.php');
$args = ['projectId' => 'test-project-id'];
$wh = new Webhook($args);
// Get all parameters as an array
$parametersArray = $wh->get_parameters();
// OR, get a specific parameter. If it does not exist, the function will return FALSE
$parameterValue = $wh->get_parameter('country');
Sending data is quite easy, simply use any of the below API calls.
The build_* functions create specific rich markups that can be used to send more complicated information to devices running the google assistant.
Here's an example of how to set up a simple service for only google devices.
// Instantiate
include('Webhook.php');
$wh = new Webhook('test-project-id');
// Build the response
$textToSpeech = "Say this text out loud";
$displayText = "Display this text on screen";
$wh->build_simpleResponse($textToSpeech, $displayText);
// Send the response
$wh->respond();
If you want to add more nuance to your speech, consider using SSML.
// Instantiate
include('Webhook.php');
$wh = new Webhook('test-project-id');
// Build the response
$ssml = '<speak> Say this out loud, wait three seconds <break time="3s"/> then continue speaking. </speak> ';
$displayText = "Display this text on screen";
$wh->build_ssmlResponse($ssml, $displayText);
// Send the response
$wh->respond();
Using SSML you can send only audio back to a user. Simply provide the url, or urls (as an array), to the audio files like so.
// Instantiate
include('Webhook.php');
$wh = new Webhook('test-project-id');
// Build the response
$displayText = "Display this text on screen";
$wh->build_audioResponse( 'https://www.example.com/examples/mp3/example1.mp3', $displayText);
// Send the response
$wh->respond();
By calling the endConversation() method, you can indicate to dialogFlow that you are not expecting a response. By default, a response from the user will be expected.