Skip to content

Add custom callbackhandler for dialog buttons #11

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions dialog/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ import (
type OnErrorHandler func(err error)

type Dialog struct {
data []string
prefix string
onError OnErrorHandler
nodes []Node
inline bool
data []string
prefix string
nodePrefix string
callbackPrefix string
onError OnErrorHandler
nodes []Node
inline bool

callbackHandlerID string
}

func New(nodes []Node, opts ...Option) *Dialog {
p := &Dialog{
prefix: bot.RandomString(16),
onError: defaultOnError,
nodes: nodes,
prefix: bot.RandomString(12),
callbackPrefix: bot.RandomString(4),
nodePrefix: bot.RandomString(4),
onError: defaultOnError,
nodes: nodes,
}

for _, opt := range opts {
Expand All @@ -50,7 +54,7 @@ func (d *Dialog) showNode(ctx context.Context, b *bot.Bot, chatID any, node Node
ChatID: chatID,
Text: node.Text,
ParseMode: models.ParseModeMarkdown,
ReplyMarkup: node.buildKB(d.prefix),
ReplyMarkup: node.buildKB(d.prefix, d.nodePrefix, d.callbackPrefix),
}

return b.SendMessage(ctx, params)
Expand All @@ -76,7 +80,19 @@ func (d *Dialog) callback(ctx context.Context, b *bot.Bot, update *models.Update
d.onError(fmt.Errorf("failed to answer callback query"))
}

nodeID := strings.TrimPrefix(update.CallbackQuery.Data, d.prefix)
btnID, isCustomCallback := strings.CutPrefix(update.CallbackQuery.Data, d.prefix+d.callbackPrefix)
if isCustomCallback {
btn, ok := d.findButton(btnID)
if !ok {
d.onError(fmt.Errorf("failed to find button with id %s", btnID))
return
}
update.CallbackQuery.Data = btn.CallbackData
btn.CallbackHandler(ctx, b, update)
return
}

nodeID := strings.TrimPrefix(update.CallbackQuery.Data, d.prefix+d.nodePrefix)
node, ok := d.findNode(nodeID)
if !ok {
d.onError(fmt.Errorf("failed to find node with id %s", nodeID))
Expand All @@ -89,7 +105,7 @@ func (d *Dialog) callback(ctx context.Context, b *bot.Bot, update *models.Update
MessageID: update.CallbackQuery.Message.Message.ID,
Text: node.Text,
ParseMode: models.ParseModeMarkdown,
ReplyMarkup: node.buildKB(d.prefix),
ReplyMarkup: node.buildKB(d.prefix, d.nodePrefix, d.callbackPrefix),
})
if errEdit != nil {
d.onError(errEdit)
Expand All @@ -101,7 +117,7 @@ func (d *Dialog) callback(ctx context.Context, b *bot.Bot, update *models.Update
ChatID: update.CallbackQuery.Message.Message.Chat.ID,
Text: node.Text,
ParseMode: models.ParseModeMarkdown,
ReplyMarkup: node.buildKB(d.prefix),
ReplyMarkup: node.buildKB(d.prefix, d.nodePrefix, d.callbackPrefix),
})
if errSend != nil {
d.onError(errSend)
Expand All @@ -117,3 +133,16 @@ func (d *Dialog) findNode(id string) (Node, bool) {

return Node{}, false
}

func (d *Dialog) findButton(ID string) (Button, bool) {
for _, node := range d.nodes {
for _, row := range node.Keyboard {
for _, btn := range row {
if btn.ID == ID {
return btn, true
}
}
}
}
return Button{}, false
}
22 changes: 15 additions & 7 deletions dialog/node.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package dialog

import (
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)

type Button struct {
Text string
NodeID string
URL string
ID string
Text string
NodeID string
URL string
CallbackHandler bot.HandlerFunc
CallbackData string
}

type Node struct {
Expand All @@ -16,7 +20,7 @@ type Node struct {
Keyboard [][]Button
}

func (n Node) buildKB(prefix string) models.ReplyMarkup {
func (n Node) buildKB(prefix, nodePrefix, callbackPrefix string) models.ReplyMarkup {
if len(n.Keyboard) == 0 {
return nil
}
Expand All @@ -29,10 +33,14 @@ func (n Node) buildKB(prefix string) models.ReplyMarkup {
b := models.InlineKeyboardButton{
Text: btn.Text,
}
if btn.URL != "" {
switch {
case btn.URL != "":
b.URL = btn.URL
} else {
b.CallbackData = prefix + btn.NodeID
case btn.CallbackHandler != nil:
b.CallbackData = prefix + callbackPrefix + btn.ID
default:
b.CallbackData = prefix + nodePrefix + btn.NodeID

}
kbRow = append(kbRow, b)
}
Expand Down
14 changes: 14 additions & 0 deletions dialog/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ func WithPrefix(s string) Option {
w.prefix = s
}
}

// WithCallbackPrefix is a keyboard option that sets a prefix for the widget which uses CallbackHandler
func WithCallbackPrefix(s string) Option {
return func(w *Dialog) {
w.callbackPrefix = s
}
}

// WithNodePrefix is a keyboard option that sets a prefix for the widget which uses NodeID
func WithNodePrefix(s string) Option {
return func(w *Dialog) {
w.nodePrefix = s
}
}
12 changes: 10 additions & 2 deletions examples/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

var (
dialogNodes = []dialog.Node{
{ID: "start", Text: "Start Node", Keyboard: [][]dialog.Button{{{Text: "Go to node 2", NodeID: "2"}, {Text: "Go to node 3", NodeID: "3"}}, {{Text: "Go Telegram UI", URL: "https://github.com/go-telegram/ui"}}}},
{ID: "start", Text: "Start Node", Keyboard: [][]dialog.Button{{{Text: "Go to node 2", NodeID: "2"}, {Text: "Go to node 3", NodeID: "3"}}, {{Text: "Go Telegram UI", URL: "https://github.com/sinasadeghi83/go-telegram-bot-ui"}}}},
{ID: "2", Text: "node 2 without keyboard"},
{ID: "3", Text: "node 3", Keyboard: [][]dialog.Button{{{Text: "Go to start", NodeID: "start"}, {Text: "Go to node 4", NodeID: "4"}}}},
{ID: "4", Text: "node 4", Keyboard: [][]dialog.Button{{{Text: "Back to 3", NodeID: "3"}}}},
{ID: "4", Text: "node 4", Keyboard: [][]dialog.Button{{{Text: "Back to 3", NodeID: "3"}, {Text: "Node 5", NodeID: "5"}}}},
{ID: "5", Text: "node 5", Keyboard: [][]dialog.Button{{{ID: "1", Text: "Custom Handler", CallbackHandler: handlerDialogCustom, CallbackData: "You choose custom handler"}}}},
}
)

Expand All @@ -28,3 +29,10 @@ func handlerDialogInline(ctx context.Context, b *bot.Bot, update *models.Update)

p.Show(ctx, b, update.Message.Chat.ID, "start")
}

func handlerDialogCustom(ctx context.Context, b *bot.Bot, update *models.Update) {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.CallbackQuery.Message.Message.ID,
Text: update.CallbackQuery.Data,
})
}