layout

package
v0.0.0-...-ebbf02d Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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

Bool 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) Int64s

func (c *Config) Int64s(k string) (ints []int64)

Int64s returns a field casted to the int64 slice.

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 DefaultLayout

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

DefaultLayout is a simplified layout instance with pre-defined locale by default.

func NewDefault

func NewDefault(path, locale string, funcs ...template.FuncMap) (*DefaultLayout, error)

NewDefault parses the given layout file without localization features. See Layout.Default for more details.

func (*DefaultLayout) Button

func (dlt *DefaultLayout) Button(k string, args ...interface{}) *tele.Btn

Button wraps localized layout function Button using your default locale.

func (*DefaultLayout) Callback

func (dlt *DefaultLayout) Callback(k string) tele.CallbackEndpoint

Callback returns a callback endpoint used to handle buttons.

func (*DefaultLayout) Markup

func (dlt *DefaultLayout) Markup(k string, args ...interface{}) *tele.ReplyMarkup

Markup wraps localized layout function Markup using your default locale.

func (*DefaultLayout) Middleware

func (dlt *DefaultLayout) Middleware() tele.MiddlewareFunc

Middleware wraps ordinary layout middleware with your default locale.

func (*DefaultLayout) Result

func (dlt *DefaultLayout) Result(k string, args ...interface{}) tele.Result

Result wraps localized layout function Result using your default locale.

func (*DefaultLayout) Settings

func (dlt *DefaultLayout) Settings() tele.Settings

Settings returns layout settings.

func (*DefaultLayout) Text

func (dlt *DefaultLayout) Text(k string, args ...interface{}) string

Text wraps localized layout function Text using your default locale.

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, funcs ...template.FuncMap) (*Layout, error)

New 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 localized button processed with text/template engine. See Button for more details.

func (*Layout) Callback

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

Callback returns a callback endpoint used to handle buttons.

Example:

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

func (*Layout) Commands

func (lt *Layout) Commands() (cmds []tele.Command)

Commands returns a list of telebot commands, which can be used in b.SetCommands later.

func (*Layout) Default

func (lt *Layout) Default(locale string) *DefaultLayout

Default returns a simplified layout instance with the pre-defined locale. It's useful when you have no need for localization and don't want to pass context each time you use layout functions.

func (*Layout) Locale

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

Locale returns the context locale.

func (*Layout) Locales

func (lt *Layout) Locales() []string

Locales returns all presented locales.

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 localized markup processed with text/template engine. See Markup for more details.

func (*Layout) Middleware

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

Middleware builds a telebot middleware to make localization work.

Usage:

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

func (*Layout) Result

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

Result returns an inline result, which locale is dependent on the context. The given optional argument will be passed to the template engine.

results:
  article:
    type: article
    id: '{{ .ID }}'
    title: '{{ .Title }}'
    description: '{{ .Description }}'
    message_text: '{{ .Content }}'
    thumb_url: '{{ .PreviewURL }}'

Usage:

func onQuery(c tele.Context) error {
	results := make(tele.Results, len(articles))
	for i, article := range articles {
		results[i] = lt.Result(c, "article", article)
	}
	return c.Answer(&tele.QueryResponse{
		Results:   results,
		CacheTime: 100,
	})
}

func (*Layout) ResultLocale

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

ResultLocale returns a localized result processed with text/template engine. See Result for more details.

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 initializing.

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 localized text processed with text/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 `yaml:"resize_keyboard,omitempty"` // nil == true
	ForceReply      bool  `yaml:"force_reply,omitempty"`
	OneTimeKeyboard bool  `yaml:"one_time_keyboard,omitempty"`
	RemoveKeyboard  bool  `yaml:"remove_keyboard,omitempty"`
	Selective       bool  `yaml:"selective,omitempty"`
	// contains filtered or unexported fields
}

Markup represents layout-specific markup to be parsed.

type Result

type Result struct {
	ResultBase `yaml:",inline"`
	Markup     string `yaml:"markup"`
	// contains filtered or unexported fields
}

Result represents layout-specific result to be parsed.

type ResultBase

type ResultBase struct {
	tele.ResultBase `yaml:",inline"`
	Content         ResultContent `yaml:"content"`
}

ResultBase represents layout-specific result's base to be parsed.

type ResultContent

type ResultContent map[string]interface{}

ResultContent represents any kind of InputMessageContent and implements it.

func (ResultContent) IsInputMessageContent

func (ResultContent) IsInputMessageContent() bool

IsInputMessageContent implements telebot.InputMessageContent.

type Settings

type Settings struct {
	URL     string
	Token   string
	Updates int

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

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

Jump to

Keyboard shortcuts

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