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.

 

Why It’s Important to do Unpaid Open Source as a Paid Developer

Programming is an art. And just like any other art, it can be distilled down into easily digested parts that you can profit from. The problem is that by putting this profit layer on your art, you’re psychologically more likely to do less. This was shown in a study done by Desmond Morris in 1962, and witnessed by anyone who’s ever gone from doing code because they loved the challenge, to doing it for a paycheck. The luster of the quest for control loses its sheen, and suddenly you’re just gathering tickets and knocking out patches.

That’s why I say it’s important to add that layer of unpaid open source into your routine. And going off of the study I linked to above, the earlier the better. The chimps in the study permanently stopped enjoying their artwork as much as they had before being paid for it. If you can trick yourself into not looking at the paycheck as the product of your code — or artwork — and instead look at it as simply paying you to show up, you’re a step ahead.

This is easier if you’re salaried. If you have the freedom to not go into work one day, and it won’t affect your paycheck, then you’re effectively not getting paid to do your artwork. You’re just getting paid to be.

But the easiest way, by far, to trick your mind into working better is by doing your artwork for free. Find an open source project that you love, open the tickets, and fix one of them. It’s that simple. Submit your patch, and then walk away. Don’t stick around and wait for the merge, don’t refresh the screen until you’re single handedly DOSing Github. Just walk away. Check your email later, or just go find another ticket, and keep doing it. Do it for the love of the code, for the search for the answer, and feeling you get when you hit submit.

Just don’t do it for money.

Thank you, Netflix, for telling me I have a problem…

I absolutely love the fact that Netflix has most of the shows that I love to watch in it’s streaming catalog. The technology itself is a display of engineering genius, being able to direct high quality content to my Xbox without a lack of quality and lag. That said, in their efforts to expand and improve their customer service, they’ve just gone too far.

I like series. I like watching them. I like watching the stories unfold over multiple episodes. And I binge, I binge on entire seasons at a time. This was never a problem before. Now it is. Because now, it seems, every 3 or 4 episodes, it pops up a message asking me if I’m still watching.

This isn’t an issue on the website, but on the Xbox, it’s a pain in the ass. I’m watching TV, watching a series, I get involved in it, get comfortable, and want to just relax. The controller is across the couch, and I’m relaxed, cuddled up under a blanket with a drink and my wife. And then that damned box pops up.

No, Netflix…I’m not watching…that’s why you’re still on.

Normalization – Use It

So I might just be a bit of an overzealous code nerd, but whenever I download a snippet of code, I always analyze it, and try to make sure it’s up to snuff. So it was no surprise that I did this when I was looking at an email newsletter plugin that I was thinking about using. The problem this time was I didn’t even get to the code before stopping.

Wrong format

  1. admin@gmail.com,admin1@gmail.com,     (Comma at the end)
  2. admin@gmail.com,,admin1@gmail.com     (Two comma)

Why is this bad? It’s called Normalization, people…

…use it. Being the devoted coder that I am, I managed to figure out the solution to this dilemma, in less characters than it took to type that instruction to the end user.

This uses trim to strip away any leading or trailing commas–the first incorrect format–and then uses str_replace to replace any commas that are doubled up into a single comma–the second incorrect format. Simple, efficient, and a single line of code. It can be run when you validate that you actually have a value in that field… I mean…you do validate that, correct?

Why is normalization so important?

To put it simply, the end user doesn’t always know what they’re doing. To make a truly usable product with a wide appeal, you need to normalize the input so you can reasonably predict what’s going to be there. In the example above, the developer ignores this aspect of development, and instead puts the responsibility of input sanitation on the end user. This isn’t good design, it’s also not good customer service.

The end user doesn’t care what’s entered into the box, as long as what comes out on the other side is correct. Good software design should be able to interpret user input in such a way that it can say, “Hey, you probably didn’t mean to do that, so let me fix that for you. It’s okay, no big deal, it’s what I’m here for.”

Get It? Normalize?

Get It? Normalize?

The New TravisWeston.com

If you’re a repeat visitor, you’re probably noticing that this is different than it was before. I’ve gone and done it again. I’ve created a new instance of TravisWeston.com, and removed the old posts and files.

Why?

Well, the basic reason is that I wanted to get back to promoting my programming. I fully intend to move my writing blog to it’s own website, and I’ll post a link when that happens. Until then, it’s back to the freelancer grind for me!