Node Bot – An IRC Bot with a Brain

So I decided to take my weekend and learn how to use Node.js. It sounds like a tall order, but I didn’t think so. I’ve been a JavaScript programmer for about 13 years now, so I didn’t feel it would be that different.

It wasn’t.

The biggest thing I had to learn was npm, the Node.js Package Manager, which makes adding features to Node.js pretty simple.

Now, to rewind just a bit, I’ve also recently been trying to learn Python, and the first thing you learn about Python is that if you want to do it, someone else already has, so just install the package and use their framework. But I’m still learning Python’s syntax.

But I know JavaScript.

So here I am, with the simple to use package idea of Python coupled with the syntax of JavaScript that I already know, with only a few new things tossed in. The major thing I had to learn was how to package my code into multiple files. On the web it was easy, create files, include them with HTML. Can’t do that in Node.

But it was a simple matter of learning that single process, and after that I was able to port the entire concept into a project.

Whenever I learn a new language, or a new system like Node.js, I do the requisite “hello world” examples, but then I say, “Ok, I’m going to spend some time and put together a project.” I do this so I can feel it, I can feel how the code works top to bottom, and see the errors, handle the errors, and find the work around that I’m going to need when I tackle it in a production environment.

I own an IRC server on the Jaundies network. It’s sphinx.jaundies.com you can get on unsecure on port 6667 or secure on port 7001. It’s a self signed certificate, so you might need to approve it. I tell you this, to lead to this: in one of the many channels I sit in on Jaundies, there are two other developers who have been toying with IRC Bots, and practicing their development skills. So when I hit Node.js, the first idea I had was to try to build an IRC bot. It was perfect for two reasons. First, it was something I’ve done before, and I understand the concepts. And second, it’s something I couldn’t do easily in JavaScript in the browser. It’d show me the power behind Node.

So I did it, it took me about an hour, and I had a very basic Node IRC Bot running. It did nothing but greet itself when it hit the room, but it was able to join and stay alive without issue. I did this by using the Node IRC package.

But, I wanted more. That was simple, and it made me really want to expand my knowledge.

Since I was 13 or so I’ve been in love with the concept of artificial intelligence, specifically neural networks. I’ve had this idea for a while to learn how to build a proper neural network, but I had yet to find a neural network framework that allowed it. FANN — Fast Artificial Neural Network — is a C neural network framework that has many ports, but none had been put together and easily documented enough for me to learn.

Then I found Node FANN. Node FANN was a Github project I found that wasn’t documented at all, but the code was readable, so I was able to figure it out.

So I ended up spending the weekend adding a Neural Network to this IRC Bot. What it does now is joins a channel, and builds the nick list. It then randomly selects anyone in the channel, and watches what all the people write. If the person whom it is watching speaks, it goes into training mode.

What it’s doing is using up to the 5 latest messages from other people as inputs, and training the neural network to output whatever the person the bot is watching would output.

Is this a great idea for a neural network? Hell no, it’s a horrible idea. But it was fun to write, it taught me how to convert letters to numbers without using a key map, and I was able to learn a bit about how Node works. I’d say I know Node enough now to be comfortable working with it in my day job. That’s how simple it is.

So why write this post? No reason. It’s been a while since I blogged, and I wanted to spread the love about Node.js that I’m feeling. I’m still a PHP guy at heart, but I’m adding Node.js as a permanent tool in my toolbox.

If you’d like to play around with the bot, or if you have any ideas about how to make the Neural Network work better than it does, feel free to fork my repo on Github. I’ll keep playing with it, and adding things here and there.