Ruby Twitter Bot

I been trying to help a friend get more attention to his twitter and his music. I choose to try to automate search results and reply back to each user with a message. Upon doing this, I discovered it would take two ruby gems.

First was twitter gem:

sudo gem install twitter

second is tweetstream:

sudo gem install tweetstream

The Twitter gem has streaming aspects in their latest version 5.8.0 say its experimental so I didn’t use it. This is a simple simple bot to run. The main hurtle is to tweet to users that are tweeting about my search field, is that I needed not to get caught by twitter.

My Idea was get a large amount of sentences together and randomly select one of them and tweet it to that user. Each time I tweeted I would let the script sleep for a while anywhere be tween 1 and 120 seconds. Since this is done with a random generator, Most of the time the random number is above 1 minute. Since the tweets are spaced apart time wise, the sentences where different enough to fool Twitter. I’ve tested twice and each time I was able to keep it running for a few hours with no problems.

here’s the script:

require 'twitter'
require 'tweetstream'


count = 1
tweets = []
api_keys = {
          consumer_key:         '1234,
          consumer_secret:      '1234',
          access_token:         '1234',
          access_token_secret:  '1234'

file ="tweets.txt", "r").each_line do |line|
  tweets << line

client = do |config|
  config.consumer_key        = api_keys[:consumer_key]
  config.consumer_secret     = api_keys[:consumer_secret]
  config.access_token        = api_keys[:access_token]
  config.access_token_secret = api_keys[:access_token_secret]

TweetStream.configure do |config|
  config.consumer_key         = api_keys[:consumer_key]
  config.consumer_secret      = api_keys[:consumer_secret]
  config.oauth_token          = api_keys[:access_token]
  config.oauth_token_secret   = api_keys[:access_token_secret]
  config.auth_method          = :oauth

puts "Api keys conected ...\n"

begin do |message|
    puts message
  end.track('keyword', 'keyword2') do |status|
    tweetn = rand(0..tweets.count)
    sleepn = rand(1..120)
    client.update("@#{status.user.screen_name} #{tweets[tweetn]}")
    printf "%-5s %s\n\n",
            "#{count}: #{status.user.screen_name.rjust(10)}",
    sleep sleepn
    count += 1
rescue Twitter::Error
  puts "This request looks like it might be automated."
rescue Exception
  puts "some other error happened!"

comments powered by Disqus