from __future__ import annotations import logging from typing import Any logger = logging.getLogger(__name__) TELEGRAM_API_BASE = "https://api.telegram.org" class TelegramProfile: def __init__(self, outbound): self._outbound = outbound async def set_my_name(self, account_id: str, name: str) -> bool: token = await self._resolve_token(account_id) if not token: return False import httpx try: async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client: resp = await client.post( f"{TELEGRAM_API_BASE}/bot{token}/setMyName", json={"name": name}, ) data = resp.json() if resp.content else {} return resp.status_code == 200 and data.get("ok", False) except Exception: logger.exception("Telegram setMyName error") return False async def get_my_name(self, account_id: str) -> dict | None: token = await self._resolve_token(account_id) if not token: return None import httpx try: async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client: resp = await client.get( f"{TELEGRAM_API_BASE}/bot{token}/getMyName", ) data = resp.json() if resp.content else {} if resp.status_code == 200 and data.get("ok"): return data.get("result", {}) except Exception: logger.exception("Telegram getMyName error") return None async def set_my_description( self, account_id: str, description: str, language_code: str | None = None, ) -> bool: token = await self._resolve_token(account_id) if not token: return False import httpx payload: dict[str, Any] = {"description": description} if language_code: payload["language_code"] = language_code try: async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client: resp = await client.post( f"{TELEGRAM_API_BASE}/bot{token}/setMyDescription", json=payload, ) data = resp.json() if resp.content else {} return resp.status_code == 200 and data.get("ok", False) except Exception: logger.exception("Telegram setMyDescription error") return False async def get_my_description( self, account_id: str, language_code: str | None = None, ) -> dict | None: token = await self._resolve_token(account_id) if not token: return None import httpx params: dict = {} if language_code: params["language_code"] = language_code try: async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client: resp = await client.get( f"{TELEGRAM_API_BASE}/bot{token}/getMyDescription", params=params, ) data = resp.json() if resp.content else {} if resp.status_code == 200 and data.get("ok"): return data.get("result", {}) except Exception: logger.exception("Telegram getMyDescription error") return None async def set_my_short_description( self, account_id: str, short_description: str, language_code: str | None = None, ) -> bool: token = await self._resolve_token(account_id) if not token: return False import httpx payload: dict[str, Any] = {"short_description": short_description} if language_code: payload["language_code"] = language_code try: async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client: resp = await client.post( f"{TELEGRAM_API_BASE}/bot{token}/setMyShortDescription", json=payload, ) data = resp.json() if resp.content else {} return resp.status_code == 200 and data.get("ok", False) except Exception: logger.exception("Telegram setMyShortDescription error") return False async def get_my_short_description( self, account_id: str, language_code: str | None = None, ) -> dict | None: token = await self._resolve_token(account_id) if not token: return None import httpx params: dict = {} if language_code: params["language_code"] = language_code try: async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client: resp = await client.get( f"{TELEGRAM_API_BASE}/bot{token}/getMyShortDescription", params=params, ) data = resp.json() if resp.content else {} if resp.status_code == 200 and data.get("ok"): return data.get("result", {}) except Exception: logger.exception("Telegram getMyShortDescription error") return None async def set_my_profile_photo( self, account_id: str, photo_path: str, ) -> bool: token = await self._resolve_token(account_id) if not token: return False import httpx try: async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client: with open(photo_path, "rb") as f: files = {"photo": ("photo.jpg", f, "image/jpeg")} resp = await client.post( f"{TELEGRAM_API_BASE}/bot{token}/setMyProfilePhoto", files=files, ) data = resp.json() if resp.content else {} return resp.status_code == 200 and data.get("ok", False) except Exception: logger.exception("Telegram setMyProfilePhoto error") return False async def remove_my_profile_photo(self, account_id: str) -> bool: token = await self._resolve_token(account_id) if not token: return False import httpx try: async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client: resp = await client.post( f"{TELEGRAM_API_BASE}/bot{token}/removeMyProfilePhoto", ) data = resp.json() if resp.content else {} return resp.status_code == 200 and data.get("ok", False) except Exception: logger.exception("Telegram removeMyProfilePhoto error") return False async def _resolve_token(self, account_id: str | None) -> str: from yuxi.channel.extensions.telegram.config import TelegramConfigAdapter adapter = TelegramConfigAdapter() aid = account_id or "default" account = await adapter.resolve_account(aid) return account.get("token", "")