Rubyでtwitterbotを作る
手順メモメモ
1.プロジェクト準備
必要なライブラリのインストール
$ gem install heroku sinatra bundler get-twitter-oauth-token --no-rdoc --no-ri
それぞれ何かと簡単調べる
・heroku
→Rubyアプリケーションのクラウドプラットフォーム(PaaS)
gitを使用してデプロイが可能
・sinatra
→軽量のWebフレームワーク
マニュアルを読むと本当に簡単にできそうな。感じはする。
・bundler
→gemパッケージの管理をしてくれるツール
正直よくわかんない。。勉強しよう。
・get-twitter-oauth-token
→TwitterのOAuth認証に必要なトークンをゲットできるライブラリかな。。
Bot用フォルダ作成
Bot用フォルダを作成して、git initしてリポジトリに登録。
$ mkdir iwamobot
$ cd iwamobot
$ git init
~/iwamobot/.gitフォルダが作成される。
今後git関連のものはこのフォルダに格納されるとのこと。
2.Twitter認証
teitter developersページにてアプリケーション登録を行う。
入力した情報は
Application Details
Name:iwamobot
Description:bot for I
Web Site:http://twitter.com/iwamobot
アプリケーション登録してからSetteinタブを選択して以下を選択
Application TypeのAccess:Read and Write
ここで表示される「Consumer key」と「Consumer secret」をメモっておく。
OAuth認証に必要なトークンを取得する
下記コマンドを実行
$ get-twitter-oauth-token
したらopensslが見つからないエラー
in `require': no such file to load -- openssl (LoadError)
いろいろ調べるとrubyインストール時に引数でopensslのパスを渡しておくらしい。。。。。
いちどrubyを削除
$ rvm remove ruby-1.9.2-p290
opensslをインストールしてから、rubyを再インストール。
$ rvm pkg install openssl
$ rvm install 1.9.2 --with-openssl-dir=/home/hoopfuyu/.rvm/src
インストール中調べてたら再度etc/opensslへ行ってから最終的にmakeすればよかったよう。
なんだかなー。
そして再度トークンを取得。get-twitter-oauth-tokenコマンドを実行すると、
「Consumer key」と「Consumer secret」の入力が求められるので入力する。
すると認証用ページに飛ぶので必要情報を入力するとPINコードが得られる。
ターミナルに貰ったPINコードを入力するとTOKENとSecret+がもらえる。
この情報も後に必要らしいのでメモ。
3.プログラム
やっとプログラムできる。。
1で作成したiwamobotフォルダにファイルを作成。
作成後はこのような感じ
$ ls -a
. .. .git Gemfile app.rb config.ru tweet.rb
それぞれのファイルの内容は以下
config.ru
require 'app.rb'
run Sinatra::Application
Gemfile
app.rb
require 'rubygems'
require 'sinatra'
require './tweet.rb'get '/' do
'under construction'
end
tweet.rb
# coding: utf-8
require 'rubygems'
require 'twitter'class Tweet
def initialize
@text = "幸せのハードルは低い方がいいんだよ"Twitter.configure do |config|
config.consumer_key = '前の手順で入手したもの'
config.consumer_secret = '前の手順で入手したもの'
config.oauth_token = '前の手順で入手したもの'
config.oauth_token_secret = '前の手順で入手したもの'
end
enddef tweet
tweet = @text
update(tweet)
endprivate
def update(tweet)
return nil unless tweetbegin
Twitter.update(tweet.chomp)
rescue => ex
nil # todo
end
endend
4.heroku登録
herokuコマンドを使って、Heroku上にWebアプリを新規作成する。重複しない名前だとOKらしい。
まー重複するわけもなく。
$ heroku create iwamobot
Enter your Heroku credentials.
Email: '入力'
Password:'入力'
Creating iwamobot... done, stack is bamboo-mri-1.9.2
http://iwamobot.heroku.com/ | git@heroku.com:iwamobot.git
Git remote heroku added
正常終了。これでgitコマンドのremote先にHerokuが追加された。
$ bundle install
ローカルリポジトリにコミットを行う。
$ git add .
$ git commit -m 'My first commit!'
ローカルへのコミットが完了したら、それをHerokuにプッシュする
$ git push heroku master
ちょっとエラーがでた。
SSH鍵を作成して、herokuに登録する
$ ssh-keygen -t rsa -C "メールアドレス"
(省略)
$ heroku keys:add ~/.ssh/id_rsa.pub
再度Herokuへプッシュする。今度はうまくいった。
$ heroku open
するとブラウザが起動して、「under construction」と表示される予定が、Application Error発生。
「An error occurred in the application and your page could not be served. Please try again in a few moments.
If you are the application owner, check your logs for details.」
ログを確認してみる
$ heroku logs
(省略)
Unexpected error while processing request: no such file to load -- app.rb
Error H13 (Connection closed without response) -> GET iwamobot.heroku.com/ dyno=web.1 queue= wait= service= status=503 bytes=
(省略)
app.rbが見つからないらしい。
config.ruを下記に修正する。
require './app.rb'
run Sinatra::Application
ファイルを修正したので、gitでcommitを行う。
その前にstatusの確認
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add..." to update what will be committed)
# (use "git checkout --..." to discard changes in working directory)
#
# modified: config.ru
#
no changes added to commit (use "git add" and/or "git commit -a")
gitは初めて使うけど、ここで修正されたものがもう確認できるのか。
$ git commit -m 'config.ru update'
再度再度Herokuへプッシュする。
heroku openすると「under construction」と表示された。
アドレスのおしりに/tweetを追加してアクセスしてみる。
Bot用Twitterアカウントのタイムラインを確認してみると、、ツイートできてる!!
長かったーー。