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.