DialogFlow API – Use in Laravel

DialogFlow is one of the best tool available for Machine Learning. It is developed and maintained by Google Team. You can use DialogFlow for various applications like Chat bot, Customer support service etc. In this blog I am going to explain how you can connect DialogFlow with Laravel using APIs.

DialogFlow Logo

DialogFlow

Generate Connection File

For using API, you first have to generate connection file which will be used for Authentication. For this here are the steps.

  1. Login to DialogFlow Console
  2. Select the Project
  3. Go to Settings
  4. Click on Service Account Link
  5. You will be redirect to Google Project Console.
  6. Select Service Account menu from left hand side.
  7. Click on Add New Service Account
  8. Follow the steps and add last step create and download JSON file.

This file will be used as connection credentials.

Integrate SDK in the Laravel App

First of all you have to download SDK using composer. Execute command in terminal

composer require google/cloud-dialogflow

This will download the SDK in vendor folder. Now set a link of credential file in ENV file.

GOOGLE_APPLICATION_CREDENTIALS= /path/to/your/file

Now you can use the API to connect to DialogFlow. Here are few examples.

First of all add following dependencies in your file to use the SDK.

use Google\Cloud\Dialogflow\V2\IntentsClient;
use Google\Cloud\Dialogflow\V2\Intent;
use Google\Cloud\Dialogflow\V2\Intent\TrainingPhrase\Part;
use Google\Cloud\Dialogflow\V2\Intent\TrainingPhrase;
use Google\Cloud\Dialogflow\V2\Intent\Message;
use Google\Cloud\Dialogflow\V2\Intent\Message\Text;

Get List of All Intents

Now use following code to get list of all the intents.

$intentsClient = new IntentsClient();
$parent = $intentsClient->projectAgentName(“YOUR DIALOGFLOW PROJECT NAME”);
$intents = $intentsClient->listIntents($parent,array(“intentView”=>1));
$allIntents = array();
$iterator = $intents->getPage()->getResponseObject()->getIntents()->getIterator();
while($iterator->valid()){
$intent = $iterator->current();
$allIntents[] = array(“id”=>$intent->getName(),”name”=>$intent->getDisplayName());
$iterator->next();
}
return Response::json(array(‘success’=>true,”allIntents”=>$allIntents));

Get Text Prediction

Use following code to get text prediction.

$sessionsClient = new SessionsClient($credentials);
$session = $sessionsClient->sessionName($projectName, uniqid());
$languageCode = ‘en’;
// create text input
$textInput = new TextInput();
$textInput->setText($text);
$textInput->setLanguageCode($languageCode);

// create query input
$queryInput = new QueryInput();
$queryInput->setText($textInput);

// get response and relevant info
$response = $sessionsClient->detectIntent($session, $queryInput);
$queryResult = $response->getQueryResult();
$queryText = $queryResult->getQueryText();
$intent = $queryResult->getIntent();
$displayName = $intent->getDisplayName();
$confidence = $queryResult->getIntentDetectionConfidence();
$fulfilmentText = $queryResult->getFulfillmentText();

Leave a Reply

Your email address will not be published.