Skip to main content
The 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

This reads and parses the YAML file at 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, use NewDefault 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

The settings: 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

Call lt.Middleware to register a Telebot middleware that assigns a locale to each incoming context. The middleware cleans up the locale when the handler returns.
If 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 the locales/ directory next to the YAML config. Each file is named <locale>.yml and contains flat or nested key-value pairs that support Go templates:
The locales_dir setting key changes this directory:

Text Rendering

Template keys support dot notation for nested values — lt.Text(c, "nested.example", "telebot") returns "This is telebot.".

Markup Helpers

The layout automatically detects whether the markup is inline or reply based on the button types in use. An error is returned if you mix inline and reply buttons in the same markup.

Button Helpers

Inline Result Helpers

Accessing Config Values

The config: section is exposed through the Config struct embedded in Layout:

Localized Bot Commands

Use lt.Commands() or lt.CommandsLocale() to register bot commands with localized descriptions:

Complete Example