Chatbot Using Java | Creating chatbot using Java | java chatbot example using aiml library

Chatbot in Java

Chatbot or AI chatbot is a software used by companies to solve problems of customers or to talk to them by using AI instead of a human being. Chatbots are very useful for a organization as they reduce their cost of Support team and increases their profit so if you want to a create a simple chatbot using java and use it into your website you can follow these steps:-

Let's look into the steps required to make and execute our bot:-

  1. Download any Code editor or IDE for Java
  2. Download Java into your system from the official website
  3. Create a new project or file in that IDE or Code Editor
  4. Name that file as main.java
  5. copy the code below and paste it in that file with your own Package name
       
package [your.package.name];

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        System.out.println("Hey There I am Chatbot");

        String timestamp = new SimpleDateFormat("dd:mm:yyyy hh:mm:ss").format(Calendar.getInstance().getTime());
        boolean running = true;
        String input;
        String tgc = "TechzGameCoding";

        while (running == true){
            System.out.println(" ");
            input = sc.nextLine();

            try {
                if (input.equals("Hi")) {
                    System.out.println("Hi there!");
                } else if (input.equals("hi")) {
                    System.out.println("Hi there!");
                } else if (input.equals("Bye")) {
                    System.out.println("Bye sir Have a Good day");
                    running = false;
                } else if (input.equals("bye")) {
                    System.out.println("Bye sir Have a Good day");
                    running = false;
                } else if (input.equals("time")) {
                    System.out.println("dd:mm:yyyy hh:mm:ss");
                    System.out.println(timestamp);
                } else if (input.equals("Open java ide")) {
                    System.out.println("Ya sure sir");
                    File file = new File("[File Path]");
                    Desktop.getDesktop().open(file);
                } else if (input.equals("Open python ide")) {
                    System.out.println("Ya sure sir");
                    System.out.println("Opening PyCharm");
                    File file = new File("[File Path]");
                    Desktop.getDesktop().open(file);
                } else if (input.equals("open code")) {
                    System.out.println("Ya sure sir");
                    System.out.println("Opening VS");
                    File file = new File("[File Path]");
                    Desktop.getDesktop().open(file);
                } else if (input.equals("open google")) {
                    URI uri = new URI("http://www.google.com");

                    java.awt.Desktop.getDesktop().browse(uri);
                    System.out.println("Web page opened in browser");
                }else if (input.equals("Open google")) {
                    URI uri = new URI("http://www.google.com");

                    java.awt.Desktop.getDesktop().browse(uri);
                    System.out.println("Web page opened in browser");
                } else if (input.equals("open online chatbot")) {
                    URI ur = new URI("https://chatbot.techzgamecoding.ml/");

                    java.awt.Desktop.getDesktop().browse(ur);
                    System.out.println("Opened Online chatbot enjoy!");
                }else if (input.equals("Open TechzGameCoding")){
                    URI u = new URI("https://techzgamecoding.blogspot.com/");

                    java.awt.Desktop.getDesktop().browse(u);
                    System.out.println("Opening "+ tgc);
                }
                else {
                    System.out.println("Sorry I don't Understand");
                }


            }
            catch(Exception e){
                e.printStackTrace();
            }
        }

        System.out.println("sir please give me next command");

    }
}

Now if you want to embed this in your website you can use any Java code runner embedder and if you want you can add more functions to it. You can connect it to a chatbot AI API and can give response from it. If someone asks question from it. You can add infinite functions to it. It is just an example. We used AIML library to show front-end response/output we will show it in the next article with proper guidance.

Java is a high-level oop (object oriented programming) language, which was developed by Sun Microsystems in 1991. It's initial name was oak. It is mainly used in Android and web development. Java is a kind of language developed by the developers for what time demanded. At that time C language family was very famous that's why java is kind of similar to C. Chatbot Using Java | Creating chatbot using Java | java chatbot example using aiml library

Post a Comment

Previous Post Next Post