Class: FlowChat::Messenger::Client

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/flow_chat/messenger/client.rb

Direct Known Subclasses

Instagram::Client

Constant Summary

Constants included from Instrumentation

Instrumentation::DELIVERED_MESSAGE_ID_KEY, Instrumentation::DELIVERY_DURATION_KEY

Instance Method Summary collapse

Methods included from 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.



10
11
12
13
# File 'lib/flow_chat/messenger/client.rb', line 10

def initialize(config)
  @config = config
  FlowChat.logger.info { "Messenger::Client: Initialized for account #{@config.}" }
end

Instance Method Details

#indicate_typing(recipient_id) ⇒ Object

Shows the person a typing bubble while a turn is being worked out.

Meta clears it when the next message arrives or after about twenty seconds, whichever comes first, so a caller holding one open for longer has to repeat it.

Inherited by Instagram rather than overridden there. Meta documents sender actions under the Messenger Platform and lists only react and unreact for Instagram, but an Instagram send of typing_on is accepted and answered with the recipient id, same as Messenger. Confirmed against a live account, since the reference does not settle it.



50
51
52
# File 'lib/flow_chat/messenger/client.rb', line 50

def indicate_typing(recipient_id)
  post_json(@config.messages_url, {recipient: {id: recipient_id}, sender_action: "typing_on"})
end

#send_message(recipient_id, prompt, choices: nil, media: nil, tag: nil) ⇒ Object

Parameters:

  • tag (String, nil) (defaults to: nil)

    a Meta message tag (e.g. "HUMAN_AGENT") that extends the free-form send window beyond 24 hours. The application decides when a send qualifies; this only carries the value through to every part of the send. Passed through unvalidated: Meta accepts only HUMAN_AGENT as of 27 April 2026 and rejects anything else with error 100, clearly enough that an allowlist here would only be one more thing to keep in sync with Meta's own set.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/flow_chat/messenger/client.rb', line 22

def send_message(recipient_id, prompt, choices: nil, media: nil, tag: nil)
  response = renderer_class.new(prompt, choices: choices, media: media).render
  type, content, options = response

  # 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.
  deliver(recipient_id, type, content, options, tag)
end

#send_text(recipient_id, text, tag: nil) ⇒ Object



35
36
37
# File 'lib/flow_chat/messenger/client.rb', line 35

def send_text(recipient_id, text, tag: nil)
  send_message(recipient_id, text, tag: tag)
end

#upload_media(url, type: :image) ⇒ Object

Uploads a file for reuse and returns the id Meta assigned it.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/flow_chat/messenger/client.rb', line 55

def upload_media(url, type: :image)
  payload = {
    message: {
      attachment: {
        type: type.to_s,
        payload: {url: url, is_reusable: true}
      }
    }
  }

  result = post_json(@config.attachment_upload_url, payload)
  result && result["attachment_id"]
end