How to respond to bot's last message in Telegram

I’m trying to make my Telegram bot reply to its own messages. I can reply to user messages, but I’m stuck on replying to the bot’s last message.

Here’s what I’ve got so far:

var bot = new TelegramBotClient("myToken");

bot.OnMessage += async (s, e) =>
{
    await bot.SendTextMessageAsync(e.Message.Chat.Id, "Hey there!");
    
    // This replies to the user's message, but I want to reply to the bot's message
    await bot.SendTextMessageAsync(e.Message.Chat.Id, "How's it going?", replyToMessageId: e.Message.MessageId);
};

bot.StartReceiving();

Do I need to somehow get the bot’s message ID? If so, how can I do that? Any help would be great!

I’ve faced this issue before while working on a Telegram bot project. The key is to capture the MessageId of the bot’s sent message. Here’s what worked for me:

When you call SendTextMessageAsync, it actually returns a Message object. You can store this and use its MessageId property for your reply.

var botMessage = await bot.SendTextMessageAsync(e.Message.Chat.Id, “Hey there!”);
await bot.SendTextMessageAsync(e.Message.Chat.Id, “How’s it going?”, replyToMessageId: botMessage.MessageId);

This way, your bot will reply to its own message. Just remember to handle potential exceptions, as network issues could cause the first message to fail sending. Hope this helps!

Yo, I got a trick for ya! Try this:

var botMsg = await bot.SendTextMessageAsync(e.Message.Chat.Id, “Sup?”);
await bot.SendTextMessageAsync(e.Message.Chat.Id, “What’s crackin’?”, replyToMessageId: botMsg.MessageId);

This way ur bot’ll reply to itself. Works like a charm, trust me! :sunglasses:

To reply to the bot’s last message, you need to capture the message ID of the bot’s sent message. Here’s how you can modify your code:

var bot = new TelegramBotClient("myToken");

bot.OnMessage += async (s, e) =>
{
    var sentMessage = await bot.SendTextMessageAsync(e.Message.Chat.Id, "Hey there!");
    
    await bot.SendTextMessageAsync(e.Message.Chat.Id, "How's it going?", replyToMessageId: sentMessage.MessageId);
};

bot.StartReceiving();

The SendTextMessageAsync method returns a Message object, which includes the MessageId. Store this in a variable and use it in the subsequent SendTextMessageAsync call. This way, your bot will reply to its own message.