Skip to content

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

Closed

Description

@ffrip
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");
}

Activity

  1. Eptagone commented on Oct 18, 2023

    @Eptagone
    Owner

    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.

  2. Repository owner locked and limited conversation to collaborators on Mar 22, 2025
  3. converted this issue into a discussion #53 on Mar 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions