# -*- coding: utf-8 -*- require 'twitter' require './key.rb' @rest_client = Twitter::REST::Client.new do |config| config.consumer_key = Const::CONSUMER_KEY config.consumer_secret = Const::CONSUMER_SECRET config.access_token = Const::ACCESS_TOKEN config.access_token_secret = Const::ACCESS_TOKEN_SECRET end @stream_client = Twitter::Streaming::Client.new do |config| config.consumer_key = Const::CONSUMER_KEY config.consumer_secret = Const::CONSUMER_SECRET config.access_token = Const::ACCESS_TOKEN config.access_token_secret = Const::ACCESS_TOKEN_SECRET end @screen_name = Const::SCREEN_NAME def tweet_okazu(status) if status.text.include?("@#{@screen_name} okazu") text = status.text.sub("@#{@screen_name} okazu","") text = text.gsub(/(\s| )+/, "") #ファイル名が無かったらリターンする return unless File.exist?("/home/user/Dropbox/okazu/#{text}") #ファイルの中の画像のみで配列を作ってそこからランダムに選ぶ p = Dir.glob("/home/user/Dropbox/okazu/#{text}/*.{png,jpg,jpeg,gif}").sample #pic:選ばれた画像を開く, opt:in_reply_to, tweet:相手のscreen_name pic = open(p, "r") opt = {"in_reply_to_status_id"=>status.id.to_s} tweet = "@#{status.user.screen_name}" #画像を添付してツイート @rest_client.update_with_media(tweet, pic, opt) end end @stream_client.user do |object| next unless object.is_a? Twitter::Tweet unless object.text.start_with? "RT" tweet_okazu(object) end end