获取电报频道消息的消息ID

时间:2020-08-09 11:09:27

标签: c# winforms telegram-bot

我想转发用户询问的电报频道的最后5条消息,而我的机器人是该频道的管理员。我知道我应该通过以下代码转发邮件,但是如何动态查找邮件ID?

  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);

1 个答案:

答案 0 :(得分:0)

在电报messageId中,是聊天中的消息数,第一条消息的MessageId为1,因此请使用保存最后的MessageId并用它来编写如下消息:

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);
    }
}