A modern, async Matrix chatbot that uses OpenRouter for AI-powered responses. Built with matrix-nio and Python 3.10+.
- Listens for messages in configured Matrix rooms.
- Activates in a room when mentioned by its display name.
- Once active, responds to all messages in that room.
- Features an active listening decay mechanism: the bot will announce when it stops listening due to inactivity and can be re-activated by a mention.
- Uses OpenRouter API (configurable GPT models) for generating responses.
- Maintains a short-term memory of the conversation in each room for contextual responses.
- Configuration via environment variables.
-
Clone this repo
-
Install dependencies Ensure you have Python 3.10+ installed.
pip install -r requirements.txt
-
Configure environment variables Copy
.env.exampleto.envand fill in your values:cp .env.example .env # Edit .env with your credentialsKey variables:
MATRIX_HOMESERVER: Your Matrix homeserver URL (e.g.,https://matrix.example.org)MATRIX_USER_ID: Your bot's full Matrix user ID (e.g.,@your-bot:example.org)MATRIX_PASSWORD: The bot's passwordMATRIX_ROOM_ID: The primary room ID for the bot to join on startup (e.g.,!yourRoom:example.org)OPENROUTER_API_KEY: Your OpenRouter API key.OPENROUTER_MODEL: (Optional) AI model to use (default:openai/gpt-4o-mini).DEVICE_NAME: (Optional) Device name for the bot's Matrix session.YOUR_SITE_URL: (Optional) For OpenRouter API headers.YOUR_SITE_NAME: (Optional) For OpenRouter API headers.
See
main.pyand.env.examplefor other optional polling and memory configuration variables. -
Run the bot
python main.py
The bot will attempt to fetch its display name from its Matrix profile. This name is used for mentions.
- Activation: In a Matrix room the bot has joined, mention the bot by its display name (e.g., "Hello @mybotname, can you help?"). This will activate the bot for that room.
- Interaction: Once active, the bot will respond to messages sent in the room.
- Deactivation: If there's a period of inactivity in the room, the bot will announce it's stopping active listening. Mention it again to re-activate.
- The bot will also respond to direct messages if it's invited to a DM chat.
- Never commit your actual
.envfile or credentials to version control. The.gitignorefile should prevent this.
MIT