ForcePilot/backend/package/yuxi/channels/adapters/telegram/poll.py
Kris 71fec609bb feat(telegram-adapter): 实现完整的Telegram适配器基础代码
新增了Telegram适配器的全套基础模块,包括:
1.  核心适配器入口与会话工具
2.  账号管理、认证与配置系统
3.  连接相关的轮询、Webhook、更新偏移管理
4.  话题路由、管理与缓存系统
5.  消息反抖动、超时配置与工具类
6.  响应式UI与命令交互系统
7.  反应表情与通知系统
8.  审批与安全审计模块
9.  健康检查与状态监控
10. 贴纸缓存与视觉工具
11. 流式响应与协作功能
12. 群组迁移与目标归一化处理
2026-05-12 00:49:52 +08:00

87 lines
2.2 KiB
Python

from __future__ import annotations
from typing import Any
from telegram import Bot
from telegram.error import TelegramError
from yuxi.utils.logging_config import logger
async def create_poll(
bot: Bot,
chat_id: str,
question: str,
options: list[str],
is_anonymous: bool = True,
allows_multiple_answers: bool = False,
poll_type: str = "regular",
open_period: int | None = None,
close_date: int | None = None,
visibility: str = "public",
**kwargs,
) -> Any:
try:
poll_kwargs = {
"chat_id": chat_id,
"question": question,
"options": options,
"is_anonymous": is_anonymous,
"allows_multiple_answers": allows_multiple_answers,
"type": poll_type,
"open_period": open_period,
"close_date": close_date,
**kwargs,
}
if visibility == "quiz" and poll_type == "quiz":
poll_kwargs["type"] = "quiz"
return await bot.send_poll(**poll_kwargs)
except TelegramError as e:
logger.error(f"[Telegram] Failed to create poll: {e}")
raise
async def stop_poll(
bot: Bot,
chat_id: str,
message_id: int,
) -> Any:
try:
return await bot.stop_poll(
chat_id=chat_id,
message_id=message_id,
)
except TelegramError as e:
logger.error(f"[Telegram] Failed to stop poll: {e}")
raise
async def create_quiz(
bot: Bot,
chat_id: str,
question: str,
options: list[str],
correct_option_id: int = 0,
explanation: str | None = None,
is_anonymous: bool = True,
open_period: int | None = None,
close_date: int | None = None,
**kwargs,
) -> Any:
try:
return await bot.send_poll(
chat_id=chat_id,
question=question,
options=options,
type="quiz",
correct_option_id=correct_option_id,
explanation=explanation or "",
is_anonymous=is_anonymous,
open_period=open_period,
close_date=close_date,
**kwargs,
)
except TelegramError as e:
logger.error(f"[Telegram] Failed to create quiz: {e}")
raise