Created
May 1, 2014 23:25
-
-
Save ms2sato/11464297 to your computer and use it in GitHub Desktop.
twitter bot 作成 ref: http://qiita.com/ms2sato/items/4c16d0eb77391c4ebf3d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment