Documentation ¶
Index ¶
- type Button
- type Config
- func (c *Config) Bool(k string) bool
- func (c *Config) ChatID(k string) tele.ChatID
- func (c *Config) Duration(k string) time.Duration
- func (c *Config) Float(k string) float64
- func (c *Config) Floats(k string) (floats []float64)
- func (c *Config) Get(k string) *Config
- func (c *Config) Int(k string) int
- func (c *Config) Int64(k string) int64
- func (c *Config) Int64s(k string) (ints []int64)
- func (c *Config) Ints(k string) []int
- func (c *Config) Slice(k string) (slice []*Config)
- func (c *Config) String(k string) string
- func (c *Config) Strings(k string) []string
- func (c *Config) Unmarshal(v interface{}) error
- func (c *Config) UnmarshalKey(k string, v interface{}) error
- type DefaultLayout
- func (dlt *DefaultLayout) Button(k string, args ...interface{}) *tele.Btn
- func (dlt *DefaultLayout) Callback(k string) tele.CallbackEndpoint
- func (dlt *DefaultLayout) Markup(k string, args ...interface{}) *tele.ReplyMarkup
- func (dlt *DefaultLayout) Middleware() tele.MiddlewareFunc
- func (dlt *DefaultLayout) Result(k string, args ...interface{}) tele.Result
- func (dlt *DefaultLayout) Settings() tele.Settings
- func (dlt *DefaultLayout) Text(k string, args ...interface{}) string
- type Layout
- func (lt *Layout) Button(c tele.Context, k string, args ...interface{}) *tele.Btn
- func (lt *Layout) ButtonLocale(locale, k string, args ...interface{}) *tele.Btn
- func (lt *Layout) Callback(k string) tele.CallbackEndpoint
- func (lt *Layout) Commands() (cmds []tele.Command)
- func (lt *Layout) CommandsLocale(locale string, args ...interface{}) (cmds []tele.Command)
- func (lt *Layout) Default(locale string) *DefaultLayout
- func (lt *Layout) Locale(c tele.Context) (string, bool)
- func (lt *Layout) Locales() []string
- func (lt *Layout) Markup(c tele.Context, k string, args ...interface{}) *tele.ReplyMarkup
- func (lt *Layout) MarkupLocale(locale, k string, args ...interface{}) *tele.ReplyMarkup
- func (lt *Layout) Middleware(defaultLocale string, localeFunc ...LocaleFunc) tele.MiddlewareFunc
- func (lt *Layout) Result(c tele.Context, k string, args ...interface{}) tele.Result
- func (lt *Layout) ResultLocale(locale, k string, args ...interface{}) tele.Result
- func (lt *Layout) SetLocale(c tele.Context, locale string)
- func (lt *Layout) Settings() tele.Settings
- func (lt *Layout) Text(c tele.Context, k string, args ...interface{}) string
- func (lt *Layout) TextLocale(locale, k string, args ...interface{}) string
- func (lt *Layout) UnmarshalYAML(data []byte) error
- type LocaleFunc
- type Markup
- type Result
- type ResultBase
- type ResultContent
- type Settings
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Button ¶
type Button struct { tele.Btn `yaml:",inline"` Data interface{} `yaml:"data"` IsReply bool `yaml:"reply"` }
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) Duration ¶
Duration returns a field casted to the time.Duration. Accepts number-represented duration or a string in 0nsuµmh format.
func (*Config) Get ¶
Get returns a child map field wrapped into Config. If the field isn't a map, returns nil.
func (*Config) Slice ¶
Slice returns a child slice of objects wrapped into Config. If the field isn't a slice, returns nil.
func (*Config) Unmarshal ¶
Unmarshal parses the whole config into the out value. It's useful when you want to describe and to pre-define the fields in your custom configuration struct.
func (*Config) UnmarshalKey ¶
UnmarshalKey parses the specific key in the config into the out value.
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 (*Layout) Button ¶
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 ¶
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 ¶
Commands returns a list of telebot commands, which can be used in b.SetCommands later.
func (*Layout) CommandsLocale ¶
CommandsLocale returns a list of telebot commands and localized description, which can be used in b.SetCommands later.
Example of bot.yml:
commands: /start: '{{ text `cmdStart` }}'
en.yml:
cmdStart: Start the bot
ru.yml:
cmdStart: Запуск бота
Usage:
b.SetCommands(lt.CommandsLocale("en"), "en") b.SetCommands(lt.CommandsLocale("ru"), "ru")
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) Markup ¶
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 ¶
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 ¶
ResultLocale returns a localized result processed with text/template engine. See Result for more details.
func (*Layout) 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 ¶
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 ¶
TextLocale returns a localized text processed with text/template engine. See Text for more details.
func (*Layout) UnmarshalYAML ¶
type LocaleFunc ¶
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 { tele.ResultBase `yaml:",inline"` Content ResultContent `yaml:"content"` 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.