diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py index 09f97598e264..40f3196ba6b4 100644 --- a/plugins/platforms/telegram/adapter.py +++ b/plugins/platforms/telegram/adapter.py @@ -20,7 +20,7 @@ import time from contextvars import ContextVar from datetime import datetime, timezone -from typing import Dict, List, Optional, Set, Any +from typing import Dict, List, Optional, Set, Any, cast logger = logging.getLogger(__name__) @@ -207,7 +207,9 @@ async def _shutdown_abandoned_app(app) -> None: logger.debug("Abandoned Telegram request shutdown failed", exc_info=True) try: + import telegram as _telegram from telegram import Update, Bot, Message, InlineKeyboardButton, InlineKeyboardMarkup + CopyTextButton: Any = getattr(_telegram, "CopyTextButton", None) try: from telegram import LinkPreviewOptions except ImportError: @@ -230,6 +232,7 @@ async def _shutdown_abandoned_app(app) -> None: Message = Any InlineKeyboardButton = Any InlineKeyboardMarkup = Any + CopyTextButton = None LinkPreviewOptions = None Application = Any CommandHandler = Any @@ -373,7 +376,7 @@ def check_telegram_requirements() -> bool: so the adapter's class-level type aliases get rebound. """ global TELEGRAM_AVAILABLE, Update, Bot, Message, InlineKeyboardButton - global InlineKeyboardMarkup, LinkPreviewOptions, Application + global InlineKeyboardMarkup, CopyTextButton, LinkPreviewOptions, Application global CommandHandler, CallbackQueryHandler, TelegramMessageHandler global ContextTypes, filters, ParseMode, ChatType, HTTPXRequest if TELEGRAM_AVAILABLE: @@ -384,8 +387,10 @@ def check_telegram_requirements() -> bool: except Exception: return False try: + import telegram as _telegram from telegram import Update as _Update, Bot as _Bot, Message as _Message from telegram import InlineKeyboardButton as _IKB, InlineKeyboardMarkup as _IKM + _CTB: Any = getattr(_telegram, "CopyTextButton", None) try: from telegram import LinkPreviewOptions as _LPO except ImportError: @@ -405,6 +410,7 @@ def check_telegram_requirements() -> bool: Message = _Message InlineKeyboardButton = _IKB InlineKeyboardMarkup = _IKM + CopyTextButton = _CTB LinkPreviewOptions = _LPO Application = _App CommandHandler = _CH @@ -469,6 +475,36 @@ def _separate_chunk_indicator_from_fence(text: str) -> str: return _CHUNK_INDICATOR_ON_FENCE_RE.sub(r'```\n\g', text) +_COPY_BUTTON_LINE_RE = re.compile( + r'^\s*COPY_BUTTON:\s*(?P