LoginSignup
20
20

More than 5 years have passed since last update.

twitter bot 作成

Last updated at Posted at 2014-05-01

下記を見ると10分で作れるそうな!なんて簡単な(まとまった情報ありがたし)。
http://morizyun.github.io/blog/twitter-bot-ruby-rails/

ただし、twitterのgemがバージョンアップしていて古いのは使えない。今の最新は5.5系の様子。下記のようにしたら対応できた。コードの部分にさえ引っかからなければ10分でもいけそう。20分以内なら確実と思う。

require 'twitter'

namespace :twitter do
  desc "ツイートする"
  task :tweet => :environment do
    ## アプリ登録時に取得したCONSUMER_KEY/CONSUMER_SECRET
    CONSUMER_KEY = 'xxxxx'
    CONSUMER_SECRET = 'xxxxx'
    ## irbで取得したAccess Token/Access Secret
    OAUTH_TOKEN = 'xxxxx'
    OAUTH_TOKEN_SECRET = 'xxxxx'

    config = {
        :consumer_key    => CONSUMER_KEY,
        :consumer_secret => CONSUMER_SECRET,
        :oauth_token => OAUTH_TOKEN,
        :oauth_token_secret => OAUTH_TOKEN_SECRET
    }

    client = Twitter::REST::Client.new(config)
    tweet = "from service. this is test. "
    update(client, tweet)
  end

  def update(client, tweet)
    begin
      tweet = (tweet.length > 140) ? tweet[0..139].to_s : tweet
      client.update(tweet.chomp)
    rescue => e
      Rails.logger.error "<<twitter.rake::tweet.update ERROR : #{e.message}>>"
    end
  end
end
20
20
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
20