通过 id 访问 Telegram 消息

Accessing Telegram Message by its id

我正在使用 Telethon Library 获取消息、过滤它们,稍后如果满足某些条件,则用特定的答案回复它们。 问题是,我能否仅通过消息 ID 获取有关消息的信息(最重要的是它的文本)?

你可以使用 client.get messages

If ids is present in the named arguments and is not a list, a single Message will be returned for convenience instead of a list.

这是一个让您了解想法的最小工作示例:

from telethon import TelegramClient

API_ID= ...
API_HASH=" ... "

client = TelegramClient('session', api_id=API_ID, api_hash=API_HASH)

async def print_message():
    message = await client.get_messages('TelethonSnippets', ids=3)
    print("MESSAGE:", end="\n-------\n")
    print(message.text)

with client:
    client.loop.run_until_complete(print_message())