How to Auto-Follow on Twitter by Twitter API v1.1 & Ruby

The following code is a ruby version of “auto-follow” script introduced in the previous post: How to Auto Follow on Twitter by Twitter API & PHP.

Whole Code for Twitter Auto-Follow

#!/usr/bin/ruby

require 'rubygems'
require 'twitter'

USERNAME = 'your_twitter_username'
CONSUMER_KEY = 'your_consumer_key'
CONSUMER_SECRET = 'your_consumer_secret'
OAUTH_TOKEN = 'your_oauth_token'
OAUTH_TOKEN_SECRET = 'your_oauth_token'

client = Twitter::Client.new(
  :consumer_key => CONSUMER_KEY,
  :consumer_secret => CONSUMER_SECRET,
  :oauth_token => OAUTH_TOKEN,
  :oauth_token_secret => OAUTH_TOKEN_SECRET
)

follower_ids = []
client.follower_ids(USERNAME).each do |id|
  follower_ids.push(id)
end

friend_ids = []
client.friend_ids(USERNAME).each do |id|
  friend_ids.push(id)
end

client.follow(follower_ids - friend_ids)

How to Execute

Copy & paste the code above to a file and save it as auto_follow.rb.

Note that you need The Twitter Ruby Gem, which is a Ruby interface to the Twitter API. You can install it with gem command.

$ gem install twitter

After install, the auto-follow script in Ruby will work as it should.

$ ruby auto_follow.rb

2 thoughts on “How to Auto-Follow on Twitter by Twitter API v1.1 & Ruby

Comments are closed.