No history yet

Telegram Bot Basics

What Are Telegram Bots

Telegram bots are automated programs that run inside the Telegram app. Think of them as tiny robots you can chat with. They aren't real people and don't have their own feelings, but they can be programmed to perform a huge range of tasks. You can use them to get news updates, convert files, set reminders, or even play games.

Lesson image

Unlike a human user, a bot has no online status and no last seen time. It just waits for your commands. Bots can offer simple, repetitive functions or complex, AI-powered conversations. They interact with users through messages, commands, and inline requests. You can often control them with special buttons and keyboards they send you, making the experience feel like using a mini-app right within your chat.

Creating Your First Bot

To create a bot, you need to talk to another bot. It sounds a bit funny, but Telegram has a master bot called BotFather that handles all bot creation and management. It's the one bot to rule them all.

Lesson image

Here's how to create your bot:

  1. Find BotFather: Open your Telegram app, go to the search bar, and type @BotFather. Look for the official one with a blue checkmark.
  2. Start a chat: Tap on it to open a chat and press the Start button.
  3. Send the command: Type /newbot and send it. BotFather will ask you for a name for your bot. This is a friendly display name, like "Weather Bot."
  4. Choose a username: Next, you'll need to pick a unique username. This username must end in the word "bot". For example, MyFirstWeatherBot or weather_bot.

Once you complete these steps, BotFather will send a message with your new bot's API token. Congratulations, you've just created a bot!

The API Token

The API token is the most important piece of information you'll receive from BotFather. It’s a long string of numbers and letters that acts as a secret key, authorizing you to control your bot. Think of it as the password to your bot's control panel.

123456789:ABCDEFGHIJKL-MNOPQRSTUVWXYZ_0123456789

You must keep this token secure and private. Never share it publicly or commit it to a public code repository like GitHub. If someone gets your token, they can take full control of your bot.

If you ever accidentally expose your token, you can go back to BotFather and use the /revoke command to generate a new one. This will make the old one invalid.

The Telegram Bot API

So you have a bot and a token. Now what? You use the token to communicate with the Telegram Bot API. An API, or Application Programming Interface, is just a set of rules that lets different software applications talk to each other. In this case, it lets your program talk to Telegram's servers to manage your bot.

The API works over HTTPS and is the foundation for everything your bot will do. It's how your program will:

  • Receive messages and commands from users.
  • Send replies back, whether as text, images, or other media.
  • Edit or delete messages.
  • Create custom keyboards and interactive buttons.
  • Get information about the chat or the user.

Essentially, every action your bot performs is a request made to the Bot API. Your code will package up a command, like "send this message to this user," and send it along with your secret token to a specific Telegram URL. Telegram's servers check the token, see that it's valid, and carry out your command.

Now that you understand the basics, let's test your knowledge.

Quiz Questions 1/5

What is the name of the special bot used to create and manage all other Telegram bots?

Quiz Questions 2/5

What is the primary purpose of the API token you receive after creating a bot?

With these fundamentals in hand, you're ready to start building.