ChatBot ( AI + NPL) + REST APIs

This is the simple application which has ChatBot and ChatBot interact with REST APIs ( NPL Engine) by sending User message, REST APIs has understood the message and reply back with using NPL+AI. ( Internally it is using Pattern matching).

This application is searching Resturant based on static data, it is a just platform to get an idea how ChatBot is working.  You can use as the platform and enhance it.

Technologies used in this article:

  • ChatBot ( Opensource Project :- – It is used Node.js
  • RestEasy
  • Tomcat

Backend Services

I have created only one service, which except a message and processing message aginst static data.

File :- pom.xml

<project xmlns="" xmlns:xsi="" 	xsi:schemaLocation="">
			<id>MVN Repositories</id>

Here is my API project structure,

Screen Shot 2017-05-27 at 3.32.27 PM

This application works on static data with predefined patterns, the first string is defining patterns and the second string is its respective answer/question.

File :-

		dataList.add(createSampleData("hi|hello|hey","Hello Mr. Shailendra Soni, How can I help you."));
		dataList.add(createSampleData("restaurants|restaurant|food","Do you want to try any new cuisine or dine in the regular restaurants you visited?"));
		dataList.add(createSampleData("new","Okay. That great! What kind of Food do you like? We have found some restaurants near by you which servers following foods Mexican/Italian/Chinese/Indian, Please choose anyone of them."));
		dataList.add(createSampleData("regular","Aha.. Let me look, As per past 3 months, you had visisted Italian/chinese, Please choose anyone of them."));
		dataList.add(createSampleData("maxican","Great Choice. Here are Maxican Resturants with offers 1. Armando's Mexican Food 2. Top Shelf Mexican."));
		dataList.add(createSampleData("italian","Awesome Choice. Here are Italian Resturants with offers 1. Tutti Santi by Nina 2. Romano's Macaroni Grill "));
		dataList.add(createSampleData("chinese","Super Choice. Here are Chinese Resturants with offers 1. Panda Express 2. Go Go China "));
		dataList.add(createSampleData("indian","Amazing Choice. Here are Indian Resturants with offers 1. Bawarchi Indian Cuisine 2. Marigold Maison "));
		dataList.add(createSampleData("offers|deal|coupons","Yes, There is one offer on some the restaurants too, Here is 1. If you spend $100, you will get 20% discount. Do you want to see all those Restaurants?"));
		dataList.add(createSampleData("yes","Here are Resturants list 1. Village Inn 2. Sonic Drive-In."));
		dataList.add(createSampleData("no","Have nice day Mr. Soni."));
		dataList.add(createSampleData("thanks|thank","It's my pleasure."));
		dataList.add(createSampleData("bye","Have nice day Mr. Soni."));
		dataList.add(createSampleData("great|awesome","That's my job. Thank You."));

Below code is responsible for finding particular sentence/answer based on the message from ChatBot.

It takes the message and then applies regex with using boundaries words and then join all those words with “|” character. The final String considers as the matcher.

Now, iterate on static data and compile the patterns and match with above final string. If its match then returns pattern name and respective value otherwise it will return “no found” as the pattern and “I’m afraid I don’t understand. I’m sorry!” as value.

File :-


import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;


public class BotService {

    public ContentMatch match(String content) {

        ContentMatch contentMatch = new ContentMatch();
        if (!StringUtils.isBlank(content)) {
            // word boundaries
            String[] words = content.trim().toLowerCase().split("\\b");
            List<String> tokens = Arrays.asList(words);
            String patternString = "\\b(" + StringUtils.join(tokens, "|") + ")\\b";
            Matcher matcher = null;
            Pattern pattern = null;
            for (StaticData sd : DataBuilder.getData()) {
                pattern = Pattern.compile(sd.getPattern());
                matcher = pattern.matcher(patternString);
                while (matcher.find()) {
                    return contentMatch;

        // default set
        contentMatch.setValue("I'm afraid I don't understand. I'm sorry!");
        return contentMatch;


There are other java files too which you can check at my GitHub repository which is given at the then end of this blog.


Now, I have downloaded open source ChatBot application from below link, It runs on Node.js application, please go through below link for how to install and how to use it.

Once I have installed “bottr”, then open “index.js” from the installation directory and modified for connecting to REST APIs, get the value and same is displaying back to User.

File :- index.js

const Bottr = require('bottr')
const BottrApp = require('bottr-app')
const request = require('request');
const bot = new Bottr.Bot();
var fs = require('fs');
var path = require('path');
var sessiontest;
bot.on('message_received', function(message, session) {
    sessiontest = session;
    var propertiesObject = { content: message.text };
    var options = {
        url: 'http://localhost:8080/poc-bot/bot',
        qs: propertiesObject
    request.get(options, function(err, response, body) {
        if (err) { console.log(err); return; }
        var jsonData = JSON.parse(body);
bot.use(new BottrApp())

Bottr runs on localhost:3000. Below image is showing actual conservation between the End User and chatbot.

Screen Shot 2017-05-27 at 6.39.42 PM.png

This is the very simple way to represent ChatBot with natural language processing.

Links :-

Chatbot-api :-

Bottr :-

One thought on “ChatBot ( AI + NPL) + REST APIs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s