layout

package
v3.0.0-...-06218c9 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFunc

func AddFunc(key string, fn interface{})

AddFunc adds the given function to the template FuncMap. Note: to make it come into effect, always add functions before New().

func AddFuncs

func AddFuncs(fm template.FuncMap)

AddFuncs extends the template FuncMap with the given one. Note: to make it come into effect, always add functions before New().

Types

type Button

type Button = tele.Btn

Button is a shortcut for tele.Btn.

type Config

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

Config represents typed map interface related to the "config" section in layout.

func (*Config) Bool

func (c *Config) Bool(k string) bool

Float returns a field casted to the bool.

func (*Config) ChatID

func (c *Config) ChatID(k string) tele.ChatID

ChatID returns a field casted to the ChatID. The value must be an integer.

func (*Config) Duration

func (c *Config) Duration(k string) time.Duration

Duration returns a field casted to the time.Duration. Accepts number-represented duration or a string in 0nsuµmh format.

func (*Config) Each

func (c *Config) Each(k string, f func(int, *Config))

Each iterates over the array field. Use it only with map elements.

func (*Config) Float

func (c *Config) Float(k string) float64

Float returns a field casted to the float64.

func (*Config) Floats

func (c *Config) Floats(k string) []float64

Floats returns a field casted to the float slice.

func (*Config) Get

func (c *Config) Get(k string) *Config

Get returns child map field wrapped into Config. If the field isn't a map, returns nil.

func (*Config) Index

func (c *Config) Index(k string, i int) *Config

Index returns an i element from the array field, wrapped into Config. If the element isn't a map, returns nil. See also: Strings, Ints, Floats.

func (*Config) Int

func (c *Config) Int(k string) int

Int returns a field casted to the int.

func (*Config) Int64

func (c *Config) Int64(k string) int64

Int64 returns a field casted to the int64.

func (*Config) Ints

func (c *Config) Ints(k string) []int

Ints returns a field casted to the int slice.

func (*Config) Len

func (c *Config) Len(k string) int

Len returns the array field length.

func (*Config) String

func (c *Config) String(k string) string

String returns a field casted to the string.

func (*Config) Strings

func (c *Config) Strings(k string) []string

Strings returns a field casted to the string slice.

func (*Config) Unmarshal

func (c *Config) Unmarshal(v interface{}) error

Unmarshal parses the config into the out value. It's useful when you want to describe and to pre-define the fields in your custom configuration struct.

type Layout

type Layout struct {
	*Config
	// contains filtered or unexported fields
}

Layout provides an interface to interact with the layout, parsed from the config file and locales.

func New

func New(path string) (*Layout, error)

New reads and parses the given layout file.

func (*Layout) Button

func (lt *Layout) Button(c tele.Context, k string, args ...interface{}) *tele.Btn

Button returns a button, which locale is dependent on the context. The given optional argument will be passed to the template engine.

buttons:
  item:
    unique: item
    callback_data: {{.ID}}
    text: Item #{{.Number}}

Usage:

btns := make([]tele.Btn, len(items))
for i, item := range items {
	btns[i] = lt.Button(c, "item", struct {
		Number int
		Item   Item
	}{
		Number: i,
		Item:   item,
	})
}

m := b.NewMarkup()
m.Inline(m.Row(btns...))
// Your generated markup is ready.

func (*Layout) ButtonLocale

func (lt *Layout) ButtonLocale(locale, k string, args ...interface{}) *tele.Btn

ButtonLocale returns a localised button processed with standard template engine. See Button for more details.

func (*Layout) Callback

func (lt *Layout) Callback(k string) tele.CallbackEndpoint

Callback returns casted to CallbackEndpoint button, which mostly useful for handlers registering.

Example:

// Handling settings button
b.Handle(lt.Callback("settings"), OnSettings)

func (*Layout) Locale

func (lt *Layout) Locale(c tele.Context) (string, bool)

Locale returns the context locale.

func (*Layout) Markup

func (lt *Layout) Markup(c tele.Context, k string, args ...interface{}) *tele.ReplyMarkup

Markup returns a markup, which locale is dependent on the context. The given optional argument will be passed to the template engine.

buttons:
  settings: 'Settings'
markups:
  menu:
    - [settings]

Usage:

func OnStart(c tele.Context) error {
	return c.Send(
		lt.Text(c, "start"),
		lt.Markup(c, "menu"))
}

func (*Layout) MarkupLocale

func (lt *Layout) MarkupLocale(locale, k string, args ...interface{}) *tele.ReplyMarkup

MarkupLocale returns a localised markup processed with standard template engine. See Markup for more details.

func (*Layout) Middleware

func (lt *Layout) Middleware(defaultLocale string, localeFunc ...LocaleFunc) tele.MiddlewareFunc

Middleware builds a global middleware to make internationalisation work.

Usage:

b.Use(lt.Middleware("en", func(r tele.Recipient) string {
	loc, _ := db.UserLocale(r.Recipient())
	return loc
}))

func (*Layout) SetLocale

func (lt *Layout) SetLocale(c tele.Context, locale string)

SetLocale allows you to change a locale for the passed context.

func (*Layout) Settings

func (lt *Layout) Settings() tele.Settings

Settings returns built telebot Settings required for bot initialising.

settings:
  url: (custom url if needed)
  token: (not recommended)
  updates: (chan capacity)
  locales_dir: (optional)
  token_env: (token env var name, example: TOKEN)
  parse_mode: (default parse mode)
  long_poller: (long poller settings)
  webhook: (or webhook settings)

Usage:

lt, err := layout.New("bot.yml")
b, err := tele.NewBot(lt.Settings())
// That's all!

func (*Layout) Text

func (lt *Layout) Text(c tele.Context, k string, args ...interface{}) string

Text returns a text, which locale is dependent on the context. The given optional argument will be passed to the template engine.

Example of en.yml:

start: Hi, {{.FirstName}}!

Usage:

func OnStart(c tele.Context) error {
	return c.Send(lt.Text(c, "start", c.Sender()))
}

func (*Layout) TextLocale

func (lt *Layout) TextLocale(locale, k string, args ...interface{}) string

TextLocale returns a localised text processed with standard template engine. See Text for more details.

func (*Layout) UnmarshalYAML

func (lt *Layout) UnmarshalYAML(data []byte) error

type LocaleFunc

type LocaleFunc func(tele.Recipient) string

LocaleFunc is the function used to fetch the locale of the recipient. Returned locale will be remembered and linked to the corresponding context.

type Markup

type Markup struct {
	ResizeKeyboard  *bool `json:"resize_keyboard,omitempty"` // nil == true
	ForceReply      bool  `json:"force_reply,omitempty"`
	OneTimeKeyboard bool  `json:"one_time_keyboard,omitempty"`
	RemoveKeyboard  bool  `json:"remove_keyboard,omitempty"`
	Selective       bool  `json:"selective,omitempty"`
	// contains filtered or unexported fields
}

Markup represents layout-specific markup to be parsed.

type Settings

type Settings struct {
	URL     string
	Token   string
	Updates int

	LocalesDir string `json:"locales_dir"`
	TokenEnv   string `json:"token_env"`
	ParseMode  string `json:"parse_mode"`

	Webhook    *tele.Webhook    `json:"webhook"`
	LongPoller *tele.LongPoller `json:"long_poller"`
}

Jump to

Keyboard shortcuts

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