Why Go is the ideal language for Telegram bots
If you've ever written bots in Python, you know: everything works until you hit a performance bottleneck. Go solves this problem at the architecture level. It's fast, lightweight, and doesn't require a virtual environment. A Telegram bot in Go is not just a toy, but a full-fledged service that can handle thousands of requests per second and consumes minimal memory.
In this article, we will create a bot in Golang from scratch that will respond to commands, process messages, and even work with an external API. No unnecessary theory — just code you can run immediately.
Step 1: Environment setup
Before writing a Telegram bot in Go, make sure you have Go version 1.20 or higher installed. You can check with the command:
go versionIf Go is not installed, download it from the official website. Installation takes 2 minutes.
You will also need a bot token. Go to Telegram, find BotFather and run the command:
/newbotEnter the bot's name and username. After creation, BotFather will send you a token — save it, you'll need it to connect to the Telegram Bot API.
Step 2: Project structure
Create a project folder and initialize the module:
mkdir my-telegram-botcd my-telegram-botgo mod init my-telegram-botTo work with Telegram, we will use the tgbotapi library — this is the most popular Go library for Telegram. Install it:
go get github.com/go-telegram-bot-api/telegram-bot-api/v5Now create the main.go file. The basic structure will be like this:
package main
import ( "log" "os" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5")
func main() { token := os.Getenv("TELEGRAM_BOT_TOKEN") bot, err := tgbotapi.NewBotAPI(token) if err != nil { log.Panic(err) }
bot.Debug = true log.Printf("Bot @%s started", bot.Self.UserName)
u := tgbotapi.NewUpdate(0) u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates { if update.Message != nil { log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Hello! I'm working on Go!") bot.Send(msg) } }}Run the bot:
export TELEGRAM_BOT_TOKEN="your_token"go run main.goWrite to the bot in Telegram — it should reply. If everything works, move on to the next step.
Step 3: Command handling
Just replying "Hello" is boring. Let's add command handling for the Telegram bot. Let's implement the /start and /help commands:
for update := range updates { if update.Message != nil { switch update.Message.Command() { case "start": msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Welcome! I'm a bot in Go. Use /help for a list of commands.") bot.Send(msg) case "help": msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Available commands:/start - greeting/help - help/echo - repeat message") bot.Send(msg) default: msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Unknown command. Use /help") bot.Send(msg) } }}Now the bot responds to commands. But this is just the beginning. The real power of a Golang Telegram bot lies in its performance when handling large loads.
Step 4: Working with an external API
Let's add a useful function