在使用telethon库向telegram频道或群组发送文件(如照片和视频)或文本消息时,一个常见需求是希望能够为这些消息设置一个历史日期,使其看起来像是过去某个时间点发送的。例如,用户可能希望将一个旧文件夹中的照片上传到telegram,并让这些照片显示为它们原始拍摄日期的时间戳,以构建一个按时间排序的“备份”或“时间线”。
然而,Telegram的API设计从根本上阻止了这种操作。无论是通过Telethon的send_file函数发送文件,还是通过send_message函数发送文本消息,所有发送的消息都将自动带有服务器接收到该消息时的当前日期和时间。这意味着,用户无法通过任何API参数来指定一个过去的时间戳,也无法在消息发送后修改其时间戳。
为什么存在此限制?
Telegram强制消息时间戳的真实性,主要出于数据完整性和防止信息伪造的考虑。如果用户可以随意设置消息的发送时间,将可能导致以下问题:
因此,这一限制是Telegram平台安全和数据可信度的重要基石。
由于Telegram API层面的限制,Telethon作为其Python封装库,自然也无法提供设置追溯性时间戳的功能。无论您如何调用send_file或send_message,消息的时间戳都将是其被发送到Telegram服务器时的实际时间。
以下是一个典型的send_file操作示例:
from telethon.sync import TelegramClient from telethon import utils import os # 替换为您的API ID和API Hash api_id = 1234567 api_hash = 'your_api_hash' phone_number = '+861234567890' # 您的电话号码 # 会话文件路径 session_name = 'my_session' # 目标实体(可以是群组、频道或用户ID/用户名) # 例如:'me' (保存消息), '@my_channel_username', -1001234567890 (频道ID) target_entity = 'me' async def send_media_with_current_time(client, file_path, caption=None): """ 发送文件到Telegram,文件将带有当前的发送时间。 """ try: print(f"正在发送文件: {file_path}") message = await client.send_file( target_entity, file=file_path, caption=caption, # 没有参数可以设置追溯性时间戳 ) print(f"文件发送成功!消息ID: {message.id}, 发送时间: {message.date}") except Exception as e: print(f"发送文件失败: {e}") async def main(): client = TelegramClient(session_name, api_id, api_hash) await client.start(phone=phone_number) # 示例文件路径 test_file = 'path/to/your/image.jpg' # 替换为实际文件路径 if os.path.exists(test_file): await send_media_with_current_time(client, test_file, caption="这是一张测试图片") else: print(f"错误:文件 {test_file} 不存在。请替换为有效文件路径。") await client.run_until_disconnected() if __name__ == '__main__': import asyncio asyncio.run(main())
在上述代码中,message.date属性将始终反映文件被成功上传并发送到Telegram时的日期和时间,而不是您希望的任何自定义历史日期。
尽管无法修改消息的实际时间戳,但您可以通过在消息内容或文件描述(caption)中明确指出文件的原始日期,来模拟“时间线”效果。这是目前唯一可行的、且符合Telegram设计哲学的方法。
方法:在消息标题/描述中包含日期信息
您可以在发送文件时,在caption参数中加入文件的原始日期信息。当用户浏览这些消息时,他们可以通过阅读描述来了解文件的真实历史背景。
from telethon.sync import TelegramClient import os from datetime import datetime # ... (api_id, api_hash, phone_number, session_name, target_entity 定义同上) ... async def send_media_with_historical_caption(client, file_path, original_date, description=None): """ 发送文件到Telegram,并在描述中包含原始日期信息。 """ caption_text = f"原始日期: {original_date.strftime('%Y-%m-%d %H:%M:%S')}" if description: caption_text += f"\n{description}" try: print(f"正在发送文件: {file_path}, 附加描述: {caption_text}") message = await client.send_file( target_entity, file=file_path, caption=caption_text, ) print(f"文件发送成功!消息ID: {message.id}, 实际发送时间: {message.date}") except Exception as e: print(f"发送文件失败: {e}") async def main_with_caption(): client = TelegramClient(session_name, api_id, api_hash) await client.start(phone=phone_number) # 假设有一个文件,其原始日期是2012年6月5日 test_file_old = 'path/to/your/old_image.jpg' # 替换为实际文件路径 original_file_date = datetime(2012, 6, 5, 0, 0, 0) # 原始日期 if os.path.exists(test_file_old): await send_media_with_historical_caption( client, test_file_old, original_file_date, description="这是一张2012年的旧照片备份。" ) else: print(f"错误:文件 {test_file_old} 不存在。请替换为有效文件路径。") await client.run_until_disconnected() if __name__ == '__main__': import asyncio # 运行带有描述的发送示例 asyncio.run(main_with_caption())
通过这种方式,虽然消息在Telegram中显示的发送时间是当前的,但其内容清晰地指明了它所代表的实际历史日期。用户在浏览时,可以依据描述中的日期信息来理解和组织这些文件。
Telegram平台出于数据完整性和防止伪造的目的,不允许用户通过API设置或修改消息的发送时间戳。所有通过Telethon发送的消息,无论是文件还是文本,都将自动带上其发送时的实际日期和时间。
对于需要构建“历史时间线”或“档案备份”的用户,唯一的替代方案是在消息的标题(caption)或文本内容中明确地包含文件的原始日期信息。这种方法虽然不能改变消息本身的系统时间戳,但能有效地向接收者传达文件的历史背景,从而实现类似的时间排序和归档效果。
以上就是Telegram消息时间戳:Telethon发送文件与消息的日期限制解析的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号