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);
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With