This tutorial will show you how to build a very basic chat-bot. This chat-bot will have set responses for the input that the users type out.
For this example, the chat-bot will be designed to handle customer service type situations.
How it works:
When user types “help” the chat-bot will output a list of options the user can choose from.
The User will then pick the area they need assistance with; the options are Customer Service,Report Issue,Account Details and Make Payment.
If User types “customer service” the bot will ask them to choose from: Speak to Rep, Return Product, Broken Item
Once users has chosen, they will then be asked to enter their username and email.
For the chat-bot portion of this project, JFrame package will be used to create a GUI for the bot output and a textbar for users to type their inputs.
If you are unfamiliar to JFrame you can check out their documentation and or install JFrame here.
First, this portion of code will create the JFrame GUI.Within the GUI,the chat-bot will display its responses and an area for users to type
public class Main extends JFrame {
//Typing Bar
private JTextField inputBar = new JTextField();
//Chat Outpu Area
private JTextArea chatOutput = new JTextArea();
public Main() {
//Creating the Frame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 600);
this.setVisible(true);
this.setResizable(false);
// Setting GUI title
this.setTitle("Customer Service ChatBot");
// creating inputBar
inputBar.setLocation(2, 540);
inputBar.setSize(590, 30);
Second, this portion of code will set the dialog of the chat-bot. Here is the main part of the code, this is where the responses of the chat-bot are set.
inputBar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String userInput = inputBar.getText();
chatOutput.append("You: " + userInput + "\n");
if(userInput.contains("help")) {
botOutput("Please select a catagory I can assist you with:Customer Service,Report Issue,Account Details, Make a Payment");
}
else if(userInput.contains("customer service")){
botOutput("Please select area needing assistance: Speak to rep, Return Product, Broken Item");
}
else if(userInput.contains("Speak to rep")){
botOutput("Please wait while we connect you!");
}
else if(userInput.contains("account details")){
botOutput("Please enter Email or Account #");
}
else if(userInput.contains("make payment")){
botOutput("Please enter Email or Account #");
}
else if(userInput.contains("report issue")){
botOutput("Select an issue: Late delivery, Missing Product,
Invalide Info");
}
inputBar.setText("");
}
});
Last portion of code, labels the chat-bot output and adds the responses to the JFrame GUI
//chatOutput
chatOutput.setLocation(15, 5);
chatOutput.setSize(560, 510);
chatOutput.setLineWrap(true);
chatOutput.setEditable(false);
//Frame items
this.add(inputBar);
this.add(chatOutput);
}
public void botOutput(String s){
chatOutput.append("ChatBot: " + s + "\n");
}
public static void main(String[] args){
new Main();
}
}
This it! Your customer service chatbot should be complete. It should look like this. The label “You” represents the user typed output and the “ChatBot” labels are the responses from the chat-bot.

Complete Source Code
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main extends JFrame {
//Typing Bar
private JTextField inputBar = new JTextField();
//Chat Outpu Area
private JTextArea chatOutput = new JTextArea();
public Main() {
//Creating the Frame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 600);
this.setVisible(true);
this.setResizable(false);
// Setting GUI title
this.setTitle("Customer Service ChatBot");
// creating inputBar
inputBar.setLocation(2, 540);
inputBar.setSize(590, 30);
//inputBar Action Event
inputBar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String userInput = inputBar.getText();
chatOutput.append("You: " + userInput + "\n");
if(userInput.contains("help")) {
botOutput("Please select a catagory I can assist you with:Customer Service,Report Issue,Account Details, Make a Payment");
}
else if(userInput.contains("customer service")){
botOutput("Please select area needing assistance: Speak to rep, Return Product, Broken Item");
}
else if(userInput.contains("Speak to rep")){
botOutput("Please wait while we connect you!");
}
else if(userInput.contains("account details")){
botOutput("Please enter Email or Account #");
}
else if(userInput.contains("make payment")){
botOutput("Please enter Email or Account #");
}
else if(userInput.contains("report issue")){
botOutput("Select an issue: Late delivery, Missing Product, Invalide Info");
}
inputBar.setText("");
}
});
//chatOutput
chatOutput.setLocation(15, 5);
chatOutput.setSize(560, 510);
chatOutput.setLineWrap(true);
chatOutput.setEditable(false);
//Frame items
this.add(inputBar);
this.add(chatOutput);
}
public void botOutput(String s){
chatOutput.append("ChatBot: " + s + "\n");
}
public static void main(String[] args){
new Main();
}
}