Search code examples
telegram

Telegram API: How do I get messages from a public channel, which I am not participant of?


I am able to successfully retrieve messages from a channel via channels.getMessages request, once I know their message IDs. I find channel id by contacts.search, by the way.

At the moment, mesage IDs are consequtive integers, so getting max_id would solve the issue.

I am sure it possible, since official clients do this (view a channel without joining it). I will try to find out how official desktop app does this by reading its sources, but any help will be much appreciated.

I need this because am writing a simple public telegram channel -> rss/web interface.

Please do not confuse Telegram Client API with Telegram Bot API. Bot API allows to receive 'push' messages on new messages, but no 'reading historical logs'.


Solution

  • Here are the steps you have to do in order to get messages from a channel you're not joined:

    1. Resolve the username into ID and access_hash with contacts.resolveUsername
    2. Invoke messages.getHistory to get your desired messages

    Here is a short description of messages.getHistory parameters:

    peer:        The channel from whom to retrieve the message history
    limit:       Number of messages to be retrieved
    offset_date: Offset date (messages *previous* to this date will be retrieved)
    offset_id:   Offset message ID (only messages *previous* to the given ID will be retrieved)
    max_id:      All the messages with a higher (newer) ID or equal to this will be excluded
    min_id:      All the messages with a lower (older) ID or equal to this will be excluded
    add_offset:  Additional message offset (all of the specified offsets + this offset = older messages)