We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
from telethon import TelegramClient, events from telethon.errors import FileReferenceExpiredError from telethon.errors import FloodWaitError import configparser import asyncio
api_id = api_hash =
config = configparser.ConfigParser()
config.read('.ini')
pingdao = config.getint('pingdao', 'p1') zhuanfa = config.get('zhuanfa', 'z1')
pingdao2 = config.getint('pingdao', 'p2') zhuanfa2 = config.get('zhuanfa', 'z2')
client = TelegramClient('anon', api_id, api_hash)
messages_list = []
async def process_message(message, chat_id): try: if message.media: text = message.text media = message.media message_id = message.id
target_chat = zhuanfa if chat_id == pingdao else zhuanfa2 matching_messages = [ chat for chat in messages_list if chat['chat_id'] == target_chat and chat['message_id'] == message_id ] if matching_messages: for chat in matching_messages: try: await client.edit_message( target_chat, chat['send_message_id'], text or "", file=media or "" ) except Exception as e: print(f"编辑消息失败: {e}") else: try: send_message = await client.send_message( target_chat, text or "", file=media or "" ) message_info = { "chat_id": target_chat, "message_id": message.id, "send_message_id": send_message.id } messages_list.append(message_info) except FileReferenceExpiredError: print("文件引用已过期,无法发送此消息。") except Exception as e: print(f"发送消息失败: {e}") print(f"已成功转发到: {target_chat} 频道 当前消息ID {message.id}") await asyncio.sleep(3) except FloodWaitError as e: print(f"触发了频率限制,等待 {e.seconds} 秒后继续处理") await asyncio.sleep(e.seconds) await process_message(message, chat_id) except Exception as e: print(f"处理消息时发生错误: {e}")
async def main():
await client.start() if not await client.is_user_authorized(): phone = input('请输入您的电话号码(带国际区号,如 +1234567890): ') await client.send_code_request(phone) code = input('请输入验证码: ') await client.sign_in(phone, code) me = await client.get_me() print(f"登录成功!欢迎,{me.username}") dialogs = await client.get_dialogs() channels = [dialog for dialog in dialogs if dialog.is_channel] for channel in channels: print(f"Channel name: {channel.name}, Channel ID: {channel.id}") # 设置消息监听器:监听新消息 @client.on(events.NewMessage(chats=[pingdao,pingdao2])) async def new_message_handler(event): message = event.message if 'Server sent a very new message with ID' in message.text: print(f"警告信息 无需处理:{message.text}") return chat_id = event.chat_id chat_title = event.chat.title or "未知频道" chat_username = event.chat.username or "无用户名" print(f"新消息来自频道:{chat_title}(ID: {chat_id}, 用户名: {chat_username})") # 处理文本内容 asyncio.create_task(process_message(message, chat_id)) # 设置消息编辑监听器:监听已编辑的消息 @client.on(events.MessageEdited(chats=[pingdao,pingdao2])) async def edited_message_handler(event): message = event.message if 'Server sent a very new message with ID' in message.text: print(f"警告信息:{message.text}") chat_id = event.chat_id chat_title = event.chat.title or "未知频道" chat_username = event.chat.username or "无用户名" print(f"消息被编辑,来自频道:{chat_title}(ID: {chat_id}, 用户名: {chat_username})") asyncio.create_task(process_message(message, chat_id)) # 运行事件循环,开始监听新消息 print(f"开始监听 {pingdao} 和 {pingdao2} 频道的消息和编辑...") await client.run_until_disconnected()
asyncio.run(main())
How should I solve this problem or monitor for such prompts? I can reconnect or run again.
It can run for about 4 hours before this problem occurs, causing subsequent messages to be unable to be monitored and forwarded.
No response
1.37
3.x
windows 10
pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Code that causes the issue
from telethon import TelegramClient, events
from telethon.errors import FileReferenceExpiredError
from telethon.errors import FloodWaitError
import configparser
import asyncio
api_id =
api_hash =
创建配置解析器
config = configparser.ConfigParser()
读取配置文件
config.read('.ini')
频道和转发参数
pingdao = config.getint('pingdao', 'p1')
zhuanfa = config.get('zhuanfa', 'z1')
频道和转发参数
pingdao2 = config.getint('pingdao', 'p2')
zhuanfa2 = config.get('zhuanfa', 'z2')
创建 Telegram 客户端
client = TelegramClient('anon', api_id, api_hash)
聊天记录
messages_list = []
异步处理消息的函数
async def process_message(message, chat_id):
try:
if message.media:
text = message.text
media = message.media
message_id = message.id
async def main():
运行客户端
asyncio.run(main())
Expected behavior
How should I solve this problem or monitor for such prompts? I can reconnect or run again.
Actual behavior
It can run for about 4 hours before this problem occurs, causing subsequent messages to be unable to be monitored and forwarded.
How should I solve this problem or monitor for such prompts? I can reconnect or run again.
Traceback
No response
Telethon version
1.37
Python version
3.x
Operating system (including distribution name and version)
windows 10
Other details
No response
Checklist
pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip
and triggered the bug in the latest version.The text was updated successfully, but these errors were encountered: