To use the dialog component, follow these steps:
- Define your dialog structure as a slice of
dialog.Node objects. Each node must have a unique ID. - In your bot's handler, initialize the dialog using
dialog.New(nodes). - Trigger the dialog by calling
d.Show(ctx, b, chatID, startNodeID).
Note: chatID must be passed as a string.
package main
import (
"context"
"os"
"os/signal"
"strconv"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/go-telegram/ui/dialog"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
telegramBotToken := os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN")
opts := []bot.Option{
bot.WithDefaultHandler(defaultHandler),
}
b := bot.New(telegramBotToken, opts...)
b.Start(ctx)
}
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: "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"}}}},
},
)
func defaultHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
d := dialog.New(dialogNodes)
d.Show(ctx, b, strconv.Itoa(update.Message.Chat.ID), "start")
}