Skip to content

Commit 4cc8c46

Browse files
matvievskyАлексей Матвиевский
authored andcommitted
feat: Managed Bots support (Bot API 9.6)
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.
1 parent 4126fa6 commit 4cc8c46

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

managed_bots.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tgbotapi
2+
3+
import (
4+
"encoding/json"
5+
"strconv"
6+
)
7+
8+
// GetManagedBotToken retrieves the current token of a bot managed by this bot
9+
// (Bot API 9.6+, Managed Bots). userID is the managed bot's own id (bots are
10+
// Users in the Bot API's type system).
11+
func (bot *BotAPI) GetManagedBotToken(userID int64) (string, error) {
12+
params := Params{"user_id": strconv.FormatInt(userID, 10)}
13+
14+
resp, err := bot.MakeRequest("getManagedBotToken", params)
15+
if err != nil {
16+
return "", err
17+
}
18+
19+
var token string
20+
err = json.Unmarshal(resp.Result, &token)
21+
return token, err
22+
}
23+
24+
// ReplaceManagedBotToken rotates the token of a bot managed by this bot,
25+
// invalidating the previous one (Bot API 9.6+, Managed Bots).
26+
func (bot *BotAPI) ReplaceManagedBotToken(userID int64) (string, error) {
27+
params := Params{"user_id": strconv.FormatInt(userID, 10)}
28+
29+
resp, err := bot.MakeRequest("replaceManagedBotToken", params)
30+
if err != nil {
31+
return "", err
32+
}
33+
34+
var token string
35+
err = json.Unmarshal(resp.Result, &token)
36+
return token, err
37+
}

types.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ type Update struct {
114114
//
115115
// optional
116116
ChatJoinRequest *ChatJoinRequest `json:"chat_join_request,omitempty"`
117+
// ManagedBot is the creation, token update, or owner update of a bot
118+
// managed by the current bot (Bot API 9.6+, Managed Bots).
119+
//
120+
// optional
121+
ManagedBot *ManagedBotUpdated `json:"managed_bot,omitempty"`
117122
}
118123

119124
// SentFrom returns the user who sent an update. Can be nil, if Telegram did not provide information
@@ -217,6 +222,11 @@ type User struct {
217222
//
218223
// optional
219224
SupportsInlineQueries bool `json:"supports_inline_queries,omitempty"`
225+
// CanManageBots is true, if other bots can be created to be controlled
226+
// by this bot (Bot API 9.6+, Managed Bots). Returned only in getMe.
227+
//
228+
// optional
229+
CanManageBots bool `json:"can_manage_bots,omitempty"`
220230
}
221231

222232
// String displays a simple text version of a user.
@@ -563,6 +573,11 @@ type Message struct {
563573
//
564574
// optional
565575
MessageAutoDeleteTimerChanged *MessageAutoDeleteTimerChanged `json:"message_auto_delete_timer_changed,omitempty"`
576+
// ManagedBotCreated is a service message: a bot was created to be
577+
// managed by the current bot (Bot API 9.6+, Managed Bots).
578+
//
579+
// optional
580+
ManagedBotCreated *ManagedBotCreated `json:"managed_bot_created,omitempty"`
566581
// MigrateToChatID is the group has been migrated to a supergroup with the specified identifier.
567582
// This number may be greater than 32 bits and some programming languages
568583
// may have difficulty/silent defects in interpreting it.
@@ -1181,6 +1196,22 @@ type MessageAutoDeleteTimerChanged struct {
11811196
MessageAutoDeleteTime int `json:"message_auto_delete_time"`
11821197
}
11831198

1199+
// ManagedBotCreated contains information about a bot that was created to be
1200+
// managed by the current bot (Bot API 9.6+, Managed Bots).
1201+
type ManagedBotCreated struct {
1202+
// Bot is the newly created managed bot.
1203+
Bot User `json:"bot"`
1204+
}
1205+
1206+
// ManagedBotUpdated represents the creation, token update, or owner update of
1207+
// a bot managed by the current bot (Bot API 9.6+, Managed Bots).
1208+
type ManagedBotUpdated struct {
1209+
// User who owns the managed bot.
1210+
User User `json:"user"`
1211+
// Bot is the managed bot affected by this update.
1212+
Bot User `json:"bot"`
1213+
}
1214+
11841215
// VideoChatScheduled represents a service message about a voice chat scheduled
11851216
// in the chat.
11861217
type VideoChatScheduled struct {

0 commit comments

Comments
 (0)