Class: ActiveMatrix::MessageDispatcher
- Inherits:
-
Object
- Object
- ActiveMatrix::MessageDispatcher
- Includes:
- Instrumentation
- Defined in:
- lib/active_matrix/message_dispatcher.rb
Overview
Dispatches Matrix messages with retry logic and typing indicators
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
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#room_id ⇒ Object
readonly
Returns the value of attribute room_id.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#initialize(api:, room_id:, user_id:, retry_count: DEFAULT_RETRY_COUNT, base_delay: DEFAULT_BASE_DELAY, typing_delay: DEFAULT_TYPING_DELAY) ⇒ MessageDispatcher
constructor
A new instance of MessageDispatcher.
-
#send_emote(text, typing_delay: nil, thread_id: nil) ⇒ Hash
Send an emote message (/me action).
-
#send_html(html, body: nil, msgtype: 'm.text', typing_delay: nil, thread_id: nil) ⇒ Hash
Send an HTML message.
-
#send_html_notice(html, body: nil, typing_delay: nil, thread_id: nil) ⇒ Hash
Send an HTML notice message.
-
#send_notice(text, typing_delay: nil, thread_id: nil) ⇒ Hash
Send a notice message (typically for bot responses).
-
#send_text(text, msgtype: 'm.text', typing_delay: nil, thread_id: nil) ⇒ Hash
Send a plain text message.
-
#set_typing(typing: true, timeout: DEFAULT_TYPING_TIMEOUT) ⇒ Object
Show typing indicator.
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.
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
#api ⇒ Object (readonly)
Returns the value of attribute api.
25 26 27 |
# File 'lib/active_matrix/message_dispatcher.rb', line 25 def api @api end |
#room_id ⇒ Object (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_id ⇒ Object (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)
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
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
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)
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
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
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.}") end |