Class: FlowChat::Intercom::Client
- Inherits:
-
Object
- Object
- FlowChat::Intercom::Client
- Includes:
- FlowChat::Instrumentation
- Defined in:
- lib/flow_chat/intercom/client.rb
Constant Summary
Constants included from FlowChat::Instrumentation
FlowChat::Instrumentation::DELIVERED_MESSAGE_ID_KEY, FlowChat::Instrumentation::DELIVERY_DURATION_KEY
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
#elapsed_ms_since, #inbound_message?, #instrument, instrument, #platform_message_id_from, report_api_error, #report_delivery_failure, #report_delivery_success, #report_to_app, #report_to_subscribers
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
32 33 34 35 36 37 |
# File 'lib/flow_chat/intercom/client.rb', line 32 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.
21 22 23 |
# File 'lib/flow_chat/intercom/client.rb', line 21 def app_id @app_id end |
#intercom ⇒ Object (readonly)
Returns the value of attribute intercom.
20 21 22 |
# File 'lib/flow_chat/intercom/client.rb', line 20 def intercom @intercom end |
Class Method Details
.parse_html(html) ⇒ Object
Convert HTML from Intercom messages to Markdown
24 25 26 |
# File 'lib/flow_chat/intercom/client.rb', line 24 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/flow_chat/intercom/client.rb', line 117 def build_reply_payload(response, conversation_id) type, content, = response payload = case type when :note { message_type: "note", type: "admin", admin_id: @config.admin_id.to_s, body: content.to_s } else # :text and anything else default to comment { message_type: "comment", type: "admin", admin_id: @config.admin_id.to_s, body: content.to_s } end payload[:attachment_urls] = [:attachment_urls] if [:attachment_urls] payload end |
#parse_message(html) ⇒ Object
28 29 30 |
# File 'lib/flow_chat/intercom/client.rb', line 28 def (html) self.class.parse_html(html) end |
#send_message(conversation_id, prompt, choices: nil, media: nil) ⇒ Hash
Send a reply to a conversation
43 44 45 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 109 110 111 112 113 |
# File 'lib/flow_chat/intercom/client.rb', line 43 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 = [:attachment_urls] # MESSAGE_SENT is instrumented by the gateway, not here. This wrapped # the send in its own instrument block, and ActiveSupport::Notifications # publishes a block event once the block returns whatever it returned - # so the event fired even when the send had failed and this method was # about to answer nil, and fired a second time when the gateway # instrumented the same send. result = begin # Determine message type based on response type = case type when :note "note" else "comment" end reply_data = { id: conversation_id, type: "admin", admin_id: @config.admin_id.to_s, message_type: , body: content.to_s } reply_data[:attachment_urls] = if # Send using official gem reply = intercom.conversations.reply(reply_data) 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, error_type: "resource_not_found", 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, error_type: "authentication", 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, error_type: "server_error", 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 |