Add Telegram proxy support
This commit is contained in:
parent
db89c14b37
commit
ba466b6b03
|
|
@ -85,6 +85,7 @@ cp serv/.env.example serv/.env
|
||||||
- `NEW_QWEN_GIGACHAT_API_BASE_URL` - базовый URL inference API GigaChat
|
- `NEW_QWEN_GIGACHAT_API_BASE_URL` - базовый URL inference API GigaChat
|
||||||
- `NEW_QWEN_GIGACHAT_OAUTH_URL` - URL получения access token GigaChat
|
- `NEW_QWEN_GIGACHAT_OAUTH_URL` - URL получения access token GigaChat
|
||||||
- `NEW_QWEN_YANDEXGPT_MODEL` - имя модели для будущего YandexGPT adapter
|
- `NEW_QWEN_YANDEXGPT_MODEL` - имя модели для будущего YandexGPT adapter
|
||||||
|
- `NEW_QWEN_TELEGRAM_PROXY` - необязательный HTTPS прокси для доступа Telegram (например `http://127.0.0.1:7890`)
|
||||||
- `NEW_QWEN_TOOL_POLICY` - режим инструментов:
|
- `NEW_QWEN_TOOL_POLICY` - режим инструментов:
|
||||||
`full-access` - все инструменты
|
`full-access` - все инструменты
|
||||||
`workspace-write` - без `exec_command`
|
`workspace-write` - без `exec_command`
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
NEW_QWEN_BOT_TOKEN=0000000000:telegram-token
|
NEW_QWEN_BOT_TOKEN=0000000000:telegram-token
|
||||||
NEW_QWEN_SERVER_URL=http://127.0.0.1:8080
|
NEW_QWEN_SERVER_URL=http://127.0.0.1:8080
|
||||||
NEW_QWEN_POLL_TIMEOUT=30
|
NEW_QWEN_POLL_TIMEOUT=30
|
||||||
|
NEW_QWEN_TELEGRAM_PROXY=http://127.0.0.1:7890
|
||||||
|
|
|
||||||
|
|
@ -643,7 +643,7 @@ def handle_message(api: TelegramAPI, config: BotConfig, state: dict[str, Any], m
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
config = BotConfig.load()
|
config = BotConfig.load()
|
||||||
api = TelegramAPI(config.token)
|
api = TelegramAPI(config.token, proxy_url=config.proxy_url)
|
||||||
state = load_state()
|
state = load_state()
|
||||||
print("new-qwen bot polling started")
|
print("new-qwen bot polling started")
|
||||||
while True:
|
while True:
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ class BotConfig:
|
||||||
token: str
|
token: str
|
||||||
server_url: str
|
server_url: str
|
||||||
poll_timeout: int
|
poll_timeout: int
|
||||||
|
proxy_url: str | None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls) -> "BotConfig":
|
def load(cls) -> "BotConfig":
|
||||||
|
|
@ -33,5 +34,5 @@ class BotConfig:
|
||||||
token=token,
|
token=token,
|
||||||
server_url=os.environ.get("NEW_QWEN_SERVER_URL", "http://127.0.0.1:8080").rstrip("/"),
|
server_url=os.environ.get("NEW_QWEN_SERVER_URL", "http://127.0.0.1:8080").rstrip("/"),
|
||||||
poll_timeout=int(os.environ.get("NEW_QWEN_POLL_TIMEOUT", "30")),
|
poll_timeout=int(os.environ.get("NEW_QWEN_POLL_TIMEOUT", "30")),
|
||||||
|
proxy_url=os.environ.get("NEW_QWEN_TELEGRAM_PROXY", "").strip() or None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,27 +2,31 @@ from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from urllib import parse, request
|
from urllib import parse, request as urllib_request
|
||||||
|
|
||||||
|
|
||||||
class TelegramAPI:
|
class TelegramAPI:
|
||||||
def __init__(self, token: str) -> None:
|
def __init__(self, token: str, proxy_url: str | None = None) -> None:
|
||||||
self.base_url = f"https://api.telegram.org/bot{token}"
|
self.base_url = f"https://api.telegram.org/bot{token}"
|
||||||
|
handlers = []
|
||||||
|
if proxy_url:
|
||||||
|
handlers.append(urllib_request.ProxyHandler({"https": proxy_url, "http": proxy_url}))
|
||||||
|
self.opener = urllib_request.build_opener(*handlers) if handlers else urllib_request.build_opener()
|
||||||
|
|
||||||
def _post(self, method: str, payload: dict[str, Any]) -> dict[str, Any]:
|
def _post(self, method: str, payload: dict[str, Any]) -> dict[str, Any]:
|
||||||
data = json.dumps(payload).encode("utf-8")
|
data = json.dumps(payload).encode("utf-8")
|
||||||
req = request.Request(
|
req = urllib_request.Request(
|
||||||
f"{self.base_url}/{method}",
|
f"{self.base_url}/{method}",
|
||||||
data=data,
|
data=data,
|
||||||
headers={"Content-Type": "application/json"},
|
headers={"Content-Type": "application/json"},
|
||||||
method="POST",
|
method="POST",
|
||||||
)
|
)
|
||||||
with request.urlopen(req, timeout=90) as response:
|
with self.opener.open(req, timeout=90) as response:
|
||||||
return json.loads(response.read().decode("utf-8"))
|
return json.loads(response.read().decode("utf-8"))
|
||||||
|
|
||||||
def _get(self, method: str, params: dict[str, Any]) -> dict[str, Any]:
|
def _get(self, method: str, params: dict[str, Any]) -> dict[str, Any]:
|
||||||
query = parse.urlencode(params)
|
query = parse.urlencode(params)
|
||||||
with request.urlopen(f"{self.base_url}/{method}?{query}", timeout=90) as response:
|
with self.opener.open(f"{self.base_url}/{method}?{query}", timeout=90) as response:
|
||||||
return json.loads(response.read().decode("utf-8"))
|
return json.loads(response.read().decode("utf-8"))
|
||||||
|
|
||||||
def get_updates(self, offset: int | None, timeout: int) -> list[dict[str, Any]]:
|
def get_updates(self, offset: int | None, timeout: int) -> list[dict[str, Any]]:
|
||||||
|
|
@ -34,4 +38,3 @@ class TelegramAPI:
|
||||||
|
|
||||||
def send_message(self, chat_id: int, text: str) -> None:
|
def send_message(self, chat_id: int, text: str) -> None:
|
||||||
self._post("sendMessage", {"chat_id": chat_id, "text": text})
|
self._post("sendMessage", {"chat_id": chat_id, "text": text})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue