Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Message ID of the messages of a telegram channel

I want to forward last 5 messages of a telegram channel that the user asks and my bot is an administrator in the channel. I know that i should forward the messages by something like the codes below, But how to find the messages IDs dynamically?

  int chatId = 123456789;        // Not real chat ID.
  string channelId = "@abcdefgh" // Not real channel ID.
  int msgId = ?;                 // How to find 'msgId' (My question)
  await bot.ForwardMessageAsync(chatId, ChannelId, msgId);
like image 445
Javad-M Avatar asked Nov 22 '25 10:11

Javad-M


1 Answers

In telegram messageId is the number of message in the chat, first message has MessageId 1, So use save last MessageId and use it to forword messages like this:

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

// Defining lastMessageId to use it
private int lastMessageId = 1;

public static void Main(string[] args)
{
    Bot.OnMessage += Bot_OnMessage;
    Bot.StartReceiving();
}

private static void Bot_OnMessage(MessageEventArgs e)
{
    // Save last message id to use it
    lastMessageId = e.Message.MessageId;
}

// Method that you want:
private static async void ForwordLastFiveMessages()
{
    for (int i = 0; i < 5; i++)
    {
        // Use lastMessageId to forword messages
        await Bot.ForwordMessageAsync(123456789, "@abcefgh", lastMessageId - i);
    }
}
like image 147
Muaath Avatar answered Nov 25 '25 00:11

Muaath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!