inline

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: MIT Imports: 8 Imported by: 2

README

Inline Keyboard

inline_keyboard.png

Getting Started

package main

import (
	"context"
	"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) {
	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().
		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) MarshalJSON

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

func (*Keyboard) Prefix added in v0.3.1

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 added in v0.3.1

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