Adding some Matrix bot debug code
This commit is contained in:
@@ -30,8 +30,10 @@ pub struct BotContext {
|
|||||||
/// listening for messages. Runs the full Matrix sync loop — call from a
|
/// listening for messages. Runs the full Matrix sync loop — call from a
|
||||||
/// `tokio::spawn` task so it doesn't block the main thread.
|
/// `tokio::spawn` task so it doesn't block the main thread.
|
||||||
pub async fn run_bot(config: BotConfig, project_root: PathBuf) -> Result<(), String> {
|
pub async fn run_bot(config: BotConfig, project_root: PathBuf) -> Result<(), String> {
|
||||||
|
let store_path = project_root.join(".story_kit").join("matrix_store");
|
||||||
let client = Client::builder()
|
let client = Client::builder()
|
||||||
.homeserver_url(&config.homeserver)
|
.homeserver_url(&config.homeserver)
|
||||||
|
.sqlite_store(&store_path, None)
|
||||||
.build()
|
.build()
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Failed to build Matrix client: {e}"))?;
|
.map_err(|e| format!("Failed to build Matrix client: {e}"))?;
|
||||||
@@ -57,12 +59,18 @@ pub async fn run_bot(config: BotConfig, project_root: PathBuf) -> Result<(), Str
|
|||||||
.parse()
|
.parse()
|
||||||
.map_err(|_| format!("Invalid room ID '{}'", config.room_id))?;
|
.map_err(|_| format!("Invalid room ID '{}'", config.room_id))?;
|
||||||
|
|
||||||
client
|
// Try to join the room with a timeout. Conduit sometimes hangs or
|
||||||
.join_room_by_id(&target_room_id)
|
// returns errors on join if the bot is already a member.
|
||||||
|
match tokio::time::timeout(
|
||||||
|
std::time::Duration::from_secs(10),
|
||||||
|
client.join_room_by_id(&target_room_id),
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Failed to join room '{}': {e}", config.room_id))?;
|
{
|
||||||
|
Ok(Ok(_)) => slog!("[matrix-bot] Joined room {target_room_id}"),
|
||||||
slog!("[matrix-bot] Joined room {target_room_id}");
|
Ok(Err(e)) => slog!("[matrix-bot] Join room error (may already be a member): {e}"),
|
||||||
|
Err(_) => slog!("[matrix-bot] Join room timed out (may already be a member)"),
|
||||||
|
}
|
||||||
|
|
||||||
let ctx = BotContext {
|
let ctx = BotContext {
|
||||||
bot_user_id,
|
bot_user_id,
|
||||||
@@ -92,8 +100,16 @@ async fn on_room_message(
|
|||||||
room: Room,
|
room: Room,
|
||||||
Ctx(ctx): Ctx<BotContext>,
|
Ctx(ctx): Ctx<BotContext>,
|
||||||
) {
|
) {
|
||||||
|
slog!(
|
||||||
|
"[matrix-bot] Event received: room={} sender={} target={}",
|
||||||
|
room.room_id(),
|
||||||
|
ev.sender,
|
||||||
|
ctx.target_room_id
|
||||||
|
);
|
||||||
|
|
||||||
// Only handle messages in the configured room
|
// Only handle messages in the configured room
|
||||||
if room.room_id() != &*ctx.target_room_id {
|
if room.room_id() != &*ctx.target_room_id {
|
||||||
|
slog!("[matrix-bot] Ignoring message from wrong room");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user