Create a twitter bot using Ruby
i wanted to favourite all tweets that are talking about ruby and rails, that gets some attention to my twitter account.
require 'Twitter' #gem install twitter
while true
begin
# Create a read write application from :
# https://apps.twitter.com
# authenticate it for your account
# fill in the following
config = {
consumer_key: '',
consumer_secret: '',
access_token: '',
access_token_secret: ''
}
rClient = Twitter::REST::Client.new config
sClient = Twitter::Streaming::Client.new(config)
# topics to watch
topics = ['#rails', '#ruby', '#coding', '#codepen']
sClient.filter(:track => topics.join(',')) do |tweet|
if tweet.is_a?(Twitter::Tweet)
puts tweet.text
rClient.fav tweet
end
end
rescue
puts 'error occurred, waiting for 5 seconds'
sleep 5
end
end
Written by Emad Elsaid
Related protips
4 Responses
Do i need to install any thing before i run this bot?
twitter gem and create a twitter app with read and write paermissions
i finally got it! I have installed twitter gem and i am running your codes on terminal. It runs however, it just runs in the back ground and i have no idea what it is doing on twitter.
Please advise
Here is the message that i get in terminal;
"{:status_code=>401, :header=>{"connection"=>"close", "Cache-Control"=>"must-revalidate,no-cache,no-store", "Content-Type"=>"text/html", "date"=>"Sun, 27 Apr 2014 04:21:46 UTC", "x-connection-hash"=>"d0b88100384f4fbf0c975b00e1147783", "content-length"=>"310"}}
error occurred, waiting for 5 seconds;
BTW thanks for sharing your ruby bot codes :D