Created
October 6, 2010 10:08
-
-
Save aquarla/613120 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'rubygems' | |
require 'net/https' | |
require 'oauth' | |
require 'json' if RUBY_VERSION < '1.9.0' | |
class DajareBot | |
CONSUMER_KEY = "xxxxxxxx" | |
CONSUMER_SECRET = "xxxxxxxx" | |
ACCESS_TOKEN = "xxxxxxxx" | |
ACCESS_TOKEN_SECRET = "xxxxxxxx" | |
MY_SCREEN_NAME = "xxxxxxxx" | |
def initialize | |
@consumer = OAuth::Consumer.new( | |
CONSUMER_KEY, | |
CONSUMER_SECRET, | |
:site => 'http://twitter.com' | |
) | |
@access_token = OAuth::AccessToken.new( | |
@consumer, | |
ACCESS_TOKEN, | |
ACCESS_TOKEN_SECRET | |
) | |
open("./random.txt") do |file| | |
@dajares = file.readlines | |
end | |
end | |
def connect | |
uri = URI.parse('https://userstream.twitter.com/2/user.json') | |
https = Net::HTTP.new(uri.host, uri.port) | |
https.use_ssl = true | |
https.ca_file = './verisign.cer' | |
https.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
https.verify_depth = 5 | |
https.start do |https| | |
request = Net::HTTP::Get.new(uri.request_uri) | |
request.oauth!(https, @consumer, @access_token) | |
buf = "" | |
https.request(request) do |response| | |
response.read_body do |chunk| | |
buf << chunk | |
while (line = buf[/.+?(\r\n)+/m]) != nil | |
begin | |
buf.sub!(line,"") | |
line.strip! | |
status = JSON.parse(line) | |
rescue | |
break | |
end | |
yield status | |
end | |
end | |
end | |
end | |
end | |
def run | |
connect do |json| | |
if json['text'] | |
user = json['user'] | |
if (json['text'].match(/^@#{MY_SCREEN_NAME}\s*$/)) | |
@access_token.post('/statuses/update.json', 'status' => "@#{user['screen_name']} #{random_dajare} [ダジャレbotによる自動リプライです]", 'in_reply_to_status_id' => json['id']) | |
end | |
# puts "#{user['screen_name']}: #{json['text']}" | |
end | |
end | |
end | |
def random_dajare | |
@dajares[rand(@dajares.size)] | |
end | |
end | |
DajareBot.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment