Building a Chat Bot in under 60 seconds with Karmaloop AIML Bot Server

The Rise of the Bots

We have entered the century of Artificial Intelligence, it is finally here. However we won’t have super smart Human like AI from day one. We will ease into the   process with dumb bots taking over mundane jobs and automating them. As an example, I am already done with manually setting up alarms. That job has gone to Siri and Alexa. I am also done setting reminders. They have taken that too. In this article, let us see how we can build a bot in under 60 seconds.

Karmaloop AIML Bot Framework

Brief History

Chat bots have been here since the 1990s. In the late 90s, Dr. Richard Wallace created an XML based specification called AIML or Artificial Intelligence Markup Language. This was clean, simple and easy to customize. I was going through my Computer Science graduation in 2005, and that is when I stumbled upon Dr. Richard Wallace’s work with ALICE. ALICE was a super smart chat bot for her time and her AIML files were craftfully done. At that time, I was learning C# and decided to build an AIML parser in C# to bring ALICE to life on my computer, and also use her as my final semester project. Long story short, it all went well, and I got through my engineering and got busy with a job and later, business. It was only until 2016 when the bug bit me again and I restarted work on the libraries and in 2018 I finally released the first Open Source version of what I called, Karmaloop AIML Bot Server. Today, I will show you how you can get started with a solid underlying foundation of ALICE bot and add your custom capabilities on top.

Download and Run Karmaloop AIML Bot Server

Get the binaries from the Releases page on Github – https://github.com/KarmaloopAI/karmaloop-aiml-bot-server/releases

If you are on Windows, extract and navigate to the folder then run the KarmaloopAIMLBotServer.exe file to run the bot server. If you are on macOS or Linux, install and use Mono to run the above file.


mono KarmaloopAIMLBotServer.exe

Awesome! Now you should have ALICE bot running on your machine. Open a web browser window and point it to http://localhost:8880/api/ChatUi/index.html

On Windows, you may need to run the following command to make sure the server can open port 8880 for listening to incoming API requests.


netsh http add urlacl url=http://*:8880/ user=Everyone listen=yes

Now chat and have fun!

Customize and add your own conversations

To explore this topic in full, you may first want to aprise yourself of AIML 2.0 specifications – https://callmom.pandorabots.com/static/reference/

Step 1.

In the folder where you extracted the binaries, should be a folder called “aiml”. This is where the AIML files are stored by default. Let us create a new aiml file called magicmirror.aiml and then copy paste the following code into the file


<!--?xml version="1.0" encoding="UTF-8"?-->
<aiml version="1.0">
<category>
  <pattern>MAGIC MIRROR IN MY HAND WHO IS THE FAIREST IN THE LAND</pattern>
  <template>My Queen, you are the fairest in the land.</template>
</category>
</aiml>

Save the file, and restart your bot server. Then at the chat prompt, type the question – Magic mirror in my hand, who is the fairest in the land?

The conversation should look somewhat like below:

As expected! The response is exactly what we coded into the aiml file. That is it. If this took you under 60 seconds to do, then you built your first chat bot in less than a minute.

OK, but how can I produce dynamic responses?

Hmm… so you are ready to build something more complex, and would like to mash external data into your responses… right? Like build a weather skill that can tell you the weather of any city in the world. So let us keep this complex bit for a later post, but if your curiosity is insatiable, then simply open the file called “zweather.aiml”. Then open the source code (which you can download from Github) and look up weather skill. Set breakpoints and see how the skill executes itself. If you are good, you should already be able to see the simplicity and power of this approach, but hey more on that later!

Where to go from here

Now that you know how to create a basic conversational bot with Karmaloop AIML Bot Server you may be itching to build your own conversational bot and handle complex conversations. May be you want to automate arguments with your wife (LOL). Whatever your need may be, you may want to invariably start with reading up on AIML 2.0 specs for which link has been shared above. You may also want to visit the Github page and see how to compile from source on your platform. If you need any help, do post in the comments section and I can surely help.

Happy Bot Building!

Programmatically Triggering a Task Scheduler Job with Windows Events

Intro

Do you use Windows Task Scheduler to run scheduled batch jobs? It could be used for a great deal of things, e.g. sending out scheduled email reports or run a machine processes at a timed interval. I use it for running daily data imports, running optimization scripts on the database, sending scheduled emails and Push Notifications etc. In this quick tutorial, I will show you how to trigger a scheduled job programmatically. And just in case you are thinking, what’s the big deal? Use SCHTASKS, then you should already know that SCHTASKS fails at running tasks that require elevated permissions on the same local server instance.

Problem Statement

Why trigger a scheduled job programmatically?

Task Scheduler is used to schedule a batch job / script / program to run at a schedule. Software like Google Chrome, Apple iTunes, Graphics Drivers, Adobe Flash, etc. all schedule jobs in the Task Scheduler to check for updates. While these are fairly simple use cases and work perfectly well as long as you want to run the job on a schedule. The problem is when you want to trigger them manually. An example would be an Updater Script that runs periodically to check for software updates. Now what if you want to provide the end user a functionality to also trigger the update check manually by pressing a button on your app’s UI. Or what if you have a financial web app running on a server that uses Task Scheduler to send out monthly billing statements to customers. Now you would need to give your users a way to request for the last statement at the press of a button as well, else you risk having frustrated customers.

Why not use SCHTASKS?

Ok for those who don’t know, SCHTASKS is the command line interface to Task Scheduler and can be used to create, edit and run jobs manually. Since it can run jobs, the first thought would be to kick off the job using a simple command like SCHTASKS /Run /TN <task-name>

From within C# you could use Process.Start to achieve the needful. Try the same thing if your job needs elevated permissions or another user account to run and you will see SCHTASKS failing. This is critical because a scheduled job may need to access restricted resources like an encrypted folder or perform administrative system functions. So how do we get around that?

Solution

Scheduled jobs can be configured to have triggers. The primary trigger in 90% of the use cases is a scheduled time when the job would need to kick off. However there are a few more trigger options as you will see in this screenshot below

You will notice that your job can be triggered by a number of trigger mechanisms, schedule being the most popular use case. But we want a programmable trigger. “On an event” is perfect for our case because we can generate an event in the Event Log. Let’s get working and see how to make it happen.

Step 1. Create the “Event Source”

You can use the eventcreate.exe command or use a PowerShell command shown below

Command Line

eventcreate /ID 1 /L APPLICATION /T INFORMATION  /SO MYEVENTSOURCE /D "My first log"

PowerShell 2.0

New-EventLog -LogName Application -Source MYEVENTSOURCE

What the above does is create a distinct Event Source for us. This allows are job to “listen” for events coming from a specific event source. If we define our own event source specific to our application, we can then take custom actions based on the eventid.

Now, let us review where we are:

  • We know how to create Scheduled Jobs/Tasks with Event based Triggers
  • We know how to create a Custom event source for our application
  • We can define a set of unique event IDs specific to our application (it can be any random numbers) that our code knows how to handle
  • What remains to be seen is how can we log an event into the Windows Event Log programmatically

Step 2. Create a Scheduled Task with an Event Trigger

The below screenshot explains it all. Scheduled Tasks are easy to create. Usually most tasks have a time schedule, for example, daily at 12 am. All we are doing different is adding a trigger whose trigger is an event, as shown in the screenshot below.

Now define the trigger to listen to an event as shown below

Notice how we picked our custom MYEVENTSOURCE in the Source select box. The number in the event id, 24311, is a custom event id and can be any number you want. You may have different tasks listen for event ids. For example, I may configure a job to run for Client A when the event ID is 24311 or a Client B when the event ID is 24312. It purely is our choice.

Step 3. Log an Event from code

You can do this with almost any programming language, but in my example I will use C#.


var source = "MYEVENTSOURCE";

try
{
    EventLog.WriteEntry(source, "Run Import", EventLogEntryType.Information, 24311);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Yep, now watch your Scheduled Task kick off automatically when this C# snippet logs an event.

Conclusion

Well, now you know this neat trick that can start your job programmatically on-demand. I have found this extremely useful in scenarios where I was earlier planning to use a Windows Service, but I could eventually void writing and configuring services by simply using the powerful Task Scheduler which is a part of your Windows OS and configuring programmatic event triggers. Hope this helps you in some way.