Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@ PLUGINS_ENABLED=true
# Skip muted chats (same as «без звука» / не беспокоить in Max)
# SKIP_MUTED=true

# Telegram message formatting (plain | enhanced | compact)
# TG_FORMAT_STYLE=enhanced
# Separator line when sender changes (true/false, default true)
# TG_FORMAT_SEPARATOR=true
# Show message time in header HH:MM (true/false, default true)
# TG_FORMAT_TIMESTAMP=true

# Log directory (default: logs)
# LOG_DIR=logs
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ cp .env.example .env
| `UNREAD_ONLY` | нет | `true` — пересылать только непрочитанные (если прочитали в Max — в TG не придёт) |
| `UNREAD_DELAY_SEC` | нет | Задержка в секундах перед проверкой прочитанности (по умолчанию `2`) |
| `SKIP_MUTED` | нет | `true` — не пересылать из заглушённых чатов Max («без звука») |
| `TG_FORMAT_STYLE` | нет | Стиль оформления: `plain` (как раньше), `enhanced` (по умолчанию), `compact` (enhanced + без повторного заголовка у того же автора) |
| `TG_FORMAT_SEPARATOR` | нет | `true` — линия-разделитель при смене автора (по умолчанию `true`) |
| `TG_FORMAT_TIMESTAMP` | нет | `true` — время сообщения в заголовке `HH:MM` (по умолчанию `true`) |
| `LOG_DIR` | нет | Путь к директории логов (по умолчанию `logs`) |
| `TG_PROXY` | нет | SOCKS5-прокси для Telegram (`socks5://host:port`) |
| `TG_READ_TIMEOUT` | нет | Таймаут чтения HTTP-ответа от Telegram, в секундах |
Expand Down Expand Up @@ -396,6 +399,9 @@ cp .env.example .env
| `UNREAD_ONLY` | no | `true` — forward only unread messages (skip if read in Max) |
| `UNREAD_DELAY_SEC` | no | Delay before read check in seconds (default `2`) |
| `SKIP_MUTED` | no | `true` — skip muted / do-not-disturb chats in Max |
| `TG_FORMAT_STYLE` | no | Message style: `plain`, `enhanced` (default), `compact` (enhanced + hide repeated header for same sender) |
| `TG_FORMAT_SEPARATOR` | no | `true` — separator line when sender changes (default `true`) |
| `TG_FORMAT_TIMESTAMP` | no | `true` — show message time in header as `HH:MM` (default `true`) |
| `LOG_DIR` | no | Log directory path (default: `logs`) |
| `TG_PROXY` | no | SOCKS5 proxy for Telegram (`socks5://host:port`) |
| `TG_READ_TIMEOUT` | no | HTTP read timeout for Telegram responses, in seconds |
Expand Down
21 changes: 21 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import logging
import os
from dataclasses import dataclass

from dotenv import load_dotenv

log = logging.getLogger(__name__)

VALID_TG_FORMAT_STYLES = frozenset({"plain", "enhanced", "compact"})


@dataclass(frozen=True)
class Settings:
Expand All @@ -21,6 +26,9 @@ class Settings:
unread_only: bool = False
unread_delay_sec: float = 2.0
skip_muted: bool = False
tg_format_style: str = "enhanced"
tg_format_separator: bool = True
tg_format_timestamp: bool = True


def load_settings() -> Settings:
Expand All @@ -42,6 +50,14 @@ def load_settings() -> Settings:
f"TG_CHAT_ID must be a valid integer, got: {tg_chat_id!r}"
)

raw_format_style = os.environ.get("TG_FORMAT_STYLE", "enhanced").strip().lower()
if raw_format_style not in VALID_TG_FORMAT_STYLES:
log.warning(
"Unknown TG_FORMAT_STYLE=%r, falling back to 'enhanced'",
raw_format_style,
)
raw_format_style = "enhanced"

return Settings(
max_token=os.environ["MAX_TOKEN"].strip(),
max_device_id=os.environ["MAX_DEVICE_ID"].strip(),
Expand All @@ -58,4 +74,9 @@ def load_settings() -> Settings:
unread_only=os.environ.get("UNREAD_ONLY", "").lower() in ("1", "true", "yes"),
unread_delay_sec=float(os.environ.get("UNREAD_DELAY_SEC", "2") or "2"),
skip_muted=os.environ.get("SKIP_MUTED", "").lower() in ("1", "true", "yes"),
tg_format_style=raw_format_style,
tg_format_separator=os.environ.get("TG_FORMAT_SEPARATOR", "true").lower()
not in ("0", "false", "no"),
tg_format_timestamp=os.environ.get("TG_FORMAT_TIMESTAMP", "true").lower()
not in ("0", "false", "no"),
)
9 changes: 9 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ async def main():
debug=settings.debug, reply_enabled=settings.reply_enabled,
unread_only=settings.unread_only, unread_delay_sec=settings.unread_delay_sec,
skip_muted=settings.skip_muted,
tg_format_style=settings.tg_format_style,
tg_format_separator=settings.tg_format_separator,
tg_format_timestamp=settings.tg_format_timestamp,
)

if settings.unread_only:
Expand All @@ -102,6 +105,12 @@ async def main():
)
if settings.skip_muted:
log.info("Skip-muted mode: ON (no forwards from muted Max chats)")
log.info(
"Message format: style=%s separator=%s timestamp=%s",
settings.tg_format_style,
settings.tg_format_separator,
settings.tg_format_timestamp,
)

tg_app = None
if settings.reply_enabled:
Expand Down
Loading
Loading