Created
March 14, 2011 17:20
-
-
Save mfojtik/869481 to your computer and use it in GitHub Desktop.
Deltacloud Teambox bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'cinch' | |
require 'json' | |
require 'pp' | |
require 'rest-client' | |
def client(uri) | |
RestClient::Resource.new("https://teambox.com/api/1/#{uri}", 'x', 'x') | |
end | |
class TeamboxWatch | |
include Cinch::Plugin | |
attr_accessor :last_id | |
timer 30, method: :timed | |
def timed | |
output = JSON::parse(client("projects/38811/activities").get) | |
activity = output['objects'].first | |
if activity['id']!=@last_id | |
object = output['references'].select { |r| (r["type"]==activity["target_type"]) and (r["id"]==activity["target_id"]) }.first | |
msg=parse_activity(activity['target_type'], object, output['references']) | |
pp msg | |
if msg | |
Channel("#deltacloud").send("TEAMBOX: #{msg}") | |
end | |
@last_id = activity['id'] | |
end | |
end | |
private | |
def get_person(person_id, references) | |
user_hash = references.select { |r| r["type"]=="User" && r["id"]==person_id }.first | |
"#{user_hash['first_name']} #{user_hash['last_name']}" if user_hash | |
end | |
def get_page(page_id, references) | |
page_hash = references.select { |r| r["type"]=="Page" && r["id"]==page_id }.first | |
"#{page_hash['name']}" if page_hash | |
end | |
def task_state(id) | |
case id | |
when 0 then 'NEW' | |
when 1 then 'OPEN' | |
when 2 then 'HOLD' | |
when 3 then 'RESOLVED' | |
when 4 then 'REJECTED' | |
end | |
end | |
def parse_activity(activity, reference, references) | |
case activity | |
when 'Person' then "#{get_person(reference['user_id'], references)} just joined Deltacloud API project" | |
when 'Comment' then "#{get_person(reference['user_id'], references)} on ##{reference['target_id']}#{reference['body'] ? ": "+reference['body'][0..120]: ''}" | |
when 'Page' then "#{get_person(reference['user_id'], references)} just updated page '#{reference['name']}'" | |
when 'Note' then "Note '#{reference['name'] || '--empty--'}' was added to page '#{get_page(reference['page_id'], references)}'" | |
when 'Task' then "#{get_person(reference['user_id'], references)} -> #{task_state(reference['status'])} | ##{reference['id']}: #{reference['name']}" | |
end | |
end | |
def client(uri) | |
RestClient::Resource.new("https://teambox.com/api/1/#{uri}", 'x', 'x') | |
end | |
end | |
def create_task(body, user) | |
task_body = { | |
"status" => "0", | |
"project_id" => 38811, | |
"task_list_id" => 56540, | |
"name" => "#{body}" | |
} | |
comment_body = { | |
"project_id" => 38811, | |
"body" => "Task added by @#{user}" | |
} | |
output = JSON::parse(client("projects/38811/task_lists/56540/tasks").post(task_body)) | |
if output['id'] | |
client("tasks/#{output['id']}/comments").post(comment_body) | |
else | |
raise Exception.new | |
end | |
return output | |
end | |
def task(id) | |
JSON::parse(client("task_lists/56540/tasks/#{id.to_i}").get) | |
end | |
bot = Cinch::Bot.new do | |
configure do |c| | |
c.server = "irc.freenode.org" | |
c.channels = ["#deltacloud"] | |
c.nick = "dcteambox" | |
c.verbose = true | |
c.plugins.plugins = [TeamboxWatch] | |
end | |
on :channel, /^!addtask (.+)/ do |m, text| | |
begin | |
task = create_task(text, m.user.nick) | |
m.reply "#{m.user.nick}: Task ##{task['id']} created! (https://teambox.com/projects/deltacloud/task_lists/56540/tasks/#{task['id']})" | |
rescue Exception => e | |
m.reply "#{m.user.nick}: Teambox API failure. Please try it again. (#{e.message})" | |
pp e.backtrace | |
end | |
end | |
on :channel, /^!task (.+)/ do |m, text| | |
begin | |
t = task(text, m.user.nick) | |
m.reply "#{m.user.nick}: ##{t['id']} #{t['name']}" | |
rescue Exception => e | |
m.reply "#{m.user.nick}: Teambox API failure. Please try it again. (#{e.message})" | |
pp e.backtrace | |
end | |
end | |
end | |
bot.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment