How can I get messages from a Telegram channel with the Telegram API
Asked Answered
L

3

12

How can I access to a Telegram channel messages with a bot registered as channel admin?

I am trying to get all the messages from Telegram channel and display them in an ASP.NET webpage (c#)

I am able to get updates when new message sent directly to the bot:

var json = wc.DownloadString(" https://api.telegram.org/bot<token>/getUpdates");

but its not working for the channel.

Laughton answered 30/12, 2015 at 8:50 Comment(3)
do you know how to get messages from telegram bot that were sent by other people using /start command? Thanks.Herzen
The new @ifttt telegram bot can help.Bayless
What you called Telegram API really is Telegram Bot API.Castleberry
L
2

unfortunately it's not possible yet.(they may come up with something in future) as admin you can just send messages to channel.

Libradalibrarian answered 29/2, 2016 at 13:12 Comment(2)
sorry. not yet.Libradalibrarian
No, you can receive from channels in updates, see thisArum
A
1

You can receive channel posts and channel post editing.

But you won't receive it in OnMessage event, you can receive it from OnUpdate as Message object like code below:

Note: The bot must be an administrator in the channel.

private static readonly TelegramBotClient Bot = new TelegramBotClient("my-real-token");

public static void Main(string[] args)
{
    Bot.StartReceive(UpdateType.ChannelPost, UpdateType.EditedChannelPost);
    Bot.OnUpdate += Bot_OnUpdate;
}
    
public static void OnUpdate(UpdateEventArgs e)
{
    if (e.Update.Type == UpdateType.ChannelPost)
    {
        Message post = e.Update.ChannelPost;
        //TODO: Store channel post
    }
    else if (e.Update.Type == UpdateType.EditedChannelPost)
    {
        Message editedPost = e.Update.EditedChannelPost;
        //TODO: Store edited channel post
    }
}
Arum answered 9/8, 2020 at 14:35 Comment(0)
C
0

The only way until now is as follows:

  1. Your bot be added to the channel by its administrator.
  2. You set the bot's privacy to disabled.

Step 1 helps your bot to join the channel so it see the messages.

Step 2 makes your bot listening to all messages of the channel.

Castleberry answered 20/4, 2017 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.