From 4cc8c4655ee713539c46c84b8597b49f08552a33 Mon Sep 17 00:00:00 2001 From: Alexey Matvievsky Date: Wed, 1 Jul 2026 21:50:01 +0300 Subject: [PATCH] feat: Managed Bots support (Bot API 9.6) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the types and methods for Telegram's Managed Bots feature, introduced in Bot API 9.6 (April 2026): a bot with can_manage_bots can create and control other bots via its own Bot API token, without BotFather's manual /newbot flow or a separate credential per bot. - User.CanManageBots (getMe) - Update.ManagedBot / ManagedBotUpdated (creation, token update, or owner update of a managed bot) - Message.ManagedBotCreated / ManagedBotCreated (service message for bot creation) - BotAPI.GetManagedBotToken(userID) — fetch a managed bot's current token - BotAPI.ReplaceManagedBotToken(userID) — rotate a managed bot's token Doesn't add KeyboardButtonRequestManagedBot or savePreparedKeyboardButton (the Mini App integration half of the same API bump) — happy to extend this PR or follow up separately if there's interest. --- managed_bots.go | 37 +++++++++++++++++++++++++++++++++++++ types.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 managed_bots.go diff --git a/managed_bots.go b/managed_bots.go new file mode 100644 index 00000000..49eeda3b --- /dev/null +++ b/managed_bots.go @@ -0,0 +1,37 @@ +package tgbotapi + +import ( + "encoding/json" + "strconv" +) + +// GetManagedBotToken retrieves the current token of a bot managed by this bot +// (Bot API 9.6+, Managed Bots). userID is the managed bot's own id (bots are +// Users in the Bot API's type system). +func (bot *BotAPI) GetManagedBotToken(userID int64) (string, error) { + params := Params{"user_id": strconv.FormatInt(userID, 10)} + + resp, err := bot.MakeRequest("getManagedBotToken", params) + if err != nil { + return "", err + } + + var token string + err = json.Unmarshal(resp.Result, &token) + return token, err +} + +// ReplaceManagedBotToken rotates the token of a bot managed by this bot, +// invalidating the previous one (Bot API 9.6+, Managed Bots). +func (bot *BotAPI) ReplaceManagedBotToken(userID int64) (string, error) { + params := Params{"user_id": strconv.FormatInt(userID, 10)} + + resp, err := bot.MakeRequest("replaceManagedBotToken", params) + if err != nil { + return "", err + } + + var token string + err = json.Unmarshal(resp.Result, &token) + return token, err +} diff --git a/types.go b/types.go index 36c174b8..db539cda 100644 --- a/types.go +++ b/types.go @@ -114,6 +114,11 @@ type Update struct { // // optional ChatJoinRequest *ChatJoinRequest `json:"chat_join_request,omitempty"` + // ManagedBot is the creation, token update, or owner update of a bot + // managed by the current bot (Bot API 9.6+, Managed Bots). + // + // optional + ManagedBot *ManagedBotUpdated `json:"managed_bot,omitempty"` } // SentFrom returns the user who sent an update. Can be nil, if Telegram did not provide information @@ -217,6 +222,11 @@ type User struct { // // optional SupportsInlineQueries bool `json:"supports_inline_queries,omitempty"` + // CanManageBots is true, if other bots can be created to be controlled + // by this bot (Bot API 9.6+, Managed Bots). Returned only in getMe. + // + // optional + CanManageBots bool `json:"can_manage_bots,omitempty"` } // String displays a simple text version of a user. @@ -563,6 +573,11 @@ type Message struct { // // optional MessageAutoDeleteTimerChanged *MessageAutoDeleteTimerChanged `json:"message_auto_delete_timer_changed,omitempty"` + // ManagedBotCreated is a service message: a bot was created to be + // managed by the current bot (Bot API 9.6+, Managed Bots). + // + // optional + ManagedBotCreated *ManagedBotCreated `json:"managed_bot_created,omitempty"` // MigrateToChatID is the group has been migrated to a supergroup with the specified identifier. // This number may be greater than 32 bits and some programming languages // may have difficulty/silent defects in interpreting it. @@ -1181,6 +1196,22 @@ type MessageAutoDeleteTimerChanged struct { MessageAutoDeleteTime int `json:"message_auto_delete_time"` } +// ManagedBotCreated contains information about a bot that was created to be +// managed by the current bot (Bot API 9.6+, Managed Bots). +type ManagedBotCreated struct { + // Bot is the newly created managed bot. + Bot User `json:"bot"` +} + +// ManagedBotUpdated represents the creation, token update, or owner update of +// a bot managed by the current bot (Bot API 9.6+, Managed Bots). +type ManagedBotUpdated struct { + // User who owns the managed bot. + User User `json:"user"` + // Bot is the managed bot affected by this update. + Bot User `json:"bot"` +} + // VideoChatScheduled represents a service message about a voice chat scheduled // in the chat. type VideoChatScheduled struct {