botkit-matrix 0.1.0

Matrix implementation for botkit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::sync::Arc;

use botkit_core::{BotBuilder, BotError, Context, IntoHandler, Response};
use matrix_sdk::config::SyncSettings;
use matrix_sdk::ruma::api::client::session::get_login_types::v3::LoginType;
use matrix_sdk::ruma::events::reaction::OriginalSyncReactionEvent;
use matrix_sdk::ruma::events::room::member::StrippedRoomMemberEvent;
use matrix_sdk::ruma::events::room::message::{MessageType, OriginalSyncRoomMessageEvent};
use matrix_sdk::{Client, Room, RoomState};

use crate::client::MatrixClient;
use crate::config::{MatrixAuth, MatrixConfig};
use crate::event::MatrixContextData;

/// Matrix bot builder
///
/// Create a bot with command and reaction handlers, then call `run()` to start.
///
/// # Example
/// ```ignore
/// use botkit_matrix::{MatrixBot, MatrixConfig};
/// use botkit_core::extractor::User;
///
/// async fn ping() -> &'static str { "Pong!" }
/// async fn greet(user: User) -> String { format!("Hello, {}!", user.name) }
///
/// let config = MatrixConfig::new("https://matrix.org")
///     .password_auth("@bot:matrix.org", "password")
///     .command_prefix("!");
///
/// MatrixBot::new(config)
///     .command("ping", ping)
///     .command("greet", greet)
///     .run()
///     .await
///     .unwrap();
/// ```
pub struct MatrixBot {
    config: MatrixConfig,
    builder: BotBuilder,
}

impl MatrixBot {
    /// Create a new Matrix bot with the given configuration
    pub fn new(config: MatrixConfig) -> Self {
        Self {
            config,
            builder: BotBuilder::new(),
        }
    }

    /// Register a command handler (e.g., !ping, !help)
    ///
    /// Commands are parsed from message text using the configured prefix.
    pub fn command<H, Args>(mut self, name: impl Into<String>, handler: H) -> Self
    where
        H: IntoHandler<Args>,
    {
        self.builder = self.builder.command(name, handler);
        self
    }

    /// Register a command handler with description
    pub fn command_with_description<H, Args>(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        handler: H,
    ) -> Self
    where
        H: IntoHandler<Args>,
    {
        self.builder = self
            .builder
            .command_with_description(name, description, handler);
        self
    }

    /// Register a reaction handler
    ///
    /// Maps emoji reactions to button-like callbacks.
    /// The key can be Unicode emoji (e.g., "👍") or the emoji will be matched directly.
    pub fn reaction<H, Args>(mut self, emoji: impl Into<String>, handler: H) -> Self
    where
        H: IntoHandler<Args>,
    {
        // Map reaction to button handler for unified pattern
        let button_id = format!("reaction:{}", emoji.into());
        self.builder = self.builder.button(button_id, handler);
        self
    }

    /// Register a message handler (for non-command messages)
    pub fn message<H, Args>(mut self, handler: H) -> Self
    where
        H: IntoHandler<Args>,
    {
        self.builder = self.builder.message(handler);
        self
    }

