feat: support Telegram proxy

This commit is contained in:
MpakobecEx 2026-10-18 17:54:48 +05:00
parent ce9acabde5
commit 48a93425dd
6 changed files with 39 additions and 3 deletions

View File

@ -1,6 +1,10 @@
# Токен от @BotFather
BOT_TOKEN=
# Прокси для Telegram API. Пусто — без прокси.
# В Docker на Linux: http://host.docker.internal:20171
TELEGRAM_PROXY=
# ComfyUI API
COMFY_BASE_URL=http://192.168.31.113:8188
WORKFLOW_T2I=workflows/txt2img.json

View File

@ -29,9 +29,24 @@ Copy-Item .env.example .env
Заполнить `.env`:
- `BOT_TOKEN` — токен от `@BotFather`.
- `TELEGRAM_PROXY` — прокси для Telegram API (пусто — напрямую).
- `COMFY_BASE_URL` — например `http://192.168.31.113:8188`.
- Остальные параметры можно оставить по умолчанию.
### Прокси для Telegram
Если сервер не имеет прямого доступа к Telegram, укажи прокси V2RayA/HTTP/SOCKS5:
```dotenv
# Docker на Linux (V2RayA слушает на хосте)
TELEGRAM_PROXY=http://host.docker.internal:20171
# Запуск без Docker, на том же хосте
# TELEGRAM_PROXY=http://127.0.0.1:20171
```
Для SOCKS5 используй `socks5h://host.docker.internal:20170`; зависимость `socksio` уже включена в `requirements.txt`.
## Запуск
```powershell

8
bot.py
View File

@ -44,13 +44,17 @@ class MpakoBot:
self._last_edit: dict[int, tuple[float, int]] = {}
def run(self) -> None:
application = (
builder = (
Application.builder()
.token(self._settings.bot_token)
.post_init(self._post_init)
.post_shutdown(self._post_shutdown)
.build()
)
if self._settings.telegram_proxy:
builder = builder.proxy(self._settings.telegram_proxy).get_updates_proxy(
self._settings.telegram_proxy
)
application = builder.build()
application.add_handler(CommandHandler("start", self._start))
application.add_handler(CommandHandler("help", self._help))
application.add_handler(CommandHandler("status", self._status))

View File

@ -55,6 +55,7 @@ def _user_ids(name: str) -> frozenset[int]:
@dataclass(frozen=True)
class Settings:
bot_token: str
telegram_proxy: str | None = None
comfy_base_url: str = "http://192.168.31.113:8188"
workflow_t2i: Path = PROJECT_ROOT / "workflows" / "txt2img.json"
workflow_i2i: Path = PROJECT_ROOT / "workflows" / "img2img.json"
@ -88,8 +89,18 @@ class Settings:
if not base_url.startswith(("http://", "https://")):
raise ValueError("COMFY_BASE_URL должен начинаться с http:// или https://")
proxy = os.getenv("TELEGRAM_PROXY", "").strip() or None
if proxy and not proxy.startswith(
("http://", "https://", "socks5://", "socks5h://")
):
raise ValueError(
"TELEGRAM_PROXY должен начинаться с http://, https://, "
"socks5:// или socks5h://"
)
settings = cls(
bot_token=token,
telegram_proxy=proxy,
comfy_base_url=base_url.rstrip("/"),
workflow_t2i=_path("WORKFLOW_T2I", "workflows/txt2img.json"),
workflow_i2i=_path("WORKFLOW_I2I", "workflows/img2img.json"),

View File

@ -2,6 +2,8 @@ services:
bot:
build: .
env_file: .env
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
- ./storage:/app/storage
- ./data:/app/data

View File

@ -1,5 +1,5 @@
python-telegram-bot>=21.6,<23
httpx>=0.27,<1
httpx[socks]>=0.27,<1
aiosqlite>=0.20,<1
websockets>=13,<16
python-dotenv>=1.0,<2