1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

railsで複数tweetを連投(スレッド投稿)する

Last updated at Posted at 2018-03-15

#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

みたいな感じ。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?