Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

telegram

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'.

like image 453
Samat Avatar asked Sep 08 '17 13:09

Samat


People also ask

What is the difference between private and public channel in Telegram?

Public channels have a username. Anyone can find them in Telegram search and join. Private channels are closed societies – you need to be added by the owner or get an invite link to join.

How can I retrieve Telegram bot messages?

Follow the link to your bot by clicking the link that looks like t.me/{yourBotUsername}. This is where you will receive messages.

Can a telegram BOT send message to user?

In order to send a message to "@Username", you will need them to start your bot, and then store the username with the user_id. Then, you can input the username to find the correct user_id each time you want to send them a message.


1 Answers

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 on messages.getHistory parameters:

    :param peer:        The channel from whom to retrieve the message history
    :param limit:       Number of messages to be retrieved
    :param offset_date: Offset date (messages *previous* to this date will be retrieved)
    :param offset_id:   Offset message ID (only messages *previous* to the given ID will be retrieved)
    :param max_id:      All the messages with a higher (newer) ID or equal to this will be excluded
    :param min_id:      All the messages with a lower (older) ID or equal to this will be excluded
    :param add_offset:  Additional message offset (all of the specified offsets + this offset = older messages)
like image 117
Ali Hashemi Avatar answered Oct 07 '22 03:10

Ali Hashemi