Implemented updating inline keyboards.

This commit is contained in:
Andrey Parhomenko 2023-09-29 14:53:52 +03:00
parent 47b7c59469
commit b39e63cbaa
2 changed files with 86 additions and 44 deletions

View file

@ -60,21 +60,6 @@ var (
backButton, backButton,
) )
incDecKeyboard = tg.NewKeyboard().Row(
tg.NewButton("+").ActionFunc(func(c *tg.Context) {
d := ExtractSessionData(c)
d.Counter++
c.Sendf("%d", d.Counter)
}),
tg.NewButton("-").ActionFunc(func(c *tg.Context) {
d := ExtractSessionData(c)
d.Counter--
c.Sendf("%d", d.Counter)
}),
).Row(
backButton,
)
sendLocationKeyboard = tg.NewKeyboard().Row( sendLocationKeyboard = tg.NewKeyboard().Row(
tg.NewButton("Send location"). tg.NewButton("Send location").
WithSendLocation(true). WithSendLocation(true).
@ -101,32 +86,34 @@ WithInitFunc(func(c *tg.Context) {
c.Session.Data = &SessionData{} c.Session.Data = &SessionData{}
}).WithRootNode(tg.NewRootNode( }).WithRootNode(tg.NewRootNode(
// The "/" widget. // The "/" widget.
tg.RenderFunc(func(c *tg.Context) tg.UI {return tg.UI { tg.RenderFunc(func(c *tg.Context) tg.UI {
tg.NewMessage(fmt.Sprintf( return tg.UI {
fmt.Sprint( tg.NewMessage(fmt.Sprintf(
"Hello, %s!\n", fmt.Sprint(
"The testing bot started!\n", "Hello, %s!\n",
"You can see the basics of usage in the ", "The testing bot started!\n",
"cmd/test/main.go file!", "You can see the basics of usage in the ",
"cmd/test/main.go file!",
),
c.SentFrom().UserName,
)).Inline(
tg.NewKeyboard().Row(
tg.NewButton("GoT Github page").
WithUrl("https://github.com/mojosa-software/got"),
).Inline(),
), ),
c.SentFrom().UserName,
)).Inline(
tg.NewKeyboard().Row(
tg.NewButton("GoT Github page").
WithUrl("https://github.com/mojosa-software/got"),
).Inline(),
),
tg.NewMessage("Choose your interest").Reply( tg.NewMessage("Choose your interest").Reply(
tg.NewKeyboard().Row( tg.NewKeyboard().Row(
tg.NewButton("Inc/Dec").Go("/inc-dec"), tg.NewButton("Inc/Dec").Go("/inc-dec"),
).Row( ).Row(
tg.NewButton("Mutate messages").Go("/mutate-messages"), tg.NewButton("Mutate messages").Go("/mutate-messages"),
).Row( ).Row(
tg.NewButton("Send location").Go("/send-location"), tg.NewButton("Send location").Go("/send-location"),
).Reply(), ).Reply(),
), ),
}}), }
}),
tg.NewNode( tg.NewNode(
"mutate-messages", tg.RenderFunc(func(c *tg.Context) tg.UI { "mutate-messages", tg.RenderFunc(func(c *tg.Context) tg.UI {
@ -171,13 +158,58 @@ WithInitFunc(func(c *tg.Context) {
tg.NewNode( tg.NewNode(
"inc-dec", tg.RenderFunc(func(c *tg.Context) tg.UI { "inc-dec", tg.RenderFunc(func(c *tg.Context) tg.UI {
var (
kbd *tg.InlineCompo
inline, std, onlyInc, onlyDec *tg.Inline
)
d := ExtractSessionData(c) d := ExtractSessionData(c)
format := "Press the buttons to increment and decrement.\n" +
"Current counter value = %d"
incBtn := tg.NewButton("+").ActionFunc(func(c *tg.Context) {
d.Counter++
kbd.Text = fmt.Sprintf(format, d.Counter)
if d.Counter == 5 {
kbd.Inline = onlyDec
} else {
kbd.Inline = std
}
kbd.Update(c)
})
decBtn := tg.NewButton("-").ActionFunc(func(c *tg.Context) {
d.Counter--
kbd.Text = fmt.Sprintf(format, d.Counter)
if d.Counter == -5 {
kbd.Inline = onlyInc
} else {
kbd.Inline = std
}
kbd.Update(c)
//c.Sendf("%d", d.Counter)
})
onlyInc = tg.NewKeyboard().Row(incBtn).Inline()
onlyDec = tg.NewKeyboard().Row(decBtn).Inline()
std = tg.NewKeyboard().Row(incBtn, decBtn).Inline()
if d.Counter == 5 {
inline = onlyDec
} else if d.Counter == -5 {
inline = onlyInc
} else {
inline = std
}
kbd = tg.NewMessage(
fmt.Sprintf(format, d.Counter),
).Inline(inline)
return tg.UI{ return tg.UI{
tg.NewMessage(fmt.Sprintf( kbd,
"Press the buttons to increment and decrement.\n" + tg.NewMessage("").Reply(
"Current counter value = %d", d.Counter, backKeyboard.Reply(),
)).Reply(
incDecKeyboard.Reply(),
), ),
} }
}), }),

View file

@ -40,6 +40,16 @@ func (compo *InlineCompo) SendConfig(
return sendConfig return sendConfig
} }
func (compo *InlineCompo) Update(c *Context) {
edit := tgbotapi.NewEditMessageTextAndMarkup(
c.Session.Id.ToApi(),
compo.Message.MessageID,
compo.Text,
compo.Inline.ToApi(),
)
c.Bot.Api.Send(edit)
}
// Implementing the Filterer interface. // Implementing the Filterer interface.
func (compo *InlineCompo) Filter(u *Update) bool { func (compo *InlineCompo) Filter(u *Update) bool {
if compo == nil || u.CallbackQuery == nil { if compo == nil || u.CallbackQuery == nil {