Created
January 24, 2010 18:57
-
-
Save svperfecta/285370 to your computer and use it in GitHub Desktop.
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 "uri" | |
require "yajl/http_stream" | |
require "twitter" | |
# Campfire Setup | |
token = 'yourtoken' | |
room_id = 111111 | |
url = URI.parse("http://#{token}:[email protected]//room/#{room_id}/live.json") | |
# Twitter setup | |
twitter_auth = Twitter::HTTPAuth.new('yourname', 'yourpasswordh') | |
twitter_pattern = Regexp.new('@test\s', Regexp::IGNORECASE) | |
twitter = Twitter::Base.new(twitter_auth) | |
# Monitor the Campfire feed then tweet | |
Yajl::HttpStream.get(url, :symbolize_keys => true) do |message| | |
puts "Received #{message.inspect}" | |
# We only care about text messages, with valid bodies, that contain the case insensitive word '@twitter' at the beginning of the message | |
if message[:body] and | |
message[:type] == 'TextMessage' and | |
message[:body].index(twitter_pattern) == 0 | |
tweet = message[:body].sub(twitter_pattern, '').strip | |
# Let's tweet | |
if tweet.length > 0 | |
puts "Attempting to post: #{tweet}" | |
# Make three attempts, then bail | |
attempts = 0 | |
begin | |
twitter.update(tweet) | |
rescue Exception => e | |
attempts += 1 | |
retry if attempts <= 3 | |
puts "Failboat. Damn Twitter. On the last attempt we got #{e.message}" | |
end | |
end | |
end | |
end | |
puts 'Failed for some reason?' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment