#railsで複数tweetを連投(スレッド投稿)する
前提
- gem 'twitter'
- twitterで各app key,secret,access token,access token secret 取得済み
@post.tweets = ["tweet1","tweet2","teet3"]
posts_controller.rb
class PostsController < ApplicationController
...
client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_KEY']
config.consumer_secret = ENV['TWITTER_SECRET']
config.access_token = ENV['ACCESS_TOKEN']
config.access_token_secret = ENV['ACCESS_TOKEN_SECRET']
end
@tw_ids = Array.new
@post.tweets.each_with_index do |tw, i|
if i < 1
res = client.update(tw)
@res_ids.push(res.id)
else
res = client.update(tw, {:in_reply_to_status_id => @tw_ids.last})
@tw_ids.push(res.id)
end
end
...
end
みたいな感じ。