inline

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: 9 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

Inline Keyboard

inline_keyboard.png

Getting Started

package main

import (
	"context"
	"net/url"
	"os"
	"os/signal"

	"github.com/go-telegram/bot"
	"github.com/go-telegram/bot/models"
	"github.com/go-telegram/ui/keyboard/inline"
)

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

func defaultHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
	u := url.URL{Scheme: "https", Host: "example.com", Path: "/path"}

	kb := inline.New(b).
		Row().
		Button("Row 1, Btn 1", []byte("1-1"), onInlineKeyboardSelect).
		Button("Row 1, Btn 2", []byte("1-2"), onInlineKeyboardSelect).
		Row().
		Button("Row 2, Btn 1", []byte("2-1"), onInlineKeyboardSelect).
		Button("Row 2, Btn 2", []byte("2-2"), onInlineKeyboardSelect).
		Button("Row 2, Btn 3", []byte("2-3"), onInlineKeyboardSelect).
		Row().
		Button("Row 3, Btn 1", []byte("3-1"), onInlineKeyboardSelect).
		Button("Row 3, Btn 2", []byte("3-2"), onInlineKeyboardSelect).
		Button("Row 3, Btn 3", []byte("3-3"), onInlineKeyboardSelect).
		Button("Row 3, Btn 4", []byte("3-4"), onInlineKeyboardSelect).
		Row().
		ButtonURL("Link", u).
		Row().
		Button("Cancel", []byte("cancel"), onInlineKeyboardSelect)

	b.SendMessage(ctx, &bot.SendMessageParams{
		ChatID:      update.Message.Chat.ID,
		Text:        "Select the variant",
		ReplyMarkup: kb,
	})
}

func onInlineKeyboardSelect(ctx context.Context, b *bot.Bot, mes models.MaybeInaccessibleMessage, data []byte) {
	b.SendMessage(ctx, &bot.SendMessageParams{
		ChatID: mes.Message.Chat.ID,
		Text:   "You selected: " + string(data),
	})
}

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 Keyboard

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

func New

func New(b *bot.Bot, opts ...Option) *Keyboard

func (*Keyboard) Button

func (kb *Keyboard) Button(text string, data []byte, handler OnSelect) *Keyboard

func (*Keyboard) ButtonURL

func (kb *Keyboard) ButtonURL(text string, u url.URL) *Keyboard

func (*Keyboard) MarshalJSON

func (kb *Keyboard) MarshalJSON() ([]byte, error)

func (*Keyboard) Prefix

func (kb *Keyboard) Prefix() string

Prefix returns the prefix of the widget

func (*Keyboard) Row

func (kb *Keyboard) Row() *Keyboard

type OnErrorHandler

type OnErrorHandler func(err error)

type OnSelect

type OnSelect func(ctx context.Context, bot *bot.Bot, mes models.MaybeInaccessibleMessage, data []byte)

type Option

type Option func(kb *Keyboard)

func NoDeleteAfterClick

func NoDeleteAfterClick() Option

NoDeleteAfterClick is a keyboard option that prevents the hide keyboard after click.

func OnError

func OnError(f func(err error)) Option

OnError is a keyboard option that sets a callback function to be called when an error occurs.

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