Lita and creating a recipe handler
For the past week, I’ve been playing around with Lita, a chat bot written in Ruby. Lita works within different chat protocols, irc being one of them. It does this with the help of a plugin which helps to connect to the chat protocol of choice. There are also handler plugins that will do various tasks such as welcome a user, image search in chat, and a karma ranking for popular words. I decided to get to work on writing my own handler.
It is actually very easy to begin using Lita. The author has very extensive documentation on this which can be found here. After installing and creating the new bot, I installed gems and configured them to work. What the configuration file does for Lita is let it know how to utilize the plugins. Here is an example of botfly, my current Lita bot.
What the above shows is that my bot will be connecting to irc channel #mostlybotfly, login as “botfly” and authenticated with a password. Below this is the connection to the local redis host, special term_pattern for the karma handler, as well as setting the api for my recipe handler. Documentation for various handlers could be find online, but in general they involve sending a command to Lita and getting a response in return. Below is an example of a handler for Lita that I had fun writing.
The lita-recipe handler will return a random top recipe from Food2Fork. The idea is that if you are in a chat and looking for a random recipe idea, you can have it on demand. Lita makes it easy to create handlers and can be quickly created using
lita handler NAME_OF_YOUR_HANDLER. For mine, I did
lita handler lita-recipe. From there, Lita creates a handler ruby file to create the actions to be carried out. I have provided an example of the lita-recipe code:
You can check out lita-recipe on my github page.
In the above, the handler is getting a response from Food2Fork’s search, using an API key which would have to be provided. After running some rspec tests to make sure the handlers was replying as needed, I completed the gemspec and deployed my first gem to rubygems! It was a little too exciting to be able to include
gem "lita-recipe" in my gemfile.
Working on Lita so far has helped to reinforce my interest and workflow as a developer. I hope to create another handler which will evaluate ruby code, much like irb for a chat room.