Class: Clacky::Channel::Adapters::Feishu::MessageParser
- Inherits:
-
Object
- Object
- Clacky::Channel::Adapters::Feishu::MessageParser
- Defined in:
- lib/clacky/server/channel/adapters/feishu/message_parser.rb
Overview
Parses incoming Feishu webhook events into a standardized InboundMessage format.
Class Method Summary collapse
-
.parse(body) ⇒ Hash?
Parse a Feishu webhook event body.
Instance Method Summary collapse
-
#extract_doc_urls(text) ⇒ Array<String>
Extract Feishu document URLs from text.
-
#initialize(data) ⇒ MessageParser
constructor
A new instance of MessageParser.
-
#parse ⇒ Hash?
Inbound message or nil.
-
#parse_message_event ⇒ Hash?
Parse message.receive event.
-
#strip_mentions(text) ⇒ String
Strip bot @mentions from message text.
Constructor Details
#initialize(data) ⇒ MessageParser
Returns a new instance of MessageParser.
21 22 23 |
# File 'lib/clacky/server/channel/adapters/feishu/message_parser.rb', line 21 def initialize(data) @data = data end |
Class Method Details
.parse(body) ⇒ Hash?
Parse a Feishu webhook event body
14 15 16 17 18 19 |
# File 'lib/clacky/server/channel/adapters/feishu/message_parser.rb', line 14 def self.parse(body) data = body.is_a?(Hash) ? body : JSON.parse(body) new(data).parse rescue JSON::ParserError nil end |
Instance Method Details
#extract_doc_urls(text) ⇒ Array<String>
Extract Feishu document URLs from text. Matches: /docx/TOKEN, /docs/TOKEN, /wiki/TOKEN
111 112 113 114 115 |
# File 'lib/clacky/server/channel/adapters/feishu/message_parser.rb', line 111 def extract_doc_urls(text) return [] if text.nil? || text.empty? text.scan(%r{https?://[a-zA-Z0-9._-]+\.(?:feishu\.cn|larksuite\.com)/(?:docx|docs|wiki)/[A-Za-z0-9_-]+(?:\?[^\s]*)?}) end |
#parse ⇒ Hash?
Returns Inbound message or nil.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/clacky/server/channel/adapters/feishu/message_parser.rb', line 26 def parse # Handle verification challenge if @data["type"] == "url_verification" return { type: :challenge, challenge: @data["challenge"] } end header = @data["header"] return nil unless header event_type = header["event_type"] case event_type when "im.message.receive_v1" else nil end end |
#parse_message_event ⇒ Hash?
Parse message.receive event
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/clacky/server/channel/adapters/feishu/message_parser.rb', line 48 def event = @data["event"] return nil unless event = event["message"] sender = event["sender"] return nil unless && sender msg_type = ["message_type"] Clacky::Logger.info("[feishu] msg_type=#{msg_type} content=#{["content"].to_s[0..300]}") return nil unless %w[text image file].include?(msg_type) content_raw = ["content"] return nil unless content_raw content = JSON.parse(content_raw) text = "" image_keys = [] = [] case msg_type when "text" text = strip_mentions(content["text"].to_s.strip) return nil if text.empty? when "image" image_keys = [content["image_key"]].compact return nil if image_keys.empty? when "file" file_key = content["file_key"] file_name = content["file_name"] return nil unless file_key = [{ key: file_key, name: file_name.to_s }] end chat_id = ["chat_id"] = ["message_id"] user_id = sender.dig("sender_id", "open_id") chat_type = ["chat_type"] == "p2p" ? :direct : :group create_time = ["create_time"]&.to_i = create_time ? Time.at(create_time / 1000.0) : Time.now { type: :message, platform: :feishu, chat_id: chat_id, user_id: user_id, text: text, image_keys: image_keys, file_attachments: , doc_urls: extract_doc_urls(text), message_id: , timestamp: , chat_type: chat_type, raw: @data } rescue JSON::ParserError nil end |
#strip_mentions(text) ⇒ String
Strip bot @mentions from message text
120 121 122 123 |
# File 'lib/clacky/server/channel/adapters/feishu/message_parser.rb', line 120 def strip_mentions(text) # Feishu mentions are formatted as <at user_id="...">Name</at> text.gsub(/<at[^>]*>.*?<\/at>/, "").strip end |