A Telegram bot is an automated account that responds to commands and messages inside Telegram. Telegram offers a public and free API to create these bots, which makes it one of the simplest ways to get started with automation.

How it works

The process starts with BotFather, the official bot that creates other bots. You chat with it, choose a name and receive a token, which is your bot's password. Keep the token secret, preferably in environment variables.

Then your code receives messages in two ways: by polling the API from time to time or by receiving notifications via webhook.

A minimal example to fetch updates:

Terminal
curl "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getUpdates"

To reply, use the sendMessage method with the conversation identifier and the text.

Practical example

You have a store and want to know sales without opening the dashboard. Create a bot that, every day at 6pm, sends you on Telegram the total sold. It also accepts the /hoje command to check right away.

With an AI agent, the bot can read the sales spreadsheet and answer questions like "which product sold the most this week?".

What it's for

  • Receive alerts from systems, servers and sales.
  • Support customers with menus and automatic replies.
  • Control tasks via commands, such as requesting a report.
  • Create communities with automatic moderation.

Best practices

Never publish the token in a public repository. If it leaks, generate a new one with BotFather. Limit who can use sensitive commands by checking the user identifier.

Respect the API sending limits so the bot is not blocked and handle errors carefully, retrying after a few seconds. Test first in a private group, with few people, before opening to the public.