Rubyã§Twitter Botãä½ã£ã¦Herokuã§åãã
Web APIã®åå¼·ãå ¼ãã¦ãSinatraã使ã£ã¦Herokuã§åããTwitter Botãä½ãã¾ããã
åºæ¬çã«ãã¡ãã®è¨äºã®ä¸¸åãã§ãããRuby 2.3.0ã¨Sinatra 1.4.7ã«ãããã¦å¾®ä¿®æ£ãã¦ã¾ãã
ç®æ¬¡
- ç®æ¬¡
- Twitter APIã使ãããã®æºå
- Botã¢ããªä½æã®äºåä½æ¥
- Twitter Botã®ã³ã¼ããæ¸ã
- Herokuã«ãããã¤ãã
- Heroku Schedulerã§Botãèªåã§åãã
- åèè¨äº
- éçºç°å¢
Twitter APIã使ãããã®æºå
Botãã¤ã¶ããããã®Twitterã¢ã«ã¦ã³ã*1ãç¨æããBotãTwitterã«æ¥ç¶ããããã«å¿ è¦ã¨ãªãã³ã³ã·ã¥ãã¼ãã¼*2ãã¢ã¯ã»ã¹ãã¼ã¯ã³*3ãæºåãã¾ãã
Twitterã«Botãç»é²
Twitterã®Developerãµã¤ãã«ãµã¤ã³ã¤ã³ãã¦å¿ è¦äºé ãç»é²ããTwitter APIãå©ç¨ããã¢ããª*4ãä½æãã¾ãã
é ç® | ç»é²å 容 | |
---|---|---|
Name | b0npubot | é©å½ã«ã¢ããªã«ååãä»ãã |
Description | ãã£ããããããä½ãã¾ãã | é©å½ã«ã¢ããªã®èª¬æãæ¸ã |
Website | https://twitter.com/b0npu | ã¨ããããTwitterã¢ã«ã¦ã³ãã®URL*5ãè¨è¿° |
Callback URL | å¿ é é ç®ã§ã¯ãªãã£ãã®ã§ç©ºç½ã«ãã¾ãã |
Botã®æ¥ç¶ã«å¿ è¦ãªã¢ã¯ã»ã¹ãã¼ã¯ã³ãåå¾
Twitterã«ç»é²ããã¢ããªã±ã¼ã·ã§ã³ã®Keys and Access Tokens
ã®é
ç®ã«Consumer Key
ã¨Consumer Secret
ã¯è¨è¼ããã¦ããã®ã§ãCreate my access token
ããAccess Token
ã¨Access Token Secret
ãçæãã¾ãã
Botã®ã¢ã¯ã»ã¹ã¬ãã«ã®ç¢ºèª
Botã¯Twitter ã«ã¤ã¶ããã¾ãã®ã§ã
Access level
ã®é
ç®ãRead and write
ã«ãªã£ã¦ãããã¨ã確èª*6ãã¦ããã¾ãã
Botã¢ããªä½æã®äºåä½æ¥
Rubyã§ä½ã£ãWebã¢ããªãHeorkuã§åããããã«ã¯ãGitãªãã¸ããªã¨Gemfileãå¿
è¦ã«ãªãã®ã§ãæºåãã¾ãã
ãªããGitã®ä½¿ãæ¹ã¨Herokuã®ä½¿ãæ¹ã¯Railsãã¥ã¼ããªã¢ã«ã®ç¬¬2çãåèã«ãã¦ãã¾ãã
ã¾ããRubyã®éçºç°å¢ã¯ãã¡ãã®è¨äºãåèã«æ§ç¯ãã¦ãã¾ãã
Gitãªãã¸ããªã®ã»ããã¢ãã
é©å½ãªãã£ã¬ã¯ããªãä½æãã¦ããã£ã¬ã¯ããªã®ä¸ã«ç§»åãã¾ãã
~ $mkdir twitterbot ~ $cd twitterbot ~/twitterbot
git init
ã³ãã³ãã§ãGitãªãã¸ããªã¨ãã¦ã»ããã¢ãã*7ãã¾ãã
~/twitterbot $git init Initialized empty Git repository in /Users/b0npu/twitterbot/.git/
Gemfileãä½æ
Herokuã§ã¯Gemfileã使ã£ã¦gem*8ã管çãããããªã®ã§ãã¢ããªã±ã¼ã·ã§ã³ãã£ã¬ã¯ããªã®ç´ä¸ã«Gemfileãä½æããBotã¢ããªã§å©ç¨ããgemãè¨è¿°ãã¾ãã
~/twitterbot $touch Gemfile ~/twitterbot $vim Gemfile
Twitter APIã使ãããã®gemtwitter
ã¨ãWebãã¬ã¼ã ã¯ã¼ã¯ã®Sinatraã使ãããã®gemsinatra
ãè¨è¿°*9ãã¾ãã
source 'https://rubygems.org' # gemãæ¢ãã«è¡ãå ´æ ruby '2.3.0' # rubyã®ãã¼ã¸ã§ã³ãæå® gem 'sinatra', '1.4.7' gem 'twitter', '5.16.0'
bundle install
ã³ãã³ãã§ãGemfileã«è¨è¿°ããgemãã¤ã³ã¹ãã¼ã«ãã¾ãã
~/twitterbot
$bundle install
ã¨ãããããã®è¾ºã§git commit
ãã¦ãGitãªãã¸ããªã«ä½æ¥å
容ãä¿åãã¾ãã
~/twitterbot $git add -A ~/twitterbot $git commit -m "First commmit"
Twitter Botã®ã³ã¼ããæ¸ã
Webã¢ããªã®ã¡ã¤ã³ãã¡ã¤ã«ã«ãªãapp.rb
ã¨Botã®æ¬ä½ãè¨è¿°ããtweet.rb
ã«å ããHerokuã§ä½¿ãè¨å®ãã¡ã¤ã«ã®config.ru
ãä½æããã³ã¼ããè¨è¿°ãã¾ãã
~/twitterbot
$touch app.rb tweet.rb config.ru
ãã£ã¬ã¯ããªã®æ§æ
~/twitterbot
|- app.rb // Webã¢ããªã®ã¡ã¤ã³ãã¡ã¤ã«
|- tweet.rb // Twitter Botã®æ¬ä½
|- config.ru // Herokuã§ä½¿ãWebã¢ããªã®è¨å®ãã¡ã¤ã«
|- Gemfile // 使ç¨ããgemã管çãããã¡ã¤ã«
|- Gemfile.lock // ã¤ã³ã¹ãã¼ã«ããgemã®ãã¼ã¸ã§ã³ã管çãããã¡ã¤ã«
|- .git // Gitãªãã¸ããªã®ç®¡çãã©ã«ã
tweet.rb
@text
ã«æ ¼ç´ããæååããTwitter APIã®ã³ã³ã·ã¥ãã¼ãã¼ãã¢ã¯ã»ã¹ãã¼ã¯ã³ã使ç¨ãã¦Twitterã«ã¤ã¶ããã¾ãã
require 'twitter' class Tweet def initialize @text = ["ã¡ãããã", "ãããªããã", "ã©ãªãããã", "ãããã", "ããã!!", "ãããã§!!", "ãã£ããã!!"] @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 private def update(tweet) begin @client.update(tweet) rescue => e nil #TODO end end end # random_tweetãå®è¡ãã if __FILE__ == $0 Tweet.new.random_tweet end
tweet.rb
ã ãã§ãTwitter APIã使ã£ã¦ã¤ã¶ããããããªã®ã§ã確èªãã¦ã¿ã¾ãã
~/twitterbot
$ruby tweet.rb
ã³ã¼ããã¢ã¯ã»ã¹ãã¼ã¯ã³ã«ééããç¡ããã°ãTwitterã§æ°çã¡ãã»ã¼ã¸ã確èªã§ãã¾ãã
app.rb
Twitter BotãHerokuã§åããããã«ãSinatraã§Webã¢ããªã«ãã¾ãã
require 'sinatra' require_relative 'tweet.rb' # URL'/'ã§ã¢ã¯ã»ã¹ get '/' do 'under construction' end # URL'/random_tweet'ã§ã¢ã¯ã»ã¹ get '/random_tweet' do Tweet.new.random_tweet end
app.rb
ãåããã¦ãWebãµã¼ããèµ·åããã¾ãã
~/twitterbot $ruby app.rb [2016-06-11 16:04:28] INFO WEBrick 1.3.1 [2016-06-11 16:04:28] INFO ruby 2.3.0 (2015-12-25) [x86_64-darwin15] == Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from WEBrick [2016-06-11 16:04:28] INFO WEBrick::HTTPServer#start: pid=28495 port=4567
Webãµã¼ããèµ·åãããããã©ã¦ã¶ããhttp://localhost:4567/random_tweet
ãå
¥åãããã
curl
ã³ãã³ãã§http://localhost:4567/random_tweet
ã«æ¥ç¶ãã¦ã¿ã¾ãã
$curl http://localhost:4567/random_tweet Please check your tweet%
app.rb
ã®ã³ã¼ãã«ééããç¡ããã°ãtweet.rb
ã®åä½ç¢ºèªæã¨åæ§ã«æ°çã¡ãã»ã¼ã¸ã確èªã§ãã¾ãã
config.ru
Herokuã§Sinatraã¢ããªã¨ãã¦åãããããconfig.ru
ã«Sinatraã¢ããªã§ããæ¨ãè¨è¿°ãã¾ãã
require_relative 'app.rb' run Sinatra::Application
Herokuã«ãããã¤ãã
Twitter BotãHerokuã§åããããã«ãGitã使ç¨ãã¦Herokuã«ãããã¤ãã¾ãã
Gitãªãã¸ããªã«ã³ããã
Herokuã«ãããã¤ããåã«ä½æ¥å
容ãä¿åãã¦ããå¿
è¦ãããã¾ãã®ã§ãgit commit
ãã¾ãã
~/twitterbot $git add -A ~/twitterbot $git commit -m "Add tweet bot"
Herokuã«ã¢ããªã®ãªãã¸ããªãä½æ
heroku create
ã§ãHerokuã«Twitter Botã¢ããªã®ãªãã¸ããªãä½æãã¾ãã
~/twitterbot
$heroku create b0npubot
Herokuã«ãªãã¸ããªãä½æããããBotã¢ããªã®Gitãªãã¸ããªãHerokuã«push
ãã¾ãã
~/twitterbot
$git push heroku master
Herokuã§Twitter Botã®åä½ç¢ºèª
heroku open
ã§ãHerokuã«ä½æãããWebãµã¤ããéãã¾ãã
~/twitterbot
$heroku open
app.rb
ã«è¨è¿°ããããã«under construction
ã®æåã表示ããã¾ãã
Herokuã«ä½æãããWebãµã¤ãã®URLã«random_tweet
ãå ãã¦ãæ¥ç¶ãã¾ãã
Twiiterã«æ°çã¡ãã»ã¼ã¸ã表示ããã¦ããã°ãä¸ã æ³ã§ãã
åä½ç¢ºèªã®æ©è½ãåé¤
Herokuã«ãããã¤ããTwitter Botã®åä½ç¢ºèªãã§ãããã念ã®ãããapp.rb
ãç·¨éãã¦åä½ç¢ºèªæ©è½ãåé¤ãã¦ããã¾ãã
require 'sinatra' require_relative 'tweet.rb' get '/' do 'under construction' end get '/random_tweet' do # Tweet.new.random_tweet # 'Please check your tweet' 'Closed' end
app.rb
ã®å¤æ´ãgit commit
ã§Gitãªãã¸ããªã«ä¿åããHerokuã«push
ãã¦åæ ããã¾ãã
~/twitterbot $git commit -am "Remove manual tweet" ~/twitterbot $git push heroku master
random_tweet
ã®URLã«æ¥ç¶ãã¦ãå¤æ´ãåæ ããã¦ããäºã確èªãã¾ãã
Heroku Schedulerã§Botãèªåã§åãã
Herokuã§Twitter Botãåä½ããäºã確èªããããBotãããããããã«ãScheduler
ã¢ããªã³*10ã使ã£ã¦èªåã§åãããã«ãã¾ãã
~/twitterbot
$heroku addons:create scheduler:standard
Scheduler
ã¢ããªã³ãTwitter Botã¢ããªã«å ããããScheduler
ã®è¨å®ç»é¢ãéãã¾ãã
~/twitterbot
$heroku addons:open scheduler
ã³ãã³ãã®æ¬ã«bundle exec ruby tweet.rb
ãå
¥åããå®è¡ããé »åº¦ãè¨å®ãã¦ä¿åãã¾ãã
Scheduler
ã®è¨å®ããå¾ã¯ãç¡èª²éã§ä½¿ãããªã½ã¼ã¹ã使ãæããããªãäºãç¥ãã¾ã(´ã»Ïã»ï½)
åèè¨äº
Twitter Botã«é¢ãã¦ã¯ããã¡ãã®è¨äºãåèã«ããã¦ããã ãã¾ããã
- Ruby素人でもできた!Herokuで動かすTwitter Bot! - Qiita
- Herokuを使って1日1回名言をツイートするTwitter Botの作り方 - アインシュタインの電話番号
- Rubyで書いたTwitterのbotをRaspberry Piで動かす - DesignAssembler
Rubyã®Webãã¬ã¼ã ã¯ã¼ã¯ã«é¢ãã¦ã¯ããã¡ãã®è¨äºãåèã«ããã¦ããã ãã¾ããã
éçºç°å¢
- OSX 10.11.5 El Capitan
- ããã¹ãã¨ãã£ã¿ï¼ MacVim
- ã¿ã¼ããã«ã¨ãã¥ã¬ã¼ã¿ï¼ Macã¿ã¼ããã«
- ã·ã§ã«ï¼ zsh
- ããã±ã¼ã¸ããã¼ã¸ã£ï¼ Homebrew - Ruby 2.3.0
- ãã¼ã¸ã§ã³ããã¼ã¸ã£ï¼ rbenv
- Webãã¬ã¼ã ã¯ã¼ã¯ï¼ Sinatra 1.4.7
*1:Twitter APIã使ãå ´åã¯é»è©±çªå·ã®ç»é²ãå¿ è¦ãªããã§ã
*2:IDã®ãããªãã®
*3:ãã¹ã¯ã¼ãã®ãããªãã®
*5:é£æºã¢ããªã®å ´åã¯ãµã¤ãã®URLã¨ã
*6:å¤æ´ãå¿ è¦ãªå ´åã¯Permissionsããå¤æ´ã§ããããã§ã
*7:.gitãã£ã¬ã¯ããªãä½æããã¾ã
*9:gemã®ãã¼ã¸ã§ã³ã¯ææ°çãé¸ã³ã¾ãã
*10:ã¢ããªã³ã®å©ç¨ã«ã¯ã¢ã«ã¦ã³ãæ å ±ã«ã¯ã¬ã¸ããã«ã¼ãã®ç»é²ãå¿ è¦ã§ã