haxoring campfire: or, roll your own api
Courtenay : April 6th, 2006
wanna write your own api to a service that doesn't offer one (*cough* campfire)
Here's how we went about writing a subversion post-commit bot for campfire.. Get out there and make some cool services!
step one:
set up a http request to grab some authentication.
#!/usr/bin/ruby
require 'net/http'
require 'open-uri'
require 'cgi'
domain = "yoursite.campfirenow.com"
sekrit_invite_link = "12345"
req = Net::HTTP::Post.new("#{domain}/#{sekrit_invite_link}")
req.set_form_data({'name' => 'my_bot', 'remember' => 'ON'})
res = Net::HTTP.new(domain, 80).start { |http| http.request(req) }
step two:
grab the cookie out of the response, as well as the room's location
room_id = res['location'].scan(/room\/(\d+)/).to_s
req2 = Net::HTTP::Get.new(res['location'])
req2.initialize_http_header({'Cookie'=>res['set-cookie']})
res2 = Net::HTTP.new(domain, 80).start { |http| http.request(req2) }
step three:
post a message to the room!
message = "Monkeys rule!"
req3 = Net::HTTP::Get.new("/room/#{room_id}/speak?message=#{CGI.escape(message)}")
req3.initialize_http_header({'Cookie'=>res2['set-cookie']})
res3 = Net::HTTP.new(domain, 80).start { |http| http.request(req3) }
Step four
customize your bot. For a subversion monkey, that last step starts something like
author = "Author: " + `#{svnlook} author -r #{ARGV[1]} #{ARGV[0]}`
paths = `#{svnlook} changed -r #{ARGV[1]} #{ARGV[0]}`
log = `#{svnlook} log -r #{ARGV[1]} #{ARGV[0]}`
message = [author, paths, log].join("\r\n")
req3 = Net::HTTP::Get.new("/room/#{room_id}/speak?paste=true&message=#{CGI.escape(message)}")
The paste=true is important because it tells campfire to display the message as a "paste".
The lesson
Give us an API, or we'll write our own! Oh yeah, and if the service requires ajax? Hah, we can fake it!
req.add_field 'X-Requested-With', 'XMLHttpRequest'
req.add_field 'X-Prototype-Version', '1.5.0_pre0'
5 Responses to “haxoring campfire: or, roll your own api”
Sorry, comments are closed for this article.
April 6th, 2006 at 01:36 PM And the lesson is the most important part.
April 7th, 2006 at 03:57 AM Impressive!
April 7th, 2006 at 08:36 AM Here's a tiny (and sort of hacked-together) chat bot: http://www.mdaines.com/system/chatbot.zip It polls every 15 seconds, looking for the word "cheese". If it sees that, it says "Did someone say cheese?"
April 12th, 2006 at 10:46 AM What version of ruby are you using? I have a feeling you need something newer than 1.8.2
April 12th, 2006 at 12:12 PM yeah, it needs 1.8.4. it's fairly easy to convert to 1.8.2 though, you just have to define the headers differently.