Fully vibe coded
A Matrix bot that sanitises and substitutes share links in Matrix rooms with lightweight, privacy-respecting proxy frontend alternatives.
When in a room, the bot will listen for messages containing links. If a supported link (e.g., Twitter, YouTube) is detected, it will post a reply with a link to an alternative frontend (e.g., Nitter, Invidious).
Supported services are configurable via services.json, and the alternative frontends are defined in alts.json.
- Replaces links (e.g., Twitter/X, YouTube, Instagram, Reddit) with privacy-focused alternatives (e.g., Nitter, Invidious). URL detection is case-insensitive.
- Recognizes
x.comas an alias fortwitter.com. (It's recommended to also addx.comto thealt_domainslist fortwitter.comin yourservices.jsonfor full clarity, though the bot attempts to handle this). - Configurable list of supported services and alternative frontends via
services.jsonandalts.json. - Session Persistence: Remembers its Matrix session (access token, device ID) across restarts, avoiding the need for password login every time, by storing data in a specified directory.
- Operates only in rooms it is already a member of (manual addition to rooms required).
- Dockerized for easy deployment.
- A Matrix user account for the bot (register on your chosen homeserver).
- Python >= 3.11.
- Docker and Docker Compose (for Docker-based deployment).
The Matrix bot is configured using environment variables. Create a .env file in the root of the project by copying .env.example and fill in the details:
# .env file for Matrix Bot
HOMESERVER="https://matrix-client.matrix.org" # Your bot's homeserver URL
USER_ID="@yourbotusername:matrix.org" # Full Matrix User ID of the bot
# PASSWORD="yourbotpassword" # Bot's account password. Required for first login or if session store is invalid. Can be removed after first successful login if session is persisted.
# DEVICE_ID="linkmatrixbot" # Optional: A friendly name for the bot's device/session, used on first login.
MATRIX_BOT_STORE_PATH="/app/store" # Path inside the container for storing session data. E.g., /app/store (for Docker) or ./matrix_bot_data/store (for local)
# Optional: Custom paths for alts and services JSON files
# If not set, defaults to sample.config/alts.json and sample.config/services.json relative to the script
# MATRIX_BOT_ALTS_JSON_PATH="/path/to/your/alts.json"
# MATRIX_BOT_SERVICES_JSON_PATH="/path/to/your/services.json"HOMESERVER: The URL of the Matrix homeserver your bot is registered on.USER_ID: The bot's full Matrix ID (e.g.,@botname:yourserver.org).PASSWORD(Optional after first login): The password for the bot's Matrix account. Needed for the very first login, or if the session data inMATRIX_BOT_STORE_PATHbecomes invalid. Can be removed from the.envfile once the bot has successfully logged in and created a session store.DEVICE_ID(Optional): A descriptive name for the bot's device/session, primarily used during the initial login if no session store exists.MATRIX_BOT_STORE_PATH: Required for session persistence. Path to a directory where the bot will store its session data (access token, device ID, sync token). This allows the bot to resume its session on restart without needing the password.- When using Docker, this path should correspond to the container-side path of a mounted volume (e.g.,
/app/storewhich is mapped from./matrix_bot_data/storeon the host indocker-compose.yml). - When running directly with Python, choose a writable path (e.g.,
./matrix_bot_data/store).
- When using Docker, this path should correspond to the container-side path of a mounted volume (e.g.,
MATRIX_BOT_ALTS_JSON_PATH(Optional): Specifies a custom path to thealts.jsonfile.MATRIX_BOT_SERVICES_JSON_PATH(Optional): Specifies a custom path to theservices.jsonfile.LOG_LEVEL(Optional): Sets the logging level for the bot.
The alts.json and services.json files define the link substitution rules. By default, the bot expects these to be in the sample.config/ directory.
The bot needs to be manually invited or added to the Matrix rooms where it should operate. It will not automatically accept invitations.
- Clone the repository.
- Install Python dependencies:
pip install matrix-nio python-dotenv. - Create and configure your
.envfile, ensuringMATRIX_BOT_STORE_PATHpoints to a writable directory (e.g.,./matrix_bot_data/store). - Ensure
sample.config/alts.jsonandsample.config/services.jsonexist (or your custom paths are configured).
- Run the bot:
python matrix_bot.py.- On the first run, ensure
PASSWORDis set in.envfor initial login. The bot will create session files inMATRIX_BOT_STORE_PATH. - On subsequent runs, the bot will attempt to use the stored session.
- On the first run, ensure
- Manually add/invite the bot to the desired Matrix rooms.
- Test: Send a message containing a supported link.
- Docker and Docker Compose installed.
Use the template docker-compose file, make sure to set the required variables