    /// Run the bot using Matrix sync loop
    ///
    /// This connects to the homeserver, authenticates, and processes events
    /// using the registered handlers. It runs forever until interrupted.
    pub async fn run(self) -> Result<(), BotError> {
        let client = self.build_client().await?;
        let matrix_client = MatrixClient::new(client.clone());

        // Wrap self in Arc for sharing across handlers
        let bot = Arc::new(BotState {
            builder: self.builder,
            command_prefix: self.config.command_prefix.clone(),
        });

        // Register message event handler
        let bot_clone = Arc::clone(&bot);
        let matrix_client_clone = matrix_client.clone();
        client.add_event_handler(move |event: OriginalSyncRoomMessageEvent, room: Room| {
            let bot = Arc::clone(&bot_clone);
            let matrix_client = matrix_client_clone.clone();
            async move {
                if room.state() != RoomState::Joined {
                    return;
                }

                // Ignore messages from ourselves
                let sdk_client = room.client();
                if sdk_client.user_id().is_some_and(|u| u == event.sender) {
                    return;
                }

                if let Err(e) = handle_message(&bot, &matrix_client, &event, room).await {
                    tracing::error!("Error handling message: {}", e);
                }
            }
        });

        // Register reaction event handler
        let bot_clone = Arc::clone(&bot);
        let matrix_client_clone = matrix_client.clone();
        client.add_event_handler(move |event: OriginalSyncReactionEvent, room: Room| {
            let bot = Arc::clone(&bot_clone);
            let matrix_client = matrix_client_clone.clone();
            async move {
                if room.state() != RoomState::Joined {
                    return;
                }

                // Ignore reactions from ourselves
                let sdk_client = room.client();
                if sdk_client.user_id().is_some_and(|u| u == event.sender) {
                    return;
                }

                if let Err(e) = handle_reaction(&bot, &matrix_client, &event, room).await {
                    tracing::error!("Error handling reaction: {}", e);
                }
            }
        });

        // Auto-join rooms if configured
        if self.config.auto_join_rooms {
            client.add_event_handler(
                |event: StrippedRoomMemberEvent, room: Room, client: Client| async move {
                    // Only handle invites for ourselves
                    if event.state_key != client.user_id().expect("logged in") {
                        return;
                    }

                    if room.state() == RoomState::Invited {
                        tracing::info!("Joining room {}", room.room_id());
                        if let Err(e) = room.join().await {
                            tracing::error!("Failed to join room {}: {}", room.room_id(), e);
                        }
                    }
                },
            );
        }

        // Initial sync to get room state
        tracing::info!("Starting initial sync...");
        client
            .sync_once(SyncSettings::default())
            .await
            .map_err(|e| BotError::Connection(e.to_string()))?;

        tracing::info!("Matrix bot connected and syncing");

        // Run sync loop forever
        client
            .sync(SyncSettings::default())
            .await
            .map_err(|e| BotError::Connection(e.to_string()))?;

        Ok(())
    }

    /// Build and connect the Matrix client
    async fn build_client(&self) -> Result<Client, BotError> {
        #[cfg(not(target_arch = "wasm32"))]
        let client_builder = {
            let client_builder = Client::builder().homeserver_url(&self.config.homeserver_url);

            if let Some(path) = &self.config.state_store_path {
                client_builder.sqlite_store(path, None)
            } else {
                client_builder
            }
        };

        #[cfg(target_arch = "wasm32")]
        let client_builder = Client::builder().homeserver_url(&self.config.homeserver_url);

        let client = client_builder
            .build()
            .await
            .map_err(|e| BotError::Connection(e.to_string()))?;

        // Authenticate based on config
        match &self.config.auth {
            MatrixAuth::Password { user_id, password } => {
                // Check if password login is supported
                let login_types = client
                    .matrix_auth()
                    .get_login_types()
                    .await
                    .map_err(|e| BotError::Auth(e.to_string()))?;

                let supports_password = login_types
                    .flows
                    .iter()
                    .any(|f| matches!(f, LoginType::Password(_)));

                if !supports_password {
                    return Err(BotError::Auth(
                        "Homeserver does not support password login".to_string(),
                    ));
                }

                let mut login_builder = client.matrix_auth().login_username(user_id, password);

                if let Some(device_name) = &self.config.device_name {
                    login_builder = login_builder.initial_device_display_name(device_name);
                }

                login_builder
                    .await
                    .map_err(|e| BotError::Auth(e.to_string()))?;

                tracing::info!("Logged in as {}", user_id);
            }
            MatrixAuth::AccessToken {
                user_id,
                access_token,
                device_id,
            } => {
                use matrix_sdk::authentication::matrix::MatrixSession;
                use matrix_sdk::{SessionMeta, SessionTokens};

                let session = MatrixSession {
                    meta: SessionMeta {
                        user_id: user_id.clone(),
                        device_id: device_id.clone(),
                    },
                    tokens: SessionTokens {
                        access_token: access_token.clone(),
                        refresh_token: None,
                    },
                };

                client
                    .restore_session(session)
                    .await
                    .map_err(|e| BotError::Auth(e.to_string()))?;

                tracing::info!("Restored session for {}", user_id);
            }
        }

        Ok(client)
    }
}

/// Internal state shared across event handlers
struct BotState {
    builder: BotBuilder,
    command_prefix: String,
}

async fn handle_message(
    bot: &BotState,
    client: &MatrixClient,
    event: &OriginalSyncRoomMessageEvent,
    room: Room,
) -> Result<(), BotError> {
    let MessageType::Text(text_content) = &event.content.msgtype else {
        return Ok(());
    };

    let body = &text_content.body;

    // Determine event type and value for routing
    let (event_type, value) = if body.starts_with(&bot.command_prefix) {
        let without_prefix = &body[bot.command_prefix.len()..];
        let mut parts = without_prefix.splitn(2, char::is_whitespace);
        let cmd_name = parts.next().unwrap_or("");
        ("command", cmd_name.to_string())
    } else {
        ("message", body.clone())
    };

    // Find matching handler
    if let Some(handler) = bot.builder.find_handler(event_type, &value) {
        let data = MatrixContextData::from_message(
            event,
            room.clone(),
            client.clone(),
            &bot.command_prefix,
        );
        let ctx = Context::new(data);

        let response = handler.call(ctx).await;
        send_response(client, &room, response).await?;
    }

    Ok(())
}

async fn handle_reaction(
    bot: &BotState,
    client: &MatrixClient,
    event: &OriginalSyncReactionEvent,
    room: Room,
) -> Result<(), BotError> {
    // Extract reaction key (emoji)
    let emoji = &event.content.relates_to.key;
    let button_id = format!("reaction:{}", emoji);

    if let Some(handler) = bot.builder.find_handler("button", &button_id) {
        let data = MatrixContextData::from_reaction(event, room.clone(), client.clone());
        let ctx = Context::new(data);

        let response = handler.call(ctx).await;
        send_response(client, &room, response).await?;
    }

    Ok(())
}

async fn send_response(
    client: &MatrixClient,
    room: &Room,
    response: Response,
) -> Result<(), BotError> {
    if response.is_empty() || response.is_acknowledge() {
        return Ok(());
    }

    // Get content
    let content = match response.content() {
        Some(c) if !c.is_empty() => c,
        _ => return Ok(()),
    };

    // Check for embeds - convert to formatted message
    let embeds = response.embeds();

    if embeds.is_empty() {
        // Plain text message
        client.send_message(room, content).await?;
    } else {
        // Build HTML from content and embeds
        let html = embeds_to_html(content, embeds);
        client.send_formatted_message(room, content, &html).await?;
    }

    Ok(())
}

fn embeds_to_html(text: &str, embeds: &[botkit_core::types::Embed]) -> String {
    let mut html = String::new();

    if !text.is_empty() {
        html.push_str(&format!("<p>{}</p>", escape_html(text)));
    }

    for embed in embeds {
        html.push_str("<blockquote>");

        if let Some(title) = &embed.title {
            html.push_str(&format!("<strong>{}</strong><br/>", escape_html(title)));
        }

        if let Some(desc) = &embed.description {
            html.push_str(&format!("{}<br/>", escape_html(desc)));
        }

        for field in &embed.fields {
            html.push_str(&format!(
                "<em>{}:</em> {}<br/>",
                escape_html(&field.name),
                escape_html(&field.value)
            ));
        }

        html.push_str("</blockquote>");
    }

    html
}

fn escape_html(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}