> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://help.squarecloud.app/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# How to host a Telegram bot 24/7

## Why use the official Telegram API

Unlike other platforms, Telegram offers an **official, free bot API** — with no risk of violating the terms of service (ToS) for automation. This makes Telegram one of the most worry-free environments to host bots: you create, host, and keep it online without fear of bans for using the API as intended.

## 1. Create your bot in BotFather

1. On Telegram, search for **@BotFather** (the official creation bot).
2. Send the `/newbot` command.
3. Choose a name and a username (which must end in `bot`).
4. BotFather will hand you a **token** — keep it safe.

## 2. Protect your token

The token is your bot's access key. Never expose it:

* Don't push the token to GitHub.
* Store it in an environment variable (e.g., `TELEGRAM_TOKEN`).
* If it leaks, use `/revoke` in BotFather to generate a new one.

## 3. Prepare your project

Pick a library for the language you prefer:

* **Node.js**: `node-telegram-bot-api` or `telegraf`.
* **Python**: `python-telegram-bot` or `aiogram`.

Minimal example with telegraf (Node.js):
```
const { Telegraf } = require('telegraf');
const bot = new Telegraf(process.env.TELEGRAM_TOKEN);

bot.start((ctx) => ctx.reply('Bot online on Square Cloud!'));
bot.launch();
```

## 4. Webhook or polling?

* **Polling**: the bot asks Telegram if there's anything new. Simpler to set up — great to start.
* **Webhook**: Telegram sends updates to a public URL of your bot. More efficient for large bots.

For most cases, start with polling.

## 5. Deploy and keep it 24/7

1. Create the `squarecloud.app` file with your application's info.
2. Set the `TELEGRAM_TOKEN` environment variable.
3. Send the project via the Dashboard or the CLI.
4. Done: Square Cloud keeps your bot online 24 hours a day.

## Keeping your bot resilient

Bots that run all the time need to handle errors and reconnections well. For stability best practices, see [How to keep bots online 24/7](https://help.squarecloud.app/en-us/article/how-to-keep-bots-online-247-1j8fio/). If you're starting from scratch, also check out [What is deploy? How to put your application online](https://help.squarecloud.app/en-us/article/what-is-deploy-how-to-put-your-application-online-tq0tv5/).
