A simple PHP and JavaScript web client for interacting with Google Dialogflow CX through an interface directly on a web page.
I built this for my personal portfolio website originally, both to build out some demos and brush up on my JS and PHP. I struggled a bit at first to get authentication and API calls working (maybe I was reading the wrong documentation haha)! Anyway, once I got it working I decided to tidy up the code as best I can, and post it on github in case anyone else finds a use for it.
- Authentication using a service account to retrieve an access token
- PHP for API calls, Javascript & HTML for user interface
- On-page interface
- conversation peristence, if the page is refreshed the chat log remains
- Very simple client so should be easy to understand and customise for your needs
- Debugging mode that shows raw API responses if needed
- Live demo at https://seanmaddison.uk/portfolio/chatbot-demo
- Enable curl module in php.ini
- Enable openssl module in php.ini
- Install dependencies
- install Google APIs Client Library for PHP
- install vlucas/phpdotenv package
- I've included a composer.json file for these that only installs the required dependencies, just run
composer install
in the root directory once downloaded
- Setup your Dialogflow CX project and an agent flow
- Create a service account for your agent using the Google cloud console. It needs to at least have Dialogflow Client API permission.
- Download the .json key file for the service account and place it somewhere the PHP pages can access
- Important: make sure browsers can't view or download this file! I've include a .htaccess file that instructs Apache2.4 to disallow this
- Edit .env file with your agent details and file locations, using the .env-example file as a template
- Important: make sure browsers can't view or download your .env file! I've specified a .htaccess file for this
- Edit config.php to tweak the options that are there if needed
- Browse to index.php to try out the client!
- Customise and use :)
- Rich media handling in version 2
- Making the CSS themeable
- Tidy up code, variable names etc.
- Better error handling
- integrate another provider perhaps, e.g. GPT4