Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Building a simple Twitter bot with the Twitter Ruby gem

##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 .gitignore.

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 = 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']

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, lang: "en").first.text 

####Start tweeting 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:

def self.tweet status 

#follow another user
def self.follow_user name

#find a tweet by ID
def self.find_tweet id

####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).


This comment has been minimized.

Copy link

@droberts-sea droberts-sea commented Mar 15, 2019

Hi! This is a great resource, and I want to link to it in a course and give you credit! However your markdown formatting has a couple of issues. is a copy with the formatting cleaned up a little.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.