Created
April 12, 2015 13:52
-
-
Save tily/a8392cbf0d8582243b1c to your computer and use it in GitHub Desktop.
twitter to tumblr quote
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 | |
module Ruboty | |
module Handlers | |
class T3q < Base | |
NAMESPACE = 't3q' | |
def initialize(robot) | |
super(robot) | |
end | |
on( | |
/(?<book>.+)読始/, | |
all: true, | |
name: 'start_quoting', | |
description: 'start quoting' | |
) | |
on( | |
/(?<book>.+)読了/, | |
all: true, | |
name: 'stop_quoting', | |
description: 'start quoting' | |
) | |
on( | |
/「(?<text>.+)」/, | |
all: true, | |
name: 'quote', | |
description: 'quote' | |
) | |
def start_quoting(message) | |
return if message.original[:from] != master | |
book = message[:book] | |
if book(book)['started'] | |
dm "Error: already quote started: #{book}" | |
end | |
book(book)['started'] = true | |
book(book)['status_id'] = message.original[:tweet].try(:id) | |
dm "started quoting: #{book}" | |
end | |
def stop_quoting(message) | |
return if message.original[:from] != master | |
book = message[:book] | |
if !book(book)['started'] | |
dm "Error: not yet quoting: #{book}" | |
end | |
if book(book)['stopped'] | |
dm "Error: already quote stopped: #{book}" | |
end | |
book(book)['stopped'] = true | |
book(book)['started'] = false | |
dm "stopped quoting: #{book}" | |
end | |
def quote(message) | |
if book = data.find {|k, v| v['status_id'] == message.original[:tweet].try(:in_reply_to_status_id) } | |
tumblr.quote('tily', quote: message[:text], source: book.first) | |
dm "posted to tumblr: 「#{message[:text]}」 (#{book.first})" | |
end | |
end | |
private | |
def book(title) | |
data[title] ||= {} | |
data[title] | |
end | |
def data | |
robot.brain.data[NAMESPACE] ||= {} | |
robot.brain.data[NAMESPACE] | |
end | |
def master | |
ENV['TWITTER_MASTER'] | |
end | |
def dm(text) | |
robot.send(:adapter).send(:client).create_direct_message(master, text) | |
end | |
def tumblr | |
Tumblr.configure do |config| | |
config.consumer_key = ENV['TUMBLR_CONSUMER_KEY'] | |
config.consumer_secret = ENV['TUMBLR_CONSUMER_SECRET'] | |
config.oauth_token = ENV['TUMBLR_ACCESS_TOKEN'] | |
config.oauth_token_secret = ENV['TUMBLR_ACCESS_TOKEN_SECRET'] | |
end | |
Tumblr::Client.new | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment