##Twitter gem overview
The Twitter Ruby gem is a Ruby interface to the Twitter API. It allows you to make all available Twitter API requests (e.g., tweet, follow a user, search, etc.) within a Rails app.
This guide was adapted from multiple sources, including the Twitter Ruby gem configuration page and How to Make a Twitter Bot in Ruby on Rails. Twitter's own API docs are a good resource too, of course.
####Get set up with Twitter To begin, you'll need to register your application with Twitter and get the proper keys. Log into or sign up for a Twitter account, and scroll down to "Manage Your Apps." Click "Create a New App" and fill out the form with the app's details. Once you're on the app's management page, select the "Permissions" tab. Make sure access is set to "Read and Write." Finally, navigate to the "Keys and Access Tokens" tab. Note your "Consumer Key" and "Consumer Key Secret" tokens, and initialize the app's Access Tokens ("Access Token" and "Access Token Secret"). Keep these four keys for later.
####Configure your bot in Rails
Create a new Rails app. Add
gem 'twitter', '~> 5.16' and
figaro to your Gemfile. Run
bundle install and
figaro install. The latter command creates a new file called
application.yml that you'll use to store your secret access keys. It also adds the file to
Add your four keys to
application.yml, making sure to update the values with your actual keys:
consumer_key: "Your access code here"
consumer_secret: "Your access code here"
access_token: "Your access code here"
access_secret: "Your access code here"
To configure the bot, touch
twitter.rb file in
/config/initializers. Paste the following code:
CLIENT = Twitter::REST::Client.new do |config| config.consumer_key = ENV['consumer_key'] config.consumer_secret = ENV['consumer_secret'] config.access_token = ENV['access_token'] config.access_token_secret = ENV['access_secret'] end
Test the configuration by running
ENV['consumer_key'] in the command line. You should get back your consumer key.
If if everything's working, generate a new
Bot model in Rails (and run
rake db:migrate). Here you'll write the methods that you'll use to access Twitter. To search for the latest tweet that contains a certain string, for example, you'll want something like:
def self.search_words words CLIENT.search(words, lang: "en").first.text end
Test it out in the Rails console by running
Bot.search_words 'baby goats'. You should see the text of a recent tweet that contains the string "baby goats."
A few other methods you can try:
#tweet def self.tweet status CLIENT.update(status) end #follow another user def self.follow_user name CLIENT.follow(name) end #find a tweet by ID def self.find_tweet id CLIENT.status(id) end
####Other things to try Using this configuration as a basis, you can write methods to automate your bot's behavior and build more complex actions, like searching or tweeting at set intervals, generating tweets with regular expressions, and connecting with an external API to scrape data (like a collection of images at a museum, for instance, or a Wikipedia page's edit history).