Prerequisites
- Go 1.16 or later (the minimum version declared in Telebot’s
go.mod) - A Telegram account
Steps
Create a bot with BotFather
Open Telegram and start a conversation with @BotFather.Send the
/newbot command and follow the prompts. BotFather will give you an API token that looks like:Create main.go
Create a file named The three key calls are:
main.go with the following content:tele.NewBot(pref)— creates the bot and callsgetMeto verify the token.b.Handle("/start", ...)— registers a handler for the/startcommand. The handler receives atele.Contextand returns anerror.b.Start()— begins long-polling for updates and blocks until the bot is stopped.
Complete example
Here is the full runnable program from the steps above, with error handling and the token read from the environment:How it works
NewBot and Settings
tele.NewBot accepts a tele.Settings struct:
Poller is not set, a bare &LongPoller{} is used (no timeout). Always set a Timeout in production.
Handle
b.Handle(endpoint, handlerFunc, ...middleware) binds a HandlerFunc to an endpoint. The endpoint can be a command string, a telebot event constant, or a pointer to a button.
Context.Send
c.Send(what, ...options) sends a message to the current chat. what can be a string, a *Photo, a *Video, or any other type that implements Sendable. Options like tele.Silent and tele.NoPreview can be passed as variadic arguments.
Start
b.Start() blocks, polling for updates and dispatching them to handlers concurrently. Each handler runs in its own goroutine. To stop the bot programmatically, call b.Stop().
What’s next?
Introduction
Understand Telebot’s architecture: Bot, Context, Handlers, Middleware, and Poller.
Context
Learn every method available on
tele.Context and how to use local storage within a handler.Handlers & Routing
Explore all supported event constants, command payloads, and button routing.
Middleware
Add logging, access control, and other cross-cutting logic with the middleware chain.