新增 Telegram 渠道扩展,支持在 Yuxi 平台中集成 Telegram 即时通讯渠道。 包含以下功能模块: - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - polling: 长轮询模式 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - actions: 动作处理 - inline_keyboard: 内联键盘 - native_commands: 原生指令 - chat: 聊天管理 - delivery: 消息送达确认 - media: 媒体资源处理 - profile: 用户资料 - reactions: 表情反应 - sticker: 贴纸处理 - types: 类型定义
204 lines
7.0 KiB
Python
204 lines
7.0 KiB
Python
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", "")
|