Skip to content

Commit 269209c

Browse files
ernadoclaude
andcommitted
docs(example): demonstrate inline_message_id in the inline bot
Attach a callback keyboard to the inline results so Telegram mints an inline_message_id, then surface that id from both the chosen-inline-result update and callback queries on the inline message (which carry an inline_message_id instead of a Message). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 1a778b5 commit 269209c

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

examples/inline/main.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
// bot's @username followed by a query in any chat and it offers article results
33
// echoing the query; picking one sends the text.
44
//
5+
// It also demonstrates inline_message_id: each result carries an inline
6+
// keyboard, so when a user picks a result Telegram assigns it an
7+
// inline_message_id. That id is surfaced both on the chosen-inline-result update
8+
// and on callback queries from the inline message's button — a bot can persist
9+
// it to edit the inline message later.
10+
//
511
// Run it with an MTProto app identity (https://my.telegram.org) and a BotFather
6-
// token (inline mode must be enabled for the bot via @BotFather):
12+
// token. Both inline mode and inline feedback (for chosen-inline-result updates)
13+
// must be enabled for the bot via @BotFather:
714
//
815
// APP_ID=12345 APP_HASH=abcdef BOT_TOKEN=123:abc go run ./examples/inline
916
package main
@@ -49,6 +56,12 @@ func main() {
4956
log.Fatal("Create bot", zap.Error(err))
5057
}
5158

59+
// A callback keyboard on the result makes Telegram mint an inline_message_id
60+
// for the sent message and lets the user interact with it afterwards.
61+
keyboard := botapi.InlineKeyboard(
62+
botapi.InlineRow(botapi.InlineKeyboardButton{Text: "🔁 Shout again", CallbackData: "shout"}),
63+
)
64+
5265
bot.OnInlineQuery(func(c *botapi.Context) error {
5366
q := strings.TrimSpace(c.Update.InlineQuery.Query)
5467
if q == "" {
@@ -63,6 +76,7 @@ func main() {
6376
InputMessageContent: &botapi.InputTextMessageContent{
6477
MessageText: strings.ToUpper(q),
6578
},
79+
ReplyMarkup: keyboard,
6680
},
6781
&botapi.InlineQueryResultArticle{
6882
ID: "echo",
@@ -71,12 +85,45 @@ func main() {
7185
InputMessageContent: &botapi.InputTextMessageContent{
7286
MessageText: q,
7387
},
88+
ReplyMarkup: keyboard,
7489
},
7590
}
7691

7792
return c.AnswerInline(results, botapi.WithInlineCacheTime(1))
7893
})
7994

95+
// Fires when a user picks one of the offered results. The inline_message_id
96+
// identifies the message that was sent into the user's chat; persist it to
97+
// edit the message later. Requires inline feedback enabled in @BotFather.
98+
bot.OnChosenInlineResult(func(c *botapi.Context) error {
99+
r := c.Update.ChosenInlineResult
100+
101+
log.Info("Inline result chosen",
102+
zap.String("result_id", r.ResultID),
103+
zap.String("query", r.Query),
104+
zap.String("inline_message_id", r.InlineMessageID),
105+
)
106+
107+
return nil
108+
})
109+
110+
// Callback queries from the inline message's button carry an
111+
// inline_message_id instead of a Message (the bot never sees the host chat).
112+
bot.OnCallbackQuery(func(c *botapi.Context) error {
113+
cq := c.Update.CallbackQuery
114+
if cq.InlineMessageID == "" {
115+
// Callback from a normal message (not an inline one).
116+
return c.AnswerCallback()
117+
}
118+
119+
log.Info("Callback on inline message",
120+
zap.String("data", cq.Data),
121+
zap.String("inline_message_id", cq.InlineMessageID),
122+
)
123+
124+
return c.AnswerCallback(botapi.WithCallbackText("Got it!"), botapi.WithCallbackAlert())
125+
})
126+
80127
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
81128
defer cancel()
82129

0 commit comments

Comments
 (0)