Skip to content

Feat/matrix bot configurable paths#4

Merged
FarisZR merged 11 commits into
masterfrom
feat/matrix-bot-configurable-paths
Jun 26, 2025
Merged

Feat/matrix bot configurable paths#4
FarisZR merged 11 commits into
masterfrom
feat/matrix-bot-configurable-paths

Conversation

@FarisZR

@FarisZR FarisZR commented Jun 26, 2025

Copy link
Copy Markdown
Member

No description provided.

google-labs-jules Bot and others added 11 commits June 26, 2025 21:29
…fter initial sync

Restructures the Matrix bot's main function to:
1. Define callback handlers as regular async functions.
2. Perform an initial `client.sync()` after successful login.
3. Register event callbacks using `client.add_event_callback()` only after the initial sync has completed.

This ensures the `AsyncClient` object is fully initialized and ready before event handlers are attached, preventing the `AttributeError: 'AsyncClient' object has no attribute 'on'` that occurred when using decorators before the client was in a ready state.
This commit implements several changes to runtime behavior:

1.  Refines sync and callback registration: Callbacks are now registered
    immediately after login, followed by a single `sync_forever(full_state=True)`
    call. This aims to improve the responsiveness of invite handling.
2.  Prevents processing of old messages: The bot now records its startup
    time and ignores any messages with a `server_timestamp` older than
    this, preventing it from acting on historical messages when joining a room.
3.  Modifies reply message format: The bot now replies with only the
    substituted link, instead of including `(source: <original_link>)`.
Removes the erroneous `nonlocal bot_startup_time` declaration from the `main` function's scope that was causing a SyntaxError.

`bot_startup_time` is now initialized at the start of `main` and its value is set before callbacks are registered. The `message_handler_callback` reads this value from its enclosing scope without needing a `nonlocal` declaration for reading.
- Makes URL detection regex in `find_links_in_text` case-insensitive.
- Dynamically adds `x.com` as an alternative domain for `twitter.com` in the loaded service configuration, allowing the bot to recognize x.com links as Twitter links.
- Updates README to mention these features.
- Modifies `mk_newlinks` to convert the input URL's netloc to lowercase before matching against `SERVICES` keys and `alt_domains`.
- Ensures that `alt_domains` from `services.json` are also compared in lowercase.
- Ensures that the `service` field from `alts.json` is compared in lowercase against the matched service key.
- This complements the existing `re.IGNORECASE` in `find_links_in_text` to provide more robust case-insensitive link processing.
- Modifies `matrix_bot.py` to read the `LOG_LEVEL` environment variable (defaulting to INFO) and set the Python logging level accordingly.
- Adds `LOG_LEVEL` to `.env.example`.
- Updates `README.md` to document the new `LOG_LEVEL` configuration option.
Reverts the callback registration mechanism from decorators back to using
`client.add_event_callback()`. Callbacks are defined as regular async functions
and then registered explicitly after client login and before the main sync loop starts.

This addresses the `AttributeError: 'AsyncClient' object has no attribute 'on'`
that occurred when decorators were used within the main async function.
- Removes all automatic invitation handling logic. The bot will now only
  operate in rooms it is already a member of (must be manually added).
- Implements session persistence using `matrix-nio`'s client store.
  - Adds `MATRIX_BOT_STORE_PATH` env var to specify store location.
  - Bot attempts to load session from store; uses password only for initial
    login or if store is invalid.
- Updates Dockerfile to create a default store directory (`/app/store`).
- Updates docker-compose.yml to mount a host volume (`./matrix_bot_data/store`)
  to `/app/store` for persisting the session data.
- Updates `.env.example` and `README.md` to reflect these significant changes
  in configuration and operation.
@FarisZR FarisZR merged commit b7cd7a4 into master Jun 26, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant