Telethon中SendVoteRequest无法为Telegram匿名投票投票问题排查
排查Telethon提交匿名投票失败的问题
参数错误:投票选项传递方式不对
你直接用[b'Yes']作为选项参数是错误的。Telegram的投票选项不通过文本匹配,每个选项都有唯一的data字节标识,需要从目标投票消息的PollOption对象中提取,而非直接传选项文本的字节形式。
修正后的代码示例:
from telethon import functions, types async def send_PollAnswer(client, chat_id, message_id): # 先获取目标投票消息 message = await client.get_messages(chat_id, ids=message_id) if not isinstance(message.poll, types.Poll): print("目标消息不是投票类型") return # 匹配文本为"Yes"的选项,提取其data字段 target_option = next(opt for opt in message.poll.options if opt.text == "Yes") result = await client(functions.messages.SendVoteRequest( peer=chat_id, msg_id=message_id, options=[target_option.data] )) print(result.stringify())
连接层面错误:网络或客户端状态异常
你遇到的0 bytes read on a total of 8 expected bytes属于TCP连接中断类错误,可通过以下方式排查:
- 升级Telethon:执行
pip install --upgrade telethon,旧版本可能存在协议兼容问题 - 检查网络环境:确认网络可正常访问Telegram,必要时切换代理
- 重置客户端会话:若会话文件损坏或过期,重新登录生成新的会话文件
- 添加重试逻辑:连接错误可能偶发,增加重试机制提升成功率:
import asyncio from telethon.errors import RPCError async def send_PollAnswer(client, chat_id, message_id): max_retries = 3 for attempt in range(max_retries): try: message = await client.get_messages(chat_id, ids=message_id) target_option = next(opt for opt in message.poll.options if opt.text == "Yes") result = await client(functions.messages.SendVoteRequest( peer=chat_id, msg_id=message_id, options=[target_option.data] )) print(result.stringify()) return except (RPCError, ConnectionError) as e: print(f"第 {attempt+1} 次尝试失败: {str(e)}") if attempt < max_retries -1: await asyncio.sleep(2) print("多次重试后仍未成功提交投票")
额外验证步骤
- 确认目标投票未关闭(
message.poll.closed为False)且是匿名投票(message.poll.anonymous为True) - 检查客户端账号在目标聊天中有投票权限(非只读群组/频道)
- 确保客户端已完成正常登录,会话状态有效
内容的提问来源于stack exchange,提问作者Muhammad HAMMAD ASGHAR
相关产品推荐
相关产品推荐

