layout subpackage (gopkg.in/telebot.v4/layout) lets you define your bot’s settings, buttons, markups, inline results, and localized text in a single YAML file rather than scattering them through Go code. Template rendering, i18n, and bot settings are all handled for you.
Installation
Loading a Layout
From a file path
path. Optional template.FuncMap arguments extend the built-in template functions available in your YAML templates.
From an embedded filesystem
Without localization (DefaultLayout)
If you don’t need per-context locale switching, useNewDefault or Layout.Default to get a simplified DefaultLayout that takes no Context argument:
Key Types
Button
Wraps
tele.Btn with YAML support. Can be a simple reply button (just a text string) or an extended inline/reply button with unique, callback_data, web_app, etc.Markup
A named keyboard layout (reply or inline) defined as rows of button references. Supports
resize_keyboard, one_time_keyboard, force_reply, and remove_keyboard.Result
A named inline query result template. Supports all Telegram result types (
article, photo, audio, video, gif, etc.) with Go template rendering.Config
A typed map interface over the
config: section of the YAML file. Provides String, Int, Bool, Duration, ChatID, and other typed accessors.YAML Config Structure
The layout file has several top-level sections. Here is the full example from the source (layout/example.yml):
Settings section
Thesettings: block maps directly to a tele.Settings struct. The parser reads it as:
Use
token_env instead of token to read the bot token from an environment variable, keeping secrets out of your config file.Wiring the Bot
lt.Settings() returns a fully populated tele.Settings value that you can pass directly to tele.NewBot:
Locale Support
Setting up the middleware
Calllt.Middleware to register a Telebot middleware that assigns a locale to each incoming context. The middleware cleans up the locale when the handler returns.
localeFunc is omitted (or returns an empty string), defaultLocale is used.
Reading and changing a locale
Locale files
By default the layout looks for locale files in thelocales/ directory next to the YAML config. Each file is named <locale>.yml and contains flat or nested key-value pairs that support Go templates:
locales_dir setting key changes this directory:
Text Rendering
lt.Text(c, "nested.example", "telebot") returns "This is telebot.".
Markup Helpers
Button Helpers
Inline Result Helpers
Accessing Config Values
Theconfig: section is exposed through the Config struct embedded in Layout:
Localized Bot Commands
Uselt.Commands() or lt.CommandsLocale() to register bot commands with localized descriptions: