Search code examples
telegramtelethonmessageid

Accessing Telegram Message by its id


I'm using Telethon Library to get messages, filter them, and a bit later if some conditions were met, reply to them with a specific answer. The question is, can I get info about a message (most importantly its text), just by having its id?


Solution

  • You can use client.get messages for that.

    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.

    Here is a minimal working example to get you the idea:

    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())