Let’s create a bot using Java

A simple guide on how to create a chatbot

Photo from https://blog.printsome.com/human-resources-technology/

A chatbot is an application that simulates a human response to the user. It is a more natural way to interact with a system by using conversation and it enhances the customer experience. So, this article shows you how to build a bot using the Java Echo Bot template.

Prerequisites

  1. Java 1.8 or later
  2. Bot Framework Emulator
  3. Install Maven
  4. Install node.js version 12.10 or later

Firstly you have to install the above-mentioned tools and then install the latest npm.

npm install -g npm

After that install Yeoman

npm install -g yo

Install generator-botbuilder-java

generator-botbuilder-java is a Yeoman generator for Bot Framework. It helps you to set up a conversational AI bot using the Bot Framework.

npm install -g generator-botbuilder-java

This generator supports three template options and I’m going to use the ‘Echo Bot’ template. Those templates are,

  • Echo Bot: This template is used for the basics of sending messages to a bot and the bot process the messages by repeating the same message back to the user.
  • Empty Bot: This template is better to use if you are familiar with Bot Framework v4.
  • Core Bot: Core Bot template is more suitable if you want to create advanced bots.

After the above installations, you can verify whether Yeoman and generator-botbuilder-java have been installed correctly by using the following command.

yo botbuilder-java --help

How to create an Echo Bot?

Create a new folder and open the command prompt. After that run the following command.

yo botbuilder-java -T "echo"

Then Yeoman requests you some information and gives the default values for them.

╭─────────────────────────────╮
// \\ │ Welcome to the │
// () () \\ │ Microsoft Java Bot Builder │
\\ // /│ generator! │
\\ // ╰─────────────────────────────╯
v4.13.1
? What's the name of your bot? echo
? What's the fully qualified package name of your bot? com.mycompany.echo
? Which template would you like to start with? Echo Bot
? Looking good. Shall I go ahead and create your new bot? Yes

It’s time to start your bot…

To try this sample locally,

navigate to the directory where you created your bot and then run the following command in the terminal

mvn package

After the build is completed, run the following command

java -jar .\target\echo-1.0.0.jar

Then launch Bot Framework Emulator and click on the Open Bot option. After that enter the Bot URL of ‘http://localhost:3978/api/messages’ and click connect. Send a message to your bot and the bot will respond back 😋

In my next article, I’ll explain how to deploy your bot to Azure.

--

--