dialog

package
v0.0.0-...-0bec941 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Dialog

dialog.gif

Dialog component allows you to create a simple text dialogs. For example, you can use it to display a some menu to the user.

Use dialog.Inline() option for change message text, instead of send new messages.

While you create node keyboard, you can to define NodeID or URL.

Getting Started

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")
}

Options

See in options.go file

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Button

type Button struct {
	Name    string
	Text    string
	Goto    string
	URL     string
	Handler OnSelect
}

type Dialog

type Dialog struct {
	// contains filtered or unexported fields
}

func New

func New(b *bot.Bot, nodes []Node, opts ...Option) *Dialog

func (*Dialog) Prefix

func (d *Dialog) Prefix() string

Prefix returns the prefix of the widget

func (*Dialog) Show

func (d *Dialog) Show(ctx context.Context, b *bot.Bot, chatID any, Goto string) (*models.Message, error)

type Node

type Node struct {
	ID       string
	Text     string
	Keyboard [][]Button
}

type OnErrorHandler

type OnErrorHandler func(err error)

type OnSelect

type OnSelect func(ctx context.Context, bot *bot.Bot, update *models.Update)

type Option

type Option func(d *Dialog)

func Inline

func Inline() Option

func WithPrefix

func WithPrefix(s string) Option

WithPrefix is a keyboard option that sets a prefix for the widget

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL