A bot to provide CJK optimized searching for telegram chats.
A bot to provide CJK optimized searching for telegram chats.
A Telegram message search bot with full-text search and Chinese word segmentation support.
Note: This is a Rust rewrite of the original Python implementation. The Python version is deprecated and no longer maintained.
Telegram's built-in search functionality is limited, especially for CJK languages like Chinese where proper word segmentation is crucial. TG Searcher provides a comprehensive solution by indexing your Telegram messages locally and offering powerful full-text search capabilities through a bot interface.
main.rs (Orchestrator)
├─> Config (YAML parsing, validation)
├─> ClientSession (Telegram user login, chat cache)
├─> BackendBot (message monitoring, indexing)
│ └─> Indexer (Tantivy search with jieba)
└─> BotFrontend (bot commands, user interaction)
└─> Storage (pagination state)cargo build --releaseThe binary will be available at target/release/tg-searcher.
cp searcher.yaml.example searcher.yamlsearcher.yaml with your credentials:common:
api_id: 12345678
api_hash: "your_api_hash_here"
runtime_dir: "./tg_searcher_data"
# proxy: "socks5://localhost:1080" # Optional
sessions:
- name: "my_session"
phone: "+1234567890"
backends:
- id: "backend1"
use_session: "my_session"
config:
monitor_all: false
excluded_chats: []
frontends:
- id: "frontend1"
use_backend: "backend1"
config:
bot_token: "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz"
admin_id: 123456789
page_len: 10
private_mode: false
private_whitelist: []api_id: Telegram API IDapi_hash: Telegram API hashruntime_dir: Directory for storing sessions and indexesproxy: Optional SOCKS5 proxy (e.g., socks5://localhost:1080)name: Unique session identifierphone: Phone number for authenticationid: Unique backend identifieruse_session: Reference to session namemonitor_all: Monitor all chats (default: false)excluded_chats: Chat IDs to exclude when monitor_all is trueid: Unique frontend identifieruse_backend: Reference to backend IDbot_token: Bot token from @BotFatheradmin_id: Telegram user ID of the adminpage_len: Results per page (default: 10)private_mode: Restrict access to whitelisted usersprivate_whitelist: List of allowed user IDs# Normal mode
./target/release/tg-searcher -c searcher.yaml
# Clear existing index and start fresh
./target/release/tg-searcher --clear -c searcher.yaml
# Enable debug logging
./target/release/tg-searcher --debug -c searcher.yamlOn first run:
/start to your bot to initialize it/search - Search messages (or just type your query directly)/random - Get a random indexed message/chats [keyword] - List monitored chats, optionally filtered by keyword/stat - Show index statistics/download_chat [min=N] [max=N] [CHAT...] - Download and index chat history/monitor_chat [CHAT...] - Add chat to monitoring list/clear [all|CHAT...] - Clear index (all or specific chats)/find_chat_id - Find chat IDs by name/refresh_chat_names - Refresh chat name cacheThe search supports Whoosh query syntax:
"foo bar" - Search for exact phrasefoo AND bar - Both terms must appearfoo OR bar - Either term can appearNOT foo - Exclude messages containing "foo"foo* - Wildcard (any characters after "foo")foo? - Single character wildcardExamples:
"hello world" - Messages containing exact phrase "hello world"hello AND world - Messages containing both "hello" and "world"NOT spam AND (buy OR sell) - Messages without "spam" but with "buy" or "sell"Use /chats to list available chats. Click a chat button, then reply to that message with your search query to search only within that chat.
…# Run all tests
cargo test
# Run with output
cargo test -- --nocapture
# Run specific test
cargo test test_indexer_basic_operationsCurrent test coverage: 12 passing tests covering:
# Check compilation
cargo check
# Run linter
cargo clippy
# Format code
cargo fmtEngine: Tantivy 0.25
Tokenizer: jieba-rs for Chinese word segmentation
Schema:
content: Full-text indexed with Chinese analyzerurl: Unique identifier (e.g., https://t.me/c/1234567890/123)chat_id: Indexed for filteringpost_time: Indexed and fast-sorted (descending)sender: Stored for displaytokio: Async runtimegrammers-client: Telegram client (integration in progress)tantivy: Full-text search enginejieba-rs: Chinese text segmentationserde: Configuration serializationtracing: Structured loggingclap: CLI argument parsingdashmap: Concurrent hashmapanyhow/thiserror: Error handlingThe bot commands and user experience remain identical to the Python version for easy migration.
./tg-searcher --clear -c searcher.yaml# Remove session file and re-authenticate
rm tg_searcher_data/sessions/my_session.session
./tg-searcher -c searcher.yaml# Via command line
./tg-searcher --debug -c searcher.yaml
# Via environment variable
RUST_LOG=debug ./tg-searcher -c searcher.yamlContributions are welcome! Please:
cargo test)cargo fmt)cargo clippy)No open issues yet, or sync has not completed.