$ show khoben/telemirror
Telemirror is a Telegram message mirroring application that uses the client API (Telethon) to forward messages between channels, chats, and topics in real-time. It supports one-to-one, many-to-one, and many-to-many mappings with per-direction filter pipelines. The system handles new messages, edits, deletions, and albums, and can operate with either an in-memory cache (backed by an LRU dict) or a PostgreSQL database for persisting message ID mappings. It includes a plugin-based filter system for message transformation (URL removal, keyword replacement, forward formatting, content skipping) and patches Telethon's sending functions to support topic replies. The application is designed to run as a long-lived service with a health endpoint for PaaS deployment (Heroku, Docker). The codebase is well-structured with clean separation of concerns, async-first design, and modern Python features (dataclasses, protocols, enums, pattern matching).
The LLM identified 6 files worth highlighting for reuse:
A generic, thread-safe LRU cache implementation using collections.OrderedDict. Supports capacity limits and configurable free factor for bulk eviction. Fully self-contained and can be extracted into any Python project needing a bounded cache with LRU semantics.
A URL matching utility that parses URLs using RFC 3986 regex and supports blacklist/whitelist filtering. Provides both exact match and substring search within text. Standalone and reusable for any URL filtering task (e.g., content moderation, link stripping).
Defines a generic filter pipeline protocol with MessageFilter, CompositeMessageFilter, and FilterAction enum. Supports sequential processing, early termination (DISCARD), and forced send (FORCE_SEND). The pattern is cleanly abstracted and could be reused for any sequential data transformation pipeline beyond Telegram messages.
Contains concrete filter implementations including ForwardFormatFilter (template-based message reformatting with channel name, link, sender info), KeywordReplaceFilter (regex-based keyword replacement with case-preserving logic), and URL/message skipping filters. The KeywordReplaceFilter's case-preserving replacement logic is non-trivial and demonstrates careful handling of text transformations.
Provides a clean Database protocol with two implementations: InMemoryDatabase (backed by LRUCache) and PostgresDatabase (using psycopg async connection pool). The abstraction allows swapping storage without changing business logic. The InMemoryDatabase uses a custom LRU cache for bounded memory usage, demonstrating practical caching in an async context.
Patches Telethon's send_message, forward_messages, and send_file to support the reply_to_topic_id parameter, which was missing from the library at the time. The patch carefully wraps the original functions while preserving all existing functionality. Demonstrates how to extend third-party async libraries without forking.