Telegram.BotAPI icon indicating copy to clipboard operation
Telegram.BotAPI copied to clipboard

Сommon way to get the СhatId of any type message

Open ffrip opened this issue 1 year ago • 1 comments

if (update.Type == UpdateType.Message)
    chatId = update.Message.Chat.Id;

but how handle all other types?

if (update.Type != UpdateType.Message)
{
    chatId = ???;
    await bot.SendMessageAsync(chatId, "Message is not a message-type");
}

ffrip avatar Oct 17 '23 18:10 ffrip

Hi, it depends. In most cases you will have a copy of the message which you can use to obtain the chat id For example, you can get the message from a callback query>

if (update.Type == UpdateType.CallbackQuery)
{
    var chatId = update.CallbackQuery.Message?.Chat.Id;
    if (chatId != null)
    {
        await bot.SendMessage(chatId, "Yeah");
    }
    await bot.AnswerCallbackQueryAsync(update.CallbackQuery.Id);
}

But be careful, the message may not always be available. Try taking a look at the official documentation to see what properties you could use to get the Chat Id.

Eptagone avatar Oct 18 '23 04:10 Eptagone