Class: ActiveMatrix::MessageDispatcher

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/active_matrix/message_dispatcher.rb

Overview

Dispatches Matrix messages with retry logic and typing indicators

Examples:

Basic usage

dispatcher = ActiveMatrix::MessageDispatcher.new(api: api, room_id: '!abc:matrix.org')
dispatcher.send_text('Hello!')

With typing indicator

dispatcher.send_text('Thinking...', typing_delay: 2.0)

Thread reply

dispatcher.send_text('Reply', thread_id: '$event_id')

Constant Summary collapse

DEFAULT_RETRY_COUNT =

Default configuration

3
DEFAULT_BASE_DELAY =
1.0
DEFAULT_TYPING_DELAY =
0.5
DEFAULT_TYPING_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api:, room_id:, user_id:, retry_count: DEFAULT_RETRY_COUNT, base_delay: DEFAULT_BASE_DELAY, typing_delay: DEFAULT_TYPING_DELAY) ⇒ MessageDispatcher

Returns a new instance of MessageDispatcher.

Parameters:

  • api (ActiveMatrix::Api)

    Matrix API instance

  • room_id (String)

    Room ID to send messages to

  • user_id (String)

    User ID for typing indicator

  • retry_count (Integer) (defaults to: DEFAULT_RETRY_COUNT)

    Number of retries on failure

  • base_delay (Float) (defaults to: DEFAULT_BASE_DELAY)

    Base delay in seconds for exponential backoff

  • typing_delay (Float) (defaults to: DEFAULT_TYPING_DELAY)

    Default typing delay in seconds



33
34
35
36
37
38
39
40
41
# File 'lib/active_matrix/message_dispatcher.rb', line 33

def initialize(api:, room_id:, user_id:, retry_count: DEFAULT_RETRY_COUNT,
               base_delay: DEFAULT_BASE_DELAY, typing_delay: DEFAULT_TYPING_DELAY)
  @api = api
  @room_id = room_id
  @user_id = user_id
  @retry_count = retry_count
  @base_delay = base_delay
  @default_typing_delay = typing_delay
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



25
26
27
# File 'lib/active_matrix/message_dispatcher.rb', line 25

def api
  @api
end

#room_idObject (readonly)

Returns the value of attribute room_id.



25
26
27
# File 'lib/active_matrix/message_dispatcher.rb', line 25

def room_id
  @room_id
end

#user_idObject (readonly)

Returns the value of attribute user_id.



25
26
27
# File 'lib/active_matrix/message_dispatcher.rb', line 25

def user_id
  @user_id
end

Instance Method Details

#send_emote(text, typing_delay: nil, thread_id: nil) ⇒ Hash

Send an emote message (/me action)

Parameters:

  • text (String)

    Emote text

  • typing_delay (Float, nil) (defaults to: nil)

    Seconds to show typing indicator

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

    Event ID to reply in thread

Returns:

  • (Hash)

    Response with :event_id



107
108
109
# File 'lib/active_matrix/message_dispatcher.rb', line 107

def send_emote(text, typing_delay: nil, thread_id: nil)
  send_text(text, msgtype: 'm.emote', typing_delay: typing_delay, thread_id: thread_id)
end

#send_html(html, body: nil, msgtype: 'm.text', typing_delay: nil, thread_id: nil) ⇒ Hash

Send an HTML message

Parameters:

  • html (String)

    HTML content

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

    Plain text fallback (auto-generated if nil)

  • msgtype (String) (defaults to: 'm.text')

    Message type (default: ‘m.text’)

  • typing_delay (Float, nil) (defaults to: nil)

    Seconds to show typing indicator

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

    Event ID to reply in thread

Returns:

  • (Hash)

    Response with :event_id



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_matrix/message_dispatcher.rb', line 67

def send_html(html, body: nil, msgtype: 'm.text', typing_delay: nil, thread_id: nil)
  plain_body = body || strip_html(html)

  content = {
    msgtype: msgtype,
    body: plain_body,
    format: 'org.matrix.custom.html',
    formatted_body: html
  }

  send_with_typing(content, typing_delay: typing_delay, thread_id: thread_id)
end

#send_html_notice(html, body: nil, typing_delay: nil, thread_id: nil) ⇒ Hash

Send an HTML notice message

Parameters:

  • html (String)

    HTML content

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

    Plain text fallback

  • typing_delay (Float, nil) (defaults to: nil)

    Seconds to show typing indicator

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

    Event ID to reply in thread

Returns:

  • (Hash)

    Response with :event_id



97
98
99
# File 'lib/active_matrix/message_dispatcher.rb', line 97

def send_html_notice(html, body: nil, typing_delay: nil, thread_id: nil)
  send_html(html, body: body, msgtype: 'm.notice', typing_delay: typing_delay, thread_id: thread_id)
end

#send_notice(text, typing_delay: nil, thread_id: nil) ⇒ Hash

Send a notice message (typically for bot responses)

Parameters:

  • text (String)

    Notice text

  • typing_delay (Float, nil) (defaults to: nil)

    Seconds to show typing indicator

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

    Event ID to reply in thread

Returns:

  • (Hash)

    Response with :event_id



86
87
88
# File 'lib/active_matrix/message_dispatcher.rb', line 86

def send_notice(text, typing_delay: nil, thread_id: nil)
  send_text(text, msgtype: 'm.notice', typing_delay: typing_delay, thread_id: thread_id)
end

#send_text(text, msgtype: 'm.text', typing_delay: nil, thread_id: nil) ⇒ Hash

Send a plain text message

Parameters:

  • text (String)

    Message text

  • msgtype (String) (defaults to: 'm.text')

    Message type (default: ‘m.text’)

  • typing_delay (Float, nil) (defaults to: nil)

    Seconds to show typing indicator (nil to skip)

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

    Event ID to reply in thread

Returns:

  • (Hash)

    Response with :event_id



50
51
52
53
54
55
56
57
# File 'lib/active_matrix/message_dispatcher.rb', line 50

def send_text(text, msgtype: 'm.text', typing_delay: nil, thread_id: nil)
  content = {
    msgtype: msgtype,
    body: text
  }

  send_with_typing(content, typing_delay: typing_delay, thread_id: thread_id)
end

#set_typing(typing: true, timeout: DEFAULT_TYPING_TIMEOUT) ⇒ Object

Show typing indicator

Parameters:

  • typing (Boolean) (defaults to: true)

    Whether to show or hide typing

  • timeout (Integer) (defaults to: DEFAULT_TYPING_TIMEOUT)

    Timeout in seconds



115
116
117
118
119
# File 'lib/active_matrix/message_dispatcher.rb', line 115

def set_typing(typing: true, timeout: DEFAULT_TYPING_TIMEOUT)
  @api.set_typing(@room_id, @user_id, typing: typing, timeout: timeout)
rescue StandardError => e
  ActiveMatrix.logger.debug("Failed to set typing indicator: #{e.message}")
end