Skip to content

Instantly share code, notes, and snippets.

@tomlea
Created April 27, 2009 00:33
Show Gist options
  • Select an option

  • Save tomlea/102232 to your computer and use it in GitHub Desktop.

Select an option

Save tomlea/102232 to your computer and use it in GitHub Desktop.
A script to retweet any direct messages sent to the account.
require "rubygems"
gem "twitter4r"
require "twitter"
require "active_support"
Twitter::Client.configure do |conf|
conf.application_name = "Twitter Echo"
end
username, password = ARGV.shift, ARGV.shift
STDERR.puts "Usage #{$0} <username> <password>" unless username and password
exit(1)
client = Twitter::Client.new(:login => username, :password => password)
history_path = "#{username}.yml"
while true
begin
if File.exist? history_path
handled_messages = YAML.load(File.open(history_path)) || Set.new()
else
handled_messages = Set.new()
no_post = true
end
client.messages(:received).map{|m| [m.id, m.sender.screen_name, m.text] }.reverse.each do |id, user, message|
client.status(:post, "#{message} [@#{user}]") unless no_post or handled_messages.include?(id)
handled_messages << id
end
rescue => e
STDERR.puts "#{e.class.name}: #{e.message}"
e.backtrace.each{|l| STDERR.puts "\t#{l}" }
STDERR.puts
ensure
File.open(history_path, "w"){|f| f.puts YAML.dump(handled_messages) }
end
sleep(300)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment