Create a chatbot using IBM Watson Assistant and PHP
Chat bot has recently created havoc among consumer business. Many people are saying chat bot's usability is going to increase in coming years and you must be wondering how to create a chat bot? This blog will help a beginner to build a chat bot using IBM Watson Assistant (formerly known as Watson Conversation) service which has free subscription option useful for learning purpose. Also we will use PHP as server side language as a controller which will interact with the Assistant tool to continue the dialog flow of the conversation.
We have selected restaurant reservation as a use case for the bot. This bot will have give the user 3 options like Make or Cancel or Reschedule reservation in a particular restaurant. In this tutorial we will use the usage of Slot features in Watson Assistant tool and also will use handlers. We will see how we can integrate with Facebook Messenger to access the chat bot using the PHP controller and finally integrating with Microsoft Teams.
Step 1 - Signup for IBM Watson Assistant Tool:
To use the IBM Watson assistant tool, you must sign up first at IBM Cloud or Bluemix platform. This video will guide you on how to signup and use the tool. The video describes the basics of IBM cloud on how to login, how to subscribe to Watson Assistant service and the basics knowledge require to design and build the chatbot.
Step 2 - Design the Watson Assistant Tool:
Now lets start with the design of the actual conversation flow. Within Watson Assistant tool, you need to take care of three things. Intents, Entities and Dialog. The intents are the verb part to represent the intention of the user. In our example of restaurant reservation, the main three intents are identified as:
- Intent to make a reservation
- Intent to cancel a reservation.
- Intent to reschedule a reservation.
A detailed discussion on design the chatbot in Watson Assistant tool is done in the below video. Watch the video to understand how you design the intents, entities and dialog quickly. Also you can use the features like Slot and Event handlers.
The entities are the noun or object of class which represents the data. In our restaurant example, lets assume that the restaurant is located in three cities Kolkata, Delhi and Mumbai. So we will define an entity @city which will have three values as Kolkata, Delhi and Mumbai. Each value will have multiple synonym to identify the value as entered by the user. For example, many people call Calcutta which is the former name of city Kolkata. So the bot should be able to recognize the value of entity @city as 'Kolkata' when the user has entered as 'Calcutta'.
There are two types of entities. one is system defined which means it is defined by Watson Assistant tool to identify certain values entered by the user. For example @sys-date is used to identify a date and @sys-time is used to identify a time entered by the user. We will be using
Finally you design the dialog using the entities and intents. The dialog flow is the key part of the design. You have to use the intents to identify user input and take action appropriately by identifying the data from entities.
When you expand the #Make_Reservation dialog node, you see that I have used Slot features of Assistant tool. Slot is a nice way to capture user input and identify all the entities either in one single sentence or one by one. This feature helped me to capture the reservation date, time, city, number of person and name of the person at one go.
Step 3 - Code with PHP:
Once we finish the design of the conversation in Watson Assistant tool, it is time to write our code using a server side programming language which has the capability of calling or consuming REST API. We will use PHP here. The complete code is present in GitHub. But you may watch the below video to see the demonstration of the chat bot and the explanation of the PHP code in better way to understand.
You need change the following line of the code files.
In file app/global.php change the below line of code according to your directory structure.
const C_BASE_URL = 'http://localhost/demo/chatbot/';
Also modify the file app/ajax/class/config.php for the below line of code to put the Watson workspace id, username and password and the MySQL database information.
define("CONFIG_WATSON_WORKSPACE_ID","your-watson-workspace-id"); define("CONFIG_WATSON_USERNAME","your-watson-username"); define("CONFIG_WATSON_PASSWORD","your-watson-password");
In the same config.php file, change the database connection details with your own MySQL database name, username and password
//local dev define("mysqlServer","localhost:3306"); define("mysqlDB", "test"); define("mysqlUser","dbUserName"); define("mysqlPass","dbPassword"); define("mysqlPort","3306");
and finally change the file assets/js/chat_js.php for the below lines of code according to your directory structure:
var rootDirectory = "/demo/chatbot/"; var localDirectory = "/demo/chatbot/app/ajax";
You have to understand that REST APIs are stateless in nature means it does not store the information of the request but the Watson API expects the conversation context to be sent as input parameter. To implement this, I have to use the sessions in PHP using $_SESSION global variable.
Step 4 - External resources:
The MySQL table creation script is present at folder ext_resources along with the JSON file of the Watson Assistant workspace export.
In our next blog, we will try to integrate the same restaurant reservation bot with Facebook Messenger and Microsoft Teams like communication tool.