Consider the example of the Message object sent by the user to the bot.
Each message is an object that contains some fields, in particular, we need the chat . This field, in turn, is also an object and also contains fields, including the id we need. You can receive it in different ways, depending on how you work with the API.
Example for Go:
In this example, the bot writes a chat identifier and message text to the log (taken from the example and slightly modified)
package main import ( "log" "gopkg.in/telegram-bot-api.v4" ) func main() { bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") if err != nil { log.Panic(err) } bot.Debug = true u := tgbotapi.NewUpdate(0) u.Timeout = 60 updates, err := bot.GetUpdatesChan(u) for update := range updates { if update.Message == nil { continue } log.Printf("[%s] %s", update.Message.Chat.ID, update.Message.Text) } }
A regular message can be a text message sent by a file, sticker, photo, video or audio. There are also other types of objects received by the bot from the user - they are listed as fields of the Update object. Depending on the type of object, the logic for obtaining a chat ID may vary slightly.