Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

sumithemmadi/dialogflow-php

Repository files navigation

dialogflow-php

Version Licence FOSSA Status Maintenance issues

  • PHP Client Library for Dialogflow API v2

Requirements

Installation

  • To begin,lets clone this repository
git clone https://github.com/sumithemmadi/dialogflow-php.git
cd dialogflow-php
  • Then install the preferred dependencies for Dialogflow.
composer install

Before you begin

You should do the following before reading this guide:

  1. First create a project on Google Cloud Console then create a service account and download the private key file.
  2. Create Agent in Dialogflow and get the project ID from settings.

    for more details click here.

  3. There you can see Your Project ID at GOOGLE PROJECT in GENERAL settings

Create a service account and download the private key file

Create a service account:

  1. In the Cloud Console, go to the Create service account page.

    Go to Create service account

    image

  2. Select a project.
  3. In the Service account name field, enter a name. The Cloud Console fills in the Service account ID field based on this name.

    In the Service account description field, enter a description. For example, Service account for quickstart.

  4. Click Create and continue.
  5. Click the Select a role field.

    Under Quick access, click Basic, then click Owner.

    Note: The Role field affects which resources your service account can access in your project. You can revoke these roles or grant additional roles later. In production environments, do not grant the Owner, Editor, or Viewer roles. Instead, grant a predefined role or custom role that meets your needs.

  6. Click Continue.
  7. Click Done to finish creating the service account.

    Do not close your browser window. You will use it in the next step.

Create a service account key:

  1. In the Cloud Console, click the email address for the service account that you created.
  2. Click Keys.
  3. Click Add key, then click Create new key.
  4. Click Create. A JSON key file is downloaded to your computer.
  5. Click Close.

Usage

Text Response

<?php
namespace Google\Cloud\Samples\Dialogflow;

require __DIR__ . "/vendor/autoload.php";

// Save Google Account Credentials json file in root directory
// make sure that Google Account Credentials JSON file and this file are in same directory.

$google_application_credentials = "[GOOGLE ACCOUNT_CREDENTIALS_JSON_FILE]";

// PROJECT ID
// Please Change "[ENTER_PROJECT_ID]" with you dialogflow project ID
$projectId    = "[ENTER_PROJECT_ID]";
$languageCode = 'en-US';
$sessionId    = "123456789";

// Get response from text input.
$text = "hi";

$queryResponse = get_response_from_text($google_application_credentials,$projectId,$sessionId,$languageCode,$text);

echo $queryResponse;
?>

Audio Response

<?php
namespace Google\Cloud\Samples\Dialogflow;

require __DIR__ . "/vendor/autoload.php";

// Save Google Account Credentials json file in root directory
// make sure that Google Account Credentials JSON file and this file are in same directory.

$google_application_credentials = "[GOOGLE ACCOUNT_CREDENTIALS_JSON_FILE]";

// PROJECT ID
// Please Change "[ENTER_PROJECT_ID]" with you dialogflow project ID
$projectId    = "[ENTER_PROJECT_ID]";
$languageCode = 'en-US';
$sessionId    = "123456789";


// Get Response from audio input.
 $path = "test/hello.wav";

$audioResponse = get_response_from_audio($google_application_credentials,$projectId,$path, $sessionId, $languageCode);
echo $audioResponse;

?>

Response Output

{
    "responseId": "f7f810a1-0043-4f85-ac6c-241705aa6b8a-94f60986",
    "queryResult": {
        "queryText": "my name is sumith",
        "languageCode": "en",
        "parameters": {
            "person": {
                "name": "sumith"
            }
        },
        "allRequiredParamsPresent": true,
        "fulfillmentText": "Sorry, I was not able to understand your intention . I'm trying to get better at this thing.",
        "fulfillmentMessages": [
            {
                "text": {
                    "text": [
                        "Sorry, I was not able to understand your intention . I'm trying to get better at this thing."
                    ]
                }
            }
        ],
        "intent": {
            "name": "projects\/sumith-rwup\/agent\/intents\/f95d3add-52fb-4119-87f0-e1717181173d",
            "displayName": "user.name"
        },
        "intentDetectionConfidence": 1
    }
}

Nginx Server(Production)

  • Create a nginx server

  • Run below command to generate nginx.conf file for your server.

~$ php nginx-conf.php
  • Now nginx.conf will be created in you local directory.
  • move the nginx.conf file to /etc/nginx/.
  • now start the nginx server (Default port will be 80)
  • Then start php-cgi on port 9000
~$ php-cgi -b 127.0.0.1:9000 & nginx

LICENSE

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/

Copyright 2021 sumithemmadi

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.