tg2max-relay: peresylka otchyotov report-shot v MAX (lichnyy akkaunt, vkmax)
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""Тонкая обёртка над vkmax (unofficial user-client MAX), переживающая разрывы WS."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from vkmax.client import MaxClient
|
||||
from vkmax.functions.messages import send_file as vk_send_file
|
||||
from vkmax.functions.messages import send_message as vk_send_message
|
||||
|
||||
log = logging.getLogger("relay.maxapi")
|
||||
|
||||
|
||||
class MaxUser:
|
||||
def __init__(self, device_id: str, login_token: str) -> None:
|
||||
self.device_id = device_id
|
||||
self.login_token = login_token
|
||||
self._client: MaxClient | None = None
|
||||
|
||||
async def _new_client(self) -> MaxClient:
|
||||
await self.close()
|
||||
client = MaxClient()
|
||||
await client.connect()
|
||||
await client.login_by_token(self.login_token, self.device_id)
|
||||
self._client = client
|
||||
log.info("MAX: вход выполнен (device=%s)", self.device_id[:8])
|
||||
return client
|
||||
|
||||
async def ensure(self) -> MaxClient:
|
||||
if self._client is None or self._client._connection is None:
|
||||
return await self._new_client()
|
||||
return self._client
|
||||
|
||||
async def close(self) -> None:
|
||||
if self._client is not None:
|
||||
try:
|
||||
await self._client.disconnect()
|
||||
except Exception:
|
||||
pass
|
||||
self._client = None
|
||||
|
||||
async def send_text(self, chat_id: int, text: str) -> None:
|
||||
client = await self.ensure()
|
||||
await asyncio.wait_for(vk_send_message(client, chat_id, text), 150)
|
||||
|
||||
async def send_pdf(self, chat_id: int, path, caption: str) -> None:
|
||||
client = await self.ensure()
|
||||
await asyncio.wait_for(vk_send_file(client, chat_id, str(path), caption), 300)
|
||||
|
||||
async def test_send(self, chat_id: int) -> None:
|
||||
await self.send_text(chat_id, "🟢 tg2max-relay: тестовое сообщение с сервера")
|
||||
Reference in New Issue
Block a user