Class: FlowChat::Intercom::Client
- Inherits:
-
Object
- Object
- FlowChat::Intercom::Client
- Includes:
- FlowChat::Instrumentation
- Defined in:
- lib/flow_chat/intercom/client.rb
Instance Attribute Summary collapse
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#intercom ⇒ Object
readonly
Returns the value of attribute intercom.
Class Method Summary collapse
-
.parse_html(html) ⇒ Object
Convert HTML from Intercom messages to Markdown.
Instance Method Summary collapse
-
#build_reply_payload(response, conversation_id) ⇒ Object
Build reply payload for Intercom API This method is exposed so the gateway can use it for simulator mode.
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #parse_message(html) ⇒ Object
-
#send_message(conversation_id, prompt, choices: nil, media: nil) ⇒ Hash
Send a reply to a conversation.
Methods included from FlowChat::Instrumentation
#instrument, instrument, report_api_error
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
35 36 37 38 39 40 |
# File 'lib/flow_chat/intercom/client.rb', line 35 def initialize(config) @config = config @intercom = ::Intercom::Client.new(token: @config.access_token) FlowChat.logger.info { "Intercom::Client: Initialized Intercom client" } FlowChat.logger.debug { "Intercom::Client: API base URL: #{@config.api_base_url}" } end |
Instance Attribute Details
#app_id ⇒ Object
Returns the value of attribute app_id.
24 25 26 |
# File 'lib/flow_chat/intercom/client.rb', line 24 def app_id @app_id end |
#intercom ⇒ Object (readonly)
Returns the value of attribute intercom.
23 24 25 |
# File 'lib/flow_chat/intercom/client.rb', line 23 def intercom @intercom end |
Class Method Details
.parse_html(html) ⇒ Object
Convert HTML from Intercom messages to Markdown
27 28 29 |
# File 'lib/flow_chat/intercom/client.rb', line 27 def self.parse_html(html) ReverseMarkdown.convert(html.to_s).strip.presence || "" end |
Instance Method Details
#build_reply_payload(response, conversation_id) ⇒ Object
Build reply payload for Intercom API This method is exposed so the gateway can use it for simulator mode
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/flow_chat/intercom/client.rb', line 112 def build_reply_payload(response, conversation_id) type, content, _ = response case type when :text { message_type: "comment", type: "admin", admin_id: @config.admin_id.to_s, body: content.to_s } when :note { message_type: "note", type: "admin", admin_id: @config.admin_id.to_s, body: content.to_s } else # Default to comment { message_type: "comment", type: "admin", admin_id: @config.admin_id.to_s, body: content.to_s } end end |
#parse_message(html) ⇒ Object
31 32 33 |
# File 'lib/flow_chat/intercom/client.rb', line 31 def (html) self.class.parse_html(html) end |
#send_message(conversation_id, prompt, choices: nil, media: nil) ⇒ Hash
Send a reply to a conversation
46 47 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 106 107 108 |
# File 'lib/flow_chat/intercom/client.rb', line 46 def (conversation_id, prompt, choices: nil, media: nil) FlowChat.logger.info { "Intercom::Client: Sending message to conversation #{conversation_id}" } FlowChat.logger.debug { "Intercom::Client: Message content: '#{prompt.to_s.truncate(100)}'" } # Use renderer to convert to structured response response = FlowChat::Intercom::Renderer.new(prompt, choices: choices, media: media).render type, content, _ = response result = instrument(Events::MESSAGE_SENT, { to: conversation_id, message_type: type.to_s, content_length: content.to_s.length, platform: :intercom }) do # Determine message type based on response type = case type when :note "note" else "comment" end # Send using official gem reply = intercom.conversations.reply( id: conversation_id, type: "admin", admin_id: @config.admin_id.to_s, message_type: , body: content.to_s ) reply.to_hash end if result = result["id"] FlowChat.logger.debug { "Intercom::Client: Message sent successfully to conversation #{conversation_id}, message_id: #{}" } else FlowChat.logger.error { "Intercom::Client: Failed to send message to conversation #{conversation_id}" } end result rescue ::Intercom::ResourceNotFound => e FlowChat.logger.error { "Intercom::Client: Conversation not found: #{e.}" } report_api_error("Intercom conversation not found", error: e, conversation_id: conversation_id) nil rescue ::Intercom::AuthenticationError => e FlowChat.logger.error { "Intercom::Client: Authentication failed - check access token" } report_api_error("Intercom authentication failed", error: e, conversation_id: conversation_id) raise ConfigurationError, "Invalid Intercom access token" rescue ::Intercom::RateLimitExceeded retry_after = 60 FlowChat.logger.warn { "Intercom::Client: Rate limit exceeded - retry after #{retry_after}s" } raise RateLimitError.new("Intercom API rate limit exceeded", retry_after) rescue ::Intercom::ServerError => e FlowChat.logger.error { "Intercom::Client: Server error: #{e.}" } report_api_error("Intercom server error", error: e, conversation_id: conversation_id) nil rescue => e FlowChat.logger.error { "Intercom::Client: API request exception: #{e.class.name}: #{e.}" } report_api_error("Intercom API request exception: #{e.class.name}", error: e, conversation_id: conversation_id) nil end |