Class: Lita::Adapters::Chatwork::Connector
- Inherits:
-
Object
- Object
- Lita::Adapters::Chatwork::Connector
- Defined in:
- lib/lita/adapters/chatwork/connector.rb
Instance Attribute Summary collapse
-
#robot ⇒ Object
readonly
Returns the value of attribute robot.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(robot, api_key: nil, interval: nil, with_reply: nil, debug: nil) ⇒ Connector
constructor
A new instance of Connector.
- #message(target, strings) ⇒ Object
- #part(room_id) ⇒ Object
- #set_topic(target, topic) ⇒ Object
Constructor Details
#initialize(robot, api_key: nil, interval: nil, with_reply: nil, debug: nil) ⇒ Connector
Returns a new instance of Connector.
10 11 12 13 14 15 16 |
# File 'lib/lita/adapters/chatwork/connector.rb', line 10 def initialize(robot, api_key: nil, interval: nil, with_reply: nil, debug: nil) @robot = robot @api_key = api_key @interval = interval @with_reply = with_reply @debug = debug end |
Instance Attribute Details
#robot ⇒ Object (readonly)
Returns the value of attribute robot.
8 9 10 |
# File 'lib/lita/adapters/chatwork/connector.rb', line 8 def robot @robot end |
Instance Method Details
#connect ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lita/adapters/chatwork/connector.rb', line 18 def connect raise "api_key is required." unless @api_key ChatWork.api_key = @api_key define_robot loop do wait result = ChatWork::Room.get if result.is_a?(ChatWork::APIError) @logger.error "ChatWork::Room.get result: #{result} (#{result.})" break end joined_rooms = result.select{|r| r["sticky"] } unread_rooms = result.select{|r| r["unread_num"] > 0 } (joined_rooms | unread_rooms).each do |r| wait result = ChatWork::Message.get(room_id: r["room_id"]) next if result.is_a?(ChatWork::APIError) and result. == "204" result.each do |m| next if m["account"]["account_id"] == @me["account_id"] user = Lita::User.find_by_id(m["account"]["account_id"]) unless user user = Lita::User.create(m["account"]["account_id"], m["account"].merge("mention_name" => "[To:#{m["account"]["account_id"]}] #{m["account"]["name"]}さん")) end source_id = [r["room_id"], m["message_id"], m["send_time"], m["update-time"]].join("-") case r["type"] when "my" next when "direct" source = Lita::Source.new(user: user, room: source_id, private_message: true) else # "group" source = Lita::Source.new(user: user, room: source_id) end = Lita::Message.new(robot, m["body"], source) .command! if source. .command! if .body =~ /#{@robot.mention_name}/ .command! if .body =~ /\[(?:rp|返信) aid=#{@me["account_id"]} to=\d+-\d+\]/ robot.receive() end end end end |
#message(target, strings) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/lita/adapters/chatwork/connector.rb', line 65 def (target, strings) text = strings.join("\n") room_id, , send_time, update_time = target.room.split("-") if target.user reply_text = "[rp aid=#{target.user.id} to=#{room_id}-#{}] #{target.user.name}さん\n#{text}" ChatWork::Message.create(room_id: room_id, body: reply_text) else ChatWork::Message.create(room_id: room_id, body: text) end end |
#part(room_id) ⇒ Object
80 81 82 |
# File 'lib/lita/adapters/chatwork/connector.rb', line 80 def part(room_id) #TODO: support DELETE /rooms/#{room_id}?action_type=leave end |
#set_topic(target, topic) ⇒ Object
76 77 78 |
# File 'lib/lita/adapters/chatwork/connector.rb', line 76 def set_topic(target, topic) #TODO: support PUT /rooms/#{target.room}?name=#{topic} end |