#++ # # :title: Google Latitude plugin for rbot # # Author:: Matthew M. Boedicker # # Copyright:: (C) 2009 Matthew M. Boedicker # # License:: GPL v3 # # Version:: 0.13 # # Can only locate Google Latitude users who have made their location public # by creating a public location badge: # # http://www.google.com/latitude/apps/badge # # A user's Google Latitude id can be found in the badge iframe src: # # # # would be user id 123456789 # # Example plugin session: # # @where add 123456789 username # @where list # @where is username # @where delete username # # Thanks: # Phillip Pi, for usability suggestions require 'json' class WherePlugin < Plugin def initialize super @users = @registry[:users] ||= {} end def save @registry[:users] = @users end def help(plugin, topic="") case topic when 'add' then <<-eos where add latitude_id name => add a Google Latitude user The latitude_id is obtained by making your location public in Latitude and creating a public location badge at http://www.google.com/latitude/apps/badge In the iframe src for the badge, the id is found in the "user" query string parameter. eos when 'delete' then 'where delete name => delete a Google Latitude user' when 'is' then 'where is name => get the current location of a Google Latitude user' when 'list' then 'where list => list known Google Latitude users' else 'get the current location of a Google Latitude user: where add|delete|is|list' end end def add(m, params) name, latitude_id = params[:name].to_s, params[:latitude_id] @users[name] = latitude_id m.reply("added Google Latitude id #{latitude_id} as '#{name}'") end def delete(m, params) name = params[:name].to_s id = @users.delete(name) if id m.reply("deleted '#{name}' (Google Latitude id #{id})") else m.reply("user '#{name}' not found") end end def list(m, params) m.reply("#{@users.size} known Google Latitude users") @users.sort.each { |name,id| m.reply "#{name} = #{id}" } end def is(m, params) user_id = @users[params[:name].to_s] if user_id url = "http://www.google.com/latitude/apps/badge/api?user=#{user_id}&type=json" @bot.httputil.get(url) do |resp| if resp['content-type'][%r{application/json}] parsed = JSON.parse(resp.body) properties = parsed['features'].first['properties'] location = properties['reverseGeocode'] long, lat = parsed['features'].first['geometry']['coordinates'] timestamp = Time.at(properties['timeStamp']).strftime( '%I:%M%P on %B %d, %Y %Z') m.reply("#{params[:name]} was in #{location} at #{timestamp} http://maps.google.com/maps?f=q&q=#{lat},#{long}") elsif resp['content-type'][%r{text/plain}] m.reply("Error response from Google Latitude: #{resp.body}") else m.reply("Error response from Google Latitude, view at #{url}") end end else m.reply("I don't know the Google Latitude user id for #{params[:name]}") end end end plugin = WherePlugin.new plugin.map('where add :latitude_id *name', :action => 'add') plugin.map('where delete *name', :action => 'delete') plugin.map('where is *name', :action => 'is') plugin.map('where list', :action => 'list')