Telethon send_file/send_message能否设置消息发送回溯时间?
如何在Telethon中设置消息的回溯发送时间?
Telethon的send_file和send_message方法支持通过schedule_date参数设置消息显示的时间戳,完全可以实现你要的按文件所属日期回溯发送的需求,具体操作如下:
核心实现方法
调用发送方法时,传入schedule_date参数,值为你指定的过去日期时间(需是带时区的datetime对象)。比如要把2012-06-05文件夹的文件设为当日0点发送,就构造对应时间的datetime对象传入即可。
示例代码
from telethon import TelegramClient from datetime import datetime, timezone # 替换为你的API信息和会话名 api_id = 123456 api_hash = 'your_api_hash_here' session_name = 'backup_session' client = TelegramClient(session_name, api_id, api_hash) async def batch_send_with_backdate(): # 目标私人频道/群组的ID或用户名 target_chat = -1001234567890 # 构造目标日期:2012-06-05 00:00 UTC backdate_time = datetime(2012, 6, 5, 0, 0, 0, tzinfo=timezone.utc) # 批量发送10个文件,使用回溯时间戳 await client.send_file( target_chat, ['2012-06-05/file1.jpg', '2012-06-05/file2.mp4', ..., '2012-06-05/file10.png'], schedule_date=backdate_time ) with client: client.loop.run_until_complete(batch_send_with_backdate())
关键注意事项
- 权限要求:你必须是目标频道/群组的管理员或创建者,否则Telegram API会拒绝设置过去的时间戳(这是平台的权限限制)。
- 时间范围限制:设置的时间戳不能早于目标聊天的创建时间,否则请求会失败。
- 批量文件的时间统一:一次
send_file发送的多个文件会作为同一组媒体消息,共享你设置的时间戳,刚好符合你按文件夹批量设置的需求。 - 时区问题:务必使用带时区的
datetime对象(比如UTC时区),避免本地时区转换导致显示的时间和预期不符。
内容的提问来源于stack exchange,提问作者José Fernandes
相关产品推荐
相关产品推荐

