Contract Work

Monday, December 16, 2013

Look what my Bot can do!

It’s amazing what a little chatbot can accomplish. It can make your day much brighter and more fun!! So, let’s talk about what Rosie can do, because it’s a lot.

So, first, Rosie can do some of the standard things like reply with gifs about coffee, District Taco, and lol’s. She also responds when you say good morning so that everyone every morning gets a fun response. She reacts to you when you say you love or hate coding, and she’s all about refactoring.

Those commands are accomplished via regular expressions. Regular expressions are a concise, flexible way to pick out specific words, phrases, characters, etc. Regular expressions look really terrifying when you first encounter then, but are actually really handy and not so scary once you get a handle on them. Based on Nell’s talk/suggestion at RubyConf, I made a permalink for each regex and put it as a comment in my code so I can always go back to it later. I used rubular when crafting the regex’s which made them a breeze. (Okay, not a breeze at first, but made it much easier to learn and figure out what each character did and how it altered what I was trying to do).

The next set up functions are random things that Rosie will say periodically.
on :message do |m|
  if rand(500) == 0
    m.reply "SQUIRREL!"
  end
end
This basically say that for each message sent, the script will see if that number matches 0 and if it does, then the bot will reply with “SQUIRREL!”. There’s another one of these built in there as well, but people will have to wait to see what that is.

Finally, there are some responses where rosie can choose to respond as a reply or as an action.
These options look like this:
on :message, /#{config.nick}\?/i do |m| 
  response = [
    [:reply, "Did someone ask for me?"],
    [:reply,    "http://31.media.tumblr.com/fe021d747a605a8a7cba5767011251e1/tumblr_mjpo4q44aj1rjatglo1_500.gif"],
    [:reply, "what do you want now?"],
    [:reply, "#{m.user.nick}, why are you bothering me?"],
    [:reply, "it wasn't me."],
    [:reply, "yyyyeessss?"],
    [:action, "hides"],
  ]
  msg = response.sample    
  if msg.first == :action   
    m.channel.action msg.last 
  else
    m.reply msg.last  
  end
end
So, each line…
The first line says that on the message whatever the configured nickname of the bot is (meaning that I can change Rosie’s name and the regex will still work) followed by the “?” then (next line) Rosie will reply with one of the options in the array. The next few lines show the options that Rosie can reply with and whether they are a reply or an action. A reply would look like this:
Rosie: It wasn’t me
While an action would look like this:
*Rosie hides
Then, msg = response.sample says picks a random value from the message array. (Next line) if the first element in that array is an action (:action), then (next line) add /me to it when it replies in the channel. Otherwise, reply like normal.

The next post will talk about functionality that incorporates some more complicated methods and a database!

No comments:

Post a Comment