0

I'm trying to write a bot using ruby and the twitter gem.

Here is my code :

 #!/usr/bin/env ruby

require 'twitter'


@client = Twitter::REST::Client.new do |config|
  config.consumer_key        = "xxx"
  config.consumer_secret     = "xxx"
  config.access_token        = "xxx"
  config.access_token_secret = "xxx"
end

@last_tweet = @client.search("#Hashtag").first

while true
  puts "Bot is searching"
  puts @last_tweet.text
   if @last_tweet.id != @client.search("#Hashtag").first.id
      puts @last_tweet.id
      puts "bot found another tweet. retweet!"
      sleep 1
      @client.retweet @last_tweet
      @last_tweet = @client.search("#Hashtag").first
      puts "the last tweet is now : #{@last_tweet.text}"
   end
  sleep 5
end

The goal is to simply retweet any tweet with "#hashtag". Now, the bot is behaving very strangely. For some reasons it's sometimes randomly seem to RT the same tweet twice, causing it to crash.

I tried for hours, I even copied this gist : https://gist.github.com/nilsding/834c2fe8829d29b79e23

Which have the exact same issue.

How can I make it not retweet the same tweet ?

I've already checked this question but can't understand how to apply it to my simple ruby file.

Community
  • 1
  • 1
  • One can not rely on what twitter returns to `search`. You should explicitly store the ids of retweeted tweets locally. – Aleksei Matiushkin Mar 04 '16 at 05:35
  • So I need a whole database then ? Would you know if SQLite 3.0 be okay? – Iloverails Mar 04 '16 at 06:37
  • It depends on what are your retweet volumes :) Anyway, you do not need to store tweets forever. Run a job that will clean up everything older than, say, 1 day. In this case SQLite is definitely enough. – Aleksei Matiushkin Mar 04 '16 at 06:39

0 Answers0