Skip to content
This repository was archived by the owner on Aug 20, 2024. It is now read-only.
Kristoff Liao edited this page Aug 20, 2024 · 1 revision

Welcome to the Wiki! Are you new in Telegram Bot? I can help you guide your first Telegram Bot Project. You can use this repository as a template for your Telegram Bot. They are written in JavaScript. If you do like other language (such as Python, PHP, etc), then I'm afraid this repository cannot help you.

Understanding How Telegram Bot Works

Definition of Telegram Bot

A Telegram bot is a special account on Telegram that does not require a phone number to set up, unlike normal Telegram account, which requires phone number. Users can interact with these bots by sending them messages, commands, and inline queries. Bots can be designed to perform various automated tasks, such as providing information, managing groups, processing payments, and more.

Setup your First Telegram Bot

To create a Telegram bot, you need to have a chat with @BotFather. This account is the official account from Telegram that will guide you through the process of setup your first bot. After all the process, you will receive the Bot Token. Keep this safe and never reveal publicly.

Bot Token acts as Password to your Telegram Bot. Anyone who had access to your Bot Token can do to your Telegram Bot whatever they like. For example, Spamming. Spamming can get your bot blocked by Telegram, as Telegram enforces rate limits on bot interactions, so once again, make sure to keep your Bot Token safe.

How Telegram Bots Interact

Telegram bots interact with the Telegram server through the Telegram Bot API. This API allows you to control your bots and respond to user interactions. The interaction model generally follows these steps:

  • User Interaction: A user sends a message or command to the bot.
  • Telegram Server: The message is sent to the Telegram servers.
  • Bot Server: The Telegram server forwards the message to your server where your bot was hosted.
  • Bot Response: Your bot processes the message and sends a response back to the user through the Telegram servers.

Operation Modes

Telegram bots can operate in two main modes:

  • Polling: In this mode, your bot continuously sends requests to the Telegram server to check if there are any new messages. This method is easier to implement but less efficient as it can result in delays.
  • Webhook: In this mode, you set up a webhook URL on your server. The Telegram server will send updates to this URL whenever there’s a new message. This method is more efficient as it allows real-time updates, but it requires that your server is accessible via HTTPS.

Handling User Commands

Telegram bots can handle various types of user input:

  • Text Commands: Users can type commands (e.g., /start, /help) that trigger specific bot actions.
  • Inline Mode: Users can type commands in any chat by typing the bot's username and a query (e.g., @mybot search query). The bot can then return results directly in the chat.
  • Callback Queries: When users interact with buttons in the bot's message, the bot receives a callback query that it can respond to.
  • Files and Media: Bots can also handle media files like photos, videos, and documents. Your bot can download these files, process them, and send a response back to the user.

Development and Deployment

You can develop your bot using various programming languages (Python, Node.js, Java, PHP, etc.) by using available libraries like telebot for Python or telegraf for Node.js. Testing can be done in a sandbox environment before deploying it live. Here are the sandbox environment provider you can use for your developing your bots:

Providers Links
Project IDX https://idx.google.com
Replit https://replit.com
CodeSandbox https://codesandbox.io
Glitch https://glitch.com
Fly https://fly.io

Once your bot is ready, it can be deployed to a server with a public IP and an SSL certificate (if using webhooks). After deployment, users can start interacting with your bot directly on Telegram. Don't forget to regularly monitor your bot’s performance and user interactions. You might need to update the bot to add new features, fix bugs, or adapt to changes in the Telegram API.