User interfaces for software applications can come in a variety of formats, ranging from command-line, graphical, web application, and even voice. While the most popular user interfaces include graphical and web-based applications, occasionally the need arises for an alternative interface. Whether due to multi-threaded complexity, concurrent connectivity, or details surrounding execution of the service, a chat bot based interface may suit the need.
Chat bots typically provide a text-based user interface, allowing the user to type commands and receive text responses. Chat bots are usually stateful services, remembering previous commands (and perhaps even conversation) in order to provide functionality. A popular example from recent Internet history is the usage of IRC chat bots, often used for monitoring chat rooms, playing games, and providing help. The same technology can be used today, but to provide even more powerful functionality, such as serving business processing requests, multi-threaded processing, or any multi-threaded task that may not be ideal within a web application. When chat bot technology is integrated with popular web services, such as Gmail / GTalk, the chatbot can be utilized securely by an even larger audience.
In this tutorial, we’ll create a C# .NET Jabber chat bot, using the XMPP Jabber protocol. Our chatbot will be compatible with Gmail and run on the Gmail / GTalk XMPP server, allowing users to access the chat bot interface directly from within their Gmail account. Our C# .NET Jabber chat bot will execute commands to list the file size on its hard drive. To keep things interesting (for both the developer and the user), our chatbot will also utilize AIML to start a conversation with the user.
Also available as a YouTube video.
An often overlooked feature of Gmail is the GTalk feature, which allows users to instant message IM other Gmail users, directly from within their Gmail account. By default, Google’s instant messaging uses the XMPP Jabber protocol for communication. In this manner, our chat bot can login to Gmail’s XMPP server and listen for incoming requests. Any instant messages sent to the Gmail user will be forwarded to our chat bot software, allowing us to process and return a result.
Of course, we’ll first need a Gmail account to host our C# .NET Jabber chat bot. You can create one by signing up to Google Gmail. After signing up, the account can be used by the C# .NET Jabber chat bot to communicate. A user who wishes to communicate with the chat bot will need to enter the name of the chat bot under the “Chat” section in Gmail, and request an invite to talk. Once connected, the user can directly communicate with the C# .NET jabber chatbot.
There are a couple of technologies that need to be defined before stepping into creating the C# .NET chat bot:
Jabber is an open-source technology for communicating.
Jabber uses the XMPP protocol to communicate.
AIML is an artificial intelligence markup language.
AIML provides a system of XML files for requests and responses to communicate with a human.
We can combine Jabber XMPP with AIML and execute within Gmail to provide a fully functional and secure chatbot software application.
With the technologies defined, we can now step into creating the C# .NET Jabber chatbot. For the XMPP protocol, we’ll be using the agsXMPP SDK. For our brain, AIML, we’ll be using the popular ALICE SDK from the Artificial Intelligence Foundation. AIML will provide a human conversation feature for our Jabber chat bot (while business chat bots may be designed primarily to perform business tasks, it doesn’t hurt to communicate like a human). To get started, we’ll need to setup a connection to Gmail’s XMPP service. We can do this by initializing and connecting to Gmail at talk.google.com with the following code:
Rather than providing hard-coded values for the XMPP server information, we can move this into an App.config file for easier management. This will change our code as follows:
Notice in the above code, we’ve provided a means for allowing the console user to enter a password when running the C# .NET chatbot. Since our program is a Windows console application, we have the option to enter the password within the App.config, or to provide a small amount of security, and have the user manually enter the password within the client application at runtime.
We can actually hide the password being typed in the C# .NET console application by using the following helper method:
The above method provides a small amount of security while entering the Gmail account password, allowing the C# .NET jabber chatbot to login to the XMPP service.
We’ve also refactored the XMPP configuration to take out the hard-coded server and login parameters, and have replaced them with a C# .NET custom configuration section.
By removing the hard-coded login details, we can implement a custom configuration section, allowing our App.config to appear as follows:
We can implement the C# .NET custom configuration section with the following two classes:
With the above defined, we can access our C# .NET Jabber Gmail chatbot configuration with the following line of code:
Once the C# .NET Jabber chatbot is logged into the Gmail / GTalk service, we’ll enter a while loop within our console application. Since we’ve setup event handlers to receive incoming requests and respond, we can simple leave the program running. While the ideal solution would be to create a Windows service application to host our chatbot, for simplicity, we’ll leave a basic console application running.
While running, our chatbot will respond to the supplied events: OnLogin, OnError, and OnMessage. Of course, the most important event is OnMessage, indicating we’ve receiving an incoming instant message from a user to our chatbot.
Upon receiving an instant message on Gmail or the Jabber service, our chatbot will execute the OnMessage event handler. Within this event, we’ll process the message, to perform any service or functionality, and return an output to send back as an instant message. We can implement the OnMessage event handler as follows:
Note, in the above code we get the Gmail username from the incoming instant message. We’ll use this to provide context to our chatbot. This allows our chatbot to remember conversations and state, responding accordingly to subsequent commands or conversation. AIML has built-in support for User context, although we’ll still need to maintain a hashtable of users (identified by Gmail username) on our end.
Once we’ve received the instant message, we’ll need to process the text for any commands that we recognize. We handle this processing within the HandleMessage() method, defined as follows:
In the above code, we simply take the incoming instant message and check for any keywords or commands that we recognize. You can use your imagination to expand this functionality to any number of tasks, such as IRC bot functionality, script execution, or any number of business process features. For enhanced architecture, you could even abstract the if-then decision into a Strategy design pattern, and with the use of reflection, allow your chatbot to respond dynamically at runtime to new commands and requests (simply by dropping a new DLL into the executable folder).
In our example, we’ll simply recognize the command “DISK SPACE” in any sentence, as well as the command “DISK SIZE”. If found, we’ll perform some processing, such as accessing the C# .NET DriveInfo object, and return the output.
If a command is not recognized, we’ll pass the incoming instant message to our AIML chatbot instance to process and return a result. The result will then be sent back to the end user as a response. Of course, to process the instant message with AIML, we’ll need a brain within our C# .NET Jabber chatbot.
Our chatbot will directly handle recognized commands and return a response manually. We’ve provided this functionality directly within the HandleMessage method. However, for unrecognized commands, we’ll pass the instant message to our AIML bot to process. AIML will return a response in conversation format. We can create an AIML bot as follows:
In the above code, we’ve created a new class which wraps the AIMLbot.Bot class, provided by the AIML SDK. We initialize the C# .NET AIML bot and provide it with a list of AIML XML files (located within the /bin directory of our C# .NET console application).
Since AIML requires a series of XML files in order to process responses, we’ve included them within the Visual Studio project solution. We’ll need to copy these files to the executable directory each time. We can do this automatically with a post-build event step as follows:
The above command simply copies the two folders “aiml” and “config” into the solution’s /bin/Debug or /bin/Release folder.
The last method in our AIMLChatBot wrapper class is GetOutput, which simply calls the Chat() method of the AIML SDK to process and return a response. Since we pass in the User object for context, our AIML chat bot is able to provide context and specific conversation based upon each unique user. This allows our chatbot to remember the user’s name, identity, and other conversation features. We can also leverage this context for business functionality and stateful service within our application.
The default implementation of our C# .NET Jabber Gmail chatbot uses AIML for its “brain”, in order to provide conversation. However, the chatbot can actually be customized to provide different personality, simply by editing the AIML XML files. You can experiment by editing the files in config/ such as config/DefaultPredicates.xml to provide names and background details, etc.
It may be debatable whether a particular business problem can be best solved with the use of a C# .NET Jabber chatbot, as opposed to a C# ASP .NET web application. However, particular cases for a chatbot being more feasible over a web application may involve issues regarding multi-threaded and blocking functionality. Consider the execution of a server-side script or long-running process. Within a web application, the web page would need to serve the user a waiting page and notify the user upon completion. A chatbot, however waits by nature. You send an instant message to the chatbot. Process execution begins. The chat bot has the option to reply back with status and completion. While execution in a multi-threaded environment and the associated complexity is similar to a web application’s, a chatbot may offer a more streamlined solution, depending on business needs.
Another aspect of chatbot solutions is flexibility in the command-line driven user interface. The interface offers a more flexible and expressive means of executing a variety commands, often provided with less underlying framework on the developer’s part.
An additional added benefit to utilizing a chatbot, particular when combined with a popular and secure service such as Gmail, is the built-in security and access-point from the 3rd-party service. This allows more users to easily access the software and execute business services without the need for hosting concerns. Of course, the chatbot itself, will still need to be hosted on a server (whether central or decentralized).
You can download the project source code on GitHub by visiting the project home page.
A C# .NET Jabber chat bot application, running within the context of the popular Gmail service, can provide a variety of business functionality, including execution of tasks, information gathering, and even simulated human conversation. By integrating the Jabber XMPP protocol with the AIML chatbot SDK, an application can provide an experessive user interface and interaction, possibly providing a greater human touch to traditional user interfaces.
This article was written by Kory Becker, software developer and architect, skilled in a range of technologies, including web application development, machine learning, artificial intelligence, and data science.