dialogflow-php
- 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:
- First create a project on Google Cloud Console then create a service account and download the private key file.
- Create Agent in Dialogflow and get the project ID from settings.
for more details click here.
Create a service account and download the private key file
Create a service account:
-
In the Cloud Console, go to the Create service account page.
Go to Create service account - Select a project.
-
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
. - Click Create and continue.
-
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. - Click Continue.
-
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:
- In the Cloud Console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
Usage
curl -sX POST http://localhost:8080/dialogflow.php -d '{"sid":"1234567","message":"hi"}'
~$ curl -sX POST http://localhost:8080/dialogflow.php -d '{"sid":"12345678","message":"my name is sumith"}'
{
"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
}
}
Testing
- After saving the
service-account-file.json
file in main directory. - start a PHP server (for testing purpose)
php -S localhost:8080
-
Now open http://localhost:8080 in any web browser.
-
Now enter any name in
sender
field and enter any message inmessage
field. -
Click send message you will see
fullfilment message
Note: Wait untill you get fulfilment message.
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.