[Ruby] Twitter marketing? Should it be automated?

2 minute read

It is Abe who released the service around this time. Although I have given too many titles, I am having a hard time not knowing the marketing. For the time being, I started using Twitter on Ruby, so I will describe the knowledge. (I’m just starting out, and I’m not sure about the results.)

Twitter operation is troublesome. .. I’m a Ruby engineer and not a Twitterer (angry) Is there no easy way to operate it? That’s an article for people.

The sample is described in Ruby.


  • How did you automate it?
  • What is Twitter Rest API?
  • How to introduce Ruby’s Twitter REST API
  • What kind of operation are you actually doing?
  • Summary

How did you automate it?

I automated using Twitter Rest API.

What is Twitter Rest API?

As the name implies, it is an API provided by Twitter. By calling this API, Processing on Twitter such as tweets and retweets can be executed programmatically.

Since Ruby has a gem that can easily call this Twitter Rest API, Let’s take advantage of it. https://github.com/sferik/twitter

If you want to know the outline of each API, parameters, etc., please refer to the reference below. It is organized in an easy to understand manner. https://rdoc.info/gems/twitter/Twitter/REST

How to install Twitter REST API

Create account

First, create a Twitter developer account. I referred to this article. https://qiita.com/kngsym2018/items/2524d21455aac111cdee

It’s a bit troublesome because I have to contact you in English, Please get over it.


Let’s implement the class that creates Client. Client has each method to call Twitter API. If you create a Client and call the method, It is possible to call processing such as tweets and retweets from a program.


class TwitterWrapper
  attr_accessor :client

  def initialize
    @client = Twitter::REST::Client.new do |config|
      config.consumer_key = ENV["TWITTER_API_KEY"]
      config.consumer_secret = ENV["TWITTER_API_SECRET"]
      config.access_token = ENV["TWITTER_ACCESS_TOKEN"]
      config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]

  def tweet(content:, option: {})
    @client.update(content, option)

  def tweet_with_media(content:, images: ,option: {})
    @client.update_with_media(content, images, option)

  def retweets(tweet_id: nil)


This time, I placed the setting in config/twitter.yml, but please place it anywhere you like. There is no problem if you can enter each value correctly when you Twitter::REST::Client New. Only the method explained this time is described in twitter-wrapper.rb, There are many other methods, so I think it’s interesting to check them in Documentation.

What kind of operation are you actually using?

I am doing the following operations.

  • Tweet when content is posted
  • Retweet your tweets at regular intervals

When content is posted Tweet

When content is posted, we’re calling tweet. It just executes the update_with_media wrap method (tweet_with_media) that calls the Tweet API. I used update_with_media because I wanted to tweet the image together this time. If you don’t need images, you can use update method.


  class Tweet <Twitters::Application
    def tweet(content)
      tweet_wrapper = TwitterWrapper.new
      content = content # Tweet content
      images = [image] # Array<File>
      tweet_wrapper.tweet_wrapper.tweet_with_media(content: content, images: images)

Retweet yourself at regular intervals

The following process is called by cron. However, if you try to retweet an already retweeted tweet, you will get an error. I think it would be easier to add processing.


tweet_wrapper = TwitterWrapper.new

tweet_wrapper.search('account name').each do |tweet|
  user_name = tweet.user.screen_name

  return false if !user_name.include?('account name')

  tweet_wrapper.retweets(tweet_id: tweet_id)


The above is the method that I thought about quickly, so I think it can be further refined. If you have a way to write this way, please leave a comment!

Good luck, Twitter life!