Search code examples
rubyloopstwitterreadfiletweets

ruby tweet line from text file


My code won't tweet from the file every interval, it only prints in the terminal, but not on twitter. I'm also trying to loop it so that once done with the file.length its repeats. Thanks for your help.

require 'Twitter'

client = Twitter::REST::Client.new do |config|
  config.consumer_key = "..."
  config.consumer_secret = "..."
  config.access_token = "..."
  config.access_token_secret = "..."
end


blog_post = []
tweet_img = []


def post 
File.open("tweets.txt") do rand |line|
    line.each do |item|
        tweets = item.chomp.split("\n")
        #while client.update("#{}")
                puts tweets 
                sleep(30)
            #end 
            #puts blog_post.to_s, "\n\n"
        end
    end
end

puts client.update("#{post}").text

Solution

  • require 'Twitter'
    
    OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
    
    client = Twitter::REST::Client.new do |config|
      config.consumer_key = "xxxx"
      config.consumer_secret = "xxxx"
      config.access_token = "xxxx"
      config.access_token_secret = "xxxx"
    end
    
    file = File.open("tweets.txt")
    ary = []
    i = 0
    file.each_line do |line|
      ary[i] = line.chomp
      i += 1
    end
    file.close
    
    j = 0
    
    i.times do
      client.update("#{ary[j]}")
      j += 1
      sleep 10
    end