Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Simple Twitter Bot with Ruby

Cody Norman
December 10, 2015

Simple Twitter Bot with Ruby

Use the Twitter Ruby gem to interact and automate actions on Twitter from PhillyRB / PhillyJS beginner meetup

Cody Norman

December 10, 2015
Tweet

Other Decks in How-to & DIY

Transcript

  1. Build a Twitter bot with Ruby Interact with Twitter’s API

    in a few lines of Ruby Cody Norman / @cnorm35
  2. Setting up your API keys You’ll need a Twitter account.

    Head to www.apps.twitter.com to create a new app to get your keys. Tip: If you want to set up a new account just for the bot, use a Google voice number to register for a new account
  3. Let’s Write Some Code! Now that we have our keys,

    we’re ready to get to work. https://github.com/sferik/twitter From your terminal install Twitter gem with: $ gem install twitter
  4. Let’s Write Some Code! Gems aren’t just for Rails, we

    can use them in ruby projects also. Setting up our client to make requests to Twitter: require 'twitter' client = Twitter::REST::Client.new do |config| config.consumer_key = ENV[CONSUMER_KEY] config.consumer_secret = ENV[CONSUMER_KEY] config.access_token = ENV[ACCESS_TOKEN] config.access_token_secret = ENV[ACCESS_TOKEN_SECRET] end Config Keys for @hitchbot_ghost
  5. Let’s Write Some Code! Gems aren’t just for Rails, we

    can use them in ruby projects also. Setting up our client to make requests to Twitter: require 'twitter' client = Twitter::REST::Client.new do |config| config.consumer_key = ENV[CONSUMER_KEY] config.consumer_secret = ENV[CONSUMER_KEY] config.access_token = ENV[ACCESS_TOKEN] config.access_token_secret = ENV[ACCESS_TOKEN_SECRET] end
  6. Let’s Write Some Code! Check your configuration with a test

    Tweet require 'twitter' client = Twitter::REST::Client.new do |config| config.consumer_key = ENV[CONSUMER_KEY] config.consumer_secret = ENV[CONSUMER_KEY] config.access_token = ENV[ACCESS_TOKEN] config.access_token_secret = ENV[ACCESS_TOKEN_SECRET] end #tweet with random number so you can find yours on the shared account #if using your own keys, you don’t have to worry about my_number my_number = rand(100) client.update("Test tweet #{my_number}") puts "Look for the tweet: Test tweet #{my_number}"
  7. Let’s Write Some Code! With our client configured. We can

    now communicate with Twitter’s API. Here’s some examples of what we can do: Send a tweet from your account: client.update("I'm tweeting from the @philyrb and @phillyjsdev meetup!") Follow a user:(you can use their handle or UserId) client.follow("gem") client.follow(213747670) Or...find the 3 most recent marriage proposals to Justin Beiber: client.search("to:justinbieber marry me", result_type: "recent").take(3).each do |tweet| puts tweet.text end
  8. What we’re building Build a bot that finds tweets within

    a given radius containing “#ruby” or “#javascript” and favorites the tweet. First, let’s find 50 tweets containing “#ruby” or “#javascript” and print out the results to our console client.search("#ruby").take(50).each do |tweet| puts "#{tweet.user.screen_name}: #{tweet.text}" puts "----------------------------------------" end client.search("#javascript").take(50).each do |tweet| puts "#{tweet.user.screen_name}: #{tweet.text}" puts "----------------------------------------" end
  9. What we’re building Now, let’s refine our search a little

    bit. We’re already getting tweets to display in the console. Let’s narrow our search to people within 25 miles We need to pass Twitter the Latitude and Longitude as part of our options. For now, just go to Google Maps right- click for lat and long coords. (extra credit: look into geocoder gem) The basic format for the #search method is client.search("query", { options }) Let’s add some options to our search. Some of the available options are: • geocode (search by latitude and longitude) • language • result_type (most recent, popular, etc.) client.search("#ruby").take(50).each do |tweet| puts "#{tweet.user.screen_name}: #{tweet.text}" puts "----------------------------------------" end Add geocode to our search: client.search("#ruby", {result_type: "recent", geocode: "39.9525839, -75.1652215,10mi"}).take(50).each do |tweet| puts "#{tweet.user.screen_name}: #{tweet.text}" end
  10. Let’s Write Some Code! Let’s make it easy to add

    and remove options to our search. search_options = { result_type: "recent", geocode: "39.9525839,-75.1652215,10mi" } client.search("#ruby", search_options).take(50).each do |tweet| puts "#{tweet.user.screen_name}: #{tweet.text}" end
  11. Let’s Write Some Code! Adding interactions client.search("super specific test tweet

    to find").each do |tweet| client.favorite(tweet) client.update("@#{tweet.user.screen_name} Found It...", in_reply_to_status_id: tweet.id) end
  12. For More Info For a blog post on the topic:

    Twitter Bot Post Twitter Handle @cnorm35