Tad Thorley

creative commons license rss feed comments feed powered by Howex valid xhtml Add to Technorati Favorites

My first rbot plugin

Here's a plugin for rbot that queries google's calculator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
require 'rubygems'
require 'hpricot'
require 'open-uri'

class GcalcPlugin < Plugin

  def help(plugin, topic="")
    "gcalc <statement> => do some conversions/calculations using Google's calculator"
  end

  def retrieve(m)
    request = m.params.gsub(" ", "+")
    doc = Hpricot(open("http://google.com/search?q=#{request}"))
    response = (doc/"h2.r").first.to_plain_text.gsub(/\[|\]/, "")
    m.reply response
  end

  def privmsg(m)
    request = m.params.gsub(" ", "+")
    doc = Hpricot(open("http://google.com/search?q=#{request}"))
    response = (doc/"h2.r").first.to_plain_text.gsub(/\[|\]/, "")
    m.reply response
  end 
end

plugin = GcalcPlugin.new
plugin.map "gcalc *request", :action => 'retrieve'

3 Responses to “My first rbot plugin”

 1.   Willie

August 30th, 2007

Your code looks good, although I don’t know Ruby. Consider “bad” user input. If I type in a phone number, I would get back “Phonebook results for #####”. If I type in a generic search term, I would get the title of the first search result. Nothing that your code or Google does wrong, I just thought I would look and make sure that it didn’t respond with a page full of garbage (given the garbage input).

One more: typing in a UPS tracking number would seem to return “Track UPS package 1ZA….......”

Then again, I don’t know Ruby and could be misinterpreting what I read in the code.

 2.   Jan Wikholm

August 31st, 2007

Thanks, I put it to use right away :)

 3.   Tad

September 6th, 2007

@Jan—glad it was useful

@Willie—I’m just passing a query string to Google and returning the top result. If people want to pass garbage, I don’t care if they get garbage back. I mostly wanted a quick reference when I’m using IRC.


Sorry, comments are closed for this article.