Telethon脚本无法同步全部目标频道,报错ChannelPrivateError
频道内容同步脚本部分频道失败问题排查与解决
我用Python 3结合Telethon开发Telegram脚本,需求是将指定主频道的内容自动同步到一系列目标频道。因为目标频道数量多,我按PEP8规范把频道列表拆成多行并缩进对齐,但脚本只能给列表里的部分频道发送内容,其余频道没有同步效果。运行脚本时CMD抛出如下报错:
脚本代码
from telethon import TelegramClient, events api_id = 9969279 #API_ID api_hash = 'd5fa4ca47bcdb6f5bda7271f30e607e9' #API_HASH channel = [-1001612029593] #MAIN_CHANNEL_ID to_channel = [-1001408569373,-1001450011693,-1001589562089,-1001324581893,-1001486742214,-1001262799053,-1001577547504,-1001751417089, -1001714682472,-1001173439993,-1001564319953,-1001458872203,-1001580609047,-1001454607304,-1001412291633,-1001307551599, -1001472656059,-1001577461465,-1001577461465,-1001438912928,-1001166100393,-1001226585647,-1001662052611,-1001412291633, -1001213093519,-1001543013350,-1001599148038,-1001250770087,-1001489822613,-1001586463068,-1001173726857,-1001425575134, -1001549263406,-1001566043627,-1001605175905,-1001229590696,-1001442156928] #OTHER_CHANNELS client = TelegramClient('TGBot', api_id, api_hash) @client.on(events.NewMessage(chats=channel)) async def my_event_handler(event): if event.message: for channelx in to_channel: await client.send_message(channelx, event.message) client.start() client.run_until_disconnected()
报错堆栈
Traceback (most recent call last): File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\updates.py", line 451, in _dispatch_update await callback(event) File "C:\Users\mimif\OneDrive\Desktop\python\main.py", line 18, in my_event_handler await client.send_message(channelx, event.message) File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\messages.py", line 825, in send_message return await self.send_file( File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\uploads.py", line 412, in send_file return self._get_response_message(request, await self(request), entity) File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 30, in __call__ return await self._call(self._sender, request, ordered=ordered) File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 84, in _call result = await future telethon.errors.rpcerrorlist.ChannelPrivateError: The channel specified is private and you lack permission to access it. Another reason may be that you were banned from it (caused by SendMediaRequest)
问题原因
报错提示明确:ChannelPrivateError,说明部分目标频道是私有频道,当前脚本登录的账号没有该频道的发消息权限,或者已经被频道封禁。这和代码行长度、PEP8排版完全无关。另外观察到to_channel列表里存在重复的频道ID(比如-1001577461465和-1001412291633各出现两次),重复发送也可能触发平台限制。
解决办法
- 权限排查:逐个检查
to_channel中未同步的频道,确认当前登录账号是否是该频道的管理员,或者拥有发消息的权限;如果是被封禁,需要联系频道管理员解封。 - 去重频道列表:先对
to_channel去重,避免重复发送和不必要的请求:to_channel = list(set(to_channel)) - 添加异常捕获:修改脚本,遇到权限错误时跳过该频道并记录日志,避免整个脚本中断。
修改后的脚本示例:
from telethon import TelegramClient, events from telethon.errors import ChannelPrivateError api_id = 9969279 #API_ID api_hash = 'd5fa4ca47bcdb6f5bda7271f30e607e9' #API_HASH channel = [-1001612029593] #MAIN_CHANNEL_ID to_channel = [-1001408569373,-1001450011693,-1001589562089,-1001324581893,-1001486742214,-1001262799053,-1001577547504,-1001751417089, -1001714682472,-1001173439993,-1001564319953,-1001458872203,-1001580609047,-1001454607304,-1001412291633,-1001307551599, -1001472656059,-1001577461465,-1001577461465,-1001438912928,-1001166100393,-1001226585647,-1001662052611,-1001412291633, -1001213093519,-1001543013350,-1001599148038,-1001250770087,-1001489822613,-1001586463068,-1001173726857,-1001425575134, -1001549263406,-1001566043627,-1001605175905,-1001229590696,-1001442156928] #OTHER_CHANNELS # 去重处理 to_channel = list(set(to_channel)) client = TelegramClient('TGBot', api_id, api_hash) @client.on(events.NewMessage(chats=channel)) async def my_event_handler(event): if event.message: for channelx in to_channel: try: await client.send_message(channelx, event.message) print(f"成功同步到频道 {channelx}") except ChannelPrivateError: print(f"无法同步到频道 {channelx}:无权限或已被封禁") except Exception as e: print(f"同步频道 {channelx} 时发生未知错误:{str(e)}") client.start() client.run_until_disconnected()
内容的提问来源于stack exchange,提问作者Mimi Beard
相关产品推荐
相关产品推荐

