Skip to content

Instantly share code, notes, and snippets.

@motoso
Last active September 28, 2015 13:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save motoso/dd2a5f2797db5a6c5a83 to your computer and use it in GitHub Desktop.
Save motoso/dd2a5f2797db5a6c5a83 to your computer and use it in GitHub Desktop.
Twitter bot on Heroku
require 'rubygems'
require 'sinatra'
require_relative './tweet.rb'
get '/' do
"under construction"
end
get '/random_tweet' do
# Tweet.new.random_tweet # 動作チェックが終わったらコメントアウトすること
end
require_relative './app.rb'
run Sinatra::Application
ruby '2.1.4'
source 'https://rubygems.org'
gem 'sinatra'
gem 'twitter'
require_relative './tweet.rb'
Tweet.new.daily_tweet
require 'rubygems'
require 'twitter'
class Tweet
def initialize
@text = <<-EOF.split("\n")
# ここに31行分のテキストを入れる
EOF
@client = Twitter::REST::Client.new do |config|
config.consumer_key = 'YOUR_CONSUMER_KEY'
config.consumer_secret = 'YOUR_CONSUMER_SECRET'
config.access_token = 'YOUR_ACCESS_TOKEN'
config.access_token_secret = 'YOUR_ACCESS_SECRET'
end
end
def random_tweet
tweet = @text[rand(@text.length)]
update(tweet)
end
def daily_tweet
tweet = @text[Time.now.day - 1]
update(tweet)
end
private
def update(tweet)
return nil unless tweet
begin
@client.update(tweet.chomp)
rescue => ex
nil # todo
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment