我正在尝试使用 ruby 和 twitter gem 编写一个机器人。
这是我的代码:
#!/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
目标是简单地转发任何带有“#hashtag”的推文。现在,机器人的行为非常奇怪。由于某些原因,它有时会随机发送两次相同的推文,导致它崩溃。
我尝试了几个小时,我什至复制了这个要点:https ://gist.github.com/nilsding/834c2fe8829d29b79e23
哪个有完全相同的问题。
我怎样才能让它不转发相同的推文?
我已经检查了这个问题,但不明白如何将它应用到我的简单 ruby 文件中。