[RUBY] Twitter marketing? What if I automate it?

It is Abe who released the service around this time. Although I gave an excessive title, I am struggling because I do not understand marketing. For the time being, I started using Twitter on Ruby, so I will describe the knowledge. (It's just the beginning and the result is unknown.)

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

The sample is written in Ruby.

agenda

――How did you automate it? ――What is Twitter Rest API? --How to install Ruby's Twitter REST API ――What kind of operation are you actually doing? --Summary

How did you automate it?

I used Twitter Rest API to automate it.

What is the Twitter Rest API?

As the name suggests, it is an API provided by Twitter. By calling this API You can programmatically perform processing on Twitter such as tweets and retweets.

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 and parameters of each API, 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

Account creation

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

It's a bit of a hassle because I have to contact you in English, Please get over it with a spirit.

Implementation

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

twitter-wrapper.rb


require 'twitter'
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"]
    end
  end

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

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

  def retweets(tweet_id: nil)
    @client.retweet(tweet_id)
  end

end

This time, I placed the settings in config / twitter.yml, but please place it wherever you like. When you New Twitter :: REST :: Client, there is no problem if you can enter each value correctly. twitter-wrapper.rb only describes the method explained this time, There are many other methods, so it would be interesting to check them in Documentation.

What kind of operation is actually done

I am doing the following operations.

--Tweet when the content is posted --Retweet your tweets at regular intervals

Tweet when the content is posted

When the content is posted, we are calling the tweet. I'm just running the update_with_media wrap method (tweet_with_media) that calls the Tweet API. This time I wanted to tweet the image together, so I'm using update_with_media. If you don't need the image, you can use the update method.

tweet.rb


  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)
    end
  end

Retweet yourself at regular intervals

The following process is called by cron. However, if you try to retweet a tweet that you have already retweeted, you will get an error. I think it's kinder to add some processing.

retweet.rb


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)
end

Summary

The above is the way I thought about it, so I think it can be refined. If you have this way of writing, I would appreciate it if you could comment!

Good Twitter life!

Recommended Posts

Twitter marketing? What if I automate it?
What i learned
What I learned ② ~ Mock ~
What I learned ① ~ DJUnit ~