feat: openai proxying

This commit is contained in:
2026-07-15 14:23:05 +07:00
parent 3d171206ca
commit 4e6af66362
4 changed files with 7 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ API_BASE_URL=https://api.openai.com/v1
API_TOKEN=sk-replace-me
MODEL=gpt-4o-mini
# Optional proxy used by aiogram to reach Telegram API.
# Optional proxy used for both Telegram API and OpenAI-compatible API.
# Supports HTTP and SOCKS URLs when aiohttp-socks is installed.
# TELEGRAM_PROXY=socks5://user:pass@127.0.0.1:1080
TELEGRAM_PROXY=

View File

@@ -5,7 +5,7 @@ Minimal aiogram bot that forwards whitelisted Telegram users' text messages to a
## Features
- Configurable `API_BASE_URL` and `API_TOKEN`
- Optional proxy for aiogram Telegram API connections
- Optional proxy for Telegram API and OpenAI-compatible API connections
- Telegram typing animation while the model response is streaming
- Required users whitelist
- Persistent per-user context in SQLite
@@ -61,7 +61,7 @@ python bot.py
## Proxy
Set `TELEGRAM_PROXY` in `.env` to route aiogram traffic through a proxy:
Set `TELEGRAM_PROXY` in `.env` to route both Telegram API and OpenAI-compatible API traffic through a proxy:
```dotenv
TELEGRAM_PROXY=socks5://user:pass@127.0.0.1:1080

3
bot.py
View File

@@ -14,6 +14,7 @@ from aiogram.exceptions import TelegramBadRequest
from aiogram.filters import Command, CommandStart
from aiogram.types import Message
from dotenv import load_dotenv
from httpx import AsyncClient as HttpxAsyncClient
from openai import APIConnectionError, APIError, APITimeoutError, AsyncOpenAI, NotFoundError, RateLimitError
@@ -136,11 +137,13 @@ def load_settings() -> Settings:
settings = load_settings()
http_client = HttpxAsyncClient(proxy=settings.telegram_proxy) if settings.telegram_proxy else None
client = AsyncOpenAI(
api_key=settings.api_token,
base_url=settings.api_base_url,
timeout=settings.api_timeout_seconds,
max_retries=settings.api_max_retries,
http_client=http_client,
)
user_locks: dict[int, asyncio.Lock] = {}
pending_user_messages: dict[int, list[str]] = {}

View File

@@ -1,4 +1,5 @@
aiogram==3.13.1
aiohttp-socks==0.9.0
httpx[socks]==0.27.0
openai==1.57.0
python-dotenv==1.0.1