class Ruboty::Adapters::Chatwork
Public Instance Methods
chatwork_api_rate()
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 60 def chatwork_api_rate ENV["CHATWORK_API_RATE"].to_i end
chatwork_api_token()
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 37 def chatwork_api_token ENV["CHATWORK_API_TOKEN"] end
chatwork_messages_url()
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 47 def chatwork_messages_url URI.join(chatwork_url, "/v2/rooms/#{chatwork_room}/messages") end
chatwork_messages_url_for_saying(message)
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 56 def chatwork_messages_url_for_saying(message) URI.join(chatwork_url, "/v2/rooms/#{chatwork_room_for_saying(message)}/messages") end
chatwork_room()
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 42 def chatwork_room ENV["CHATWORK_ROOM"] end
chatwork_room_for_saying(message)
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 52 def chatwork_room_for_saying(message) message[:original][:room_id] || message[:room_id] || ENV["CHATWORK_ROOM_FOR_SAYING"] || ENV["CHATWORK_ROOM"] end
chatwork_url()
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 32 def chatwork_url URI.parse(ENV["CHATWORK_URL"] || "https://api.chatwork.com/") end
extract_room_id_statement(original_body)
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 102 def extract_room_id_statement(original_body) original_body[/(.*)( room_id:([0-9]+))\z/, 3] end
headers()
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 65 def headers { 'X-ChatWorkToken' => chatwork_api_token, } end
listen()
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 72 def listen loop do req = Net::HTTP::Get.new(chatwork_messages_url.path, headers) https = Net::HTTP.new(chatwork_messages_url.host, chatwork_messages_url.port) https.use_ssl = true res = https.start {|https| https.request(req) } pp res.body unless res.body.nil? message = JSON.parse(res.body) message.each do | m | original_body = m['body'] body = remove_room_id_statement(original_body) from_name = m['account']['name'] room_id = extract_room_id_statement(original_body) robot.receive( body: body, from_name: from_name, room_id: room_id, ) end end sleep (60 * 60) / chatwork_api_rate end end
remove_room_id_statement(original_body)
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 106 def remove_room_id_statement(original_body) if !(original_body[/(.*)( room_id:([0-9]+)\z)/, 1].nil?) return original_body[/(.*)( room_id:([0-9]+)\z)/, 1].rstrip else original_body end end
run()
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 15 def run listen end
say(message)
click to toggle source
# File lib/ruboty/adapters/chatwork.rb, line 19 def say(message) pp message url_for_saying = chatwork_messages_url_for_saying(message) req = Net::HTTP::Post.new(url_for_saying.path, headers) req.form_data = { 'body' => message[:body] } https = Net::HTTP.new(url_for_saying.host, url_for_saying.port) https.use_ssl = true https.start {|https| https.request(req) } end