How do you host a Telegram or Discord bot 24/7?
Rent a small Linux KVM VPS, create a normal user for the bot, install Python in a virtual environment or Node.js, and put the bot token in an environment file that never goes into code or git. Run the bot as a systemd service with Restart=on-failure, or under pm2 for Node.js, so it starts at boot and comes back after a crash. Telegram bots can use long polling or a webhook; Discord bots keep a Gateway connection open.
Below is each step on a RapidServer KVM VPS with full root in Mumbai (BOM1) or Delhi NCR (DEL1). Your bot must follow Telegram's or Discord's terms and our Acceptable Use Policy.
Why free hosts sleep or restart bots
Free tiers and hobby platforms are built for web apps that answer a request and then sit idle. To share hardware, they commonly sleep an app after a spell without web traffic, restart it on a schedule, cap its monthly running hours or stop it at a memory limit. A website wakes up on the next visit, but a polling Telegram bot or a Discord bot on the Gateway has no visitor to wake it, so it goes quiet. A home laptop has its own gaps: sleep mode and power cuts.
On a VPS for bot hosting, the bot is an ordinary Linux process on a server that stays on.
Telegram polling vs webhooks, and the Discord Gateway
The Telegram Bot API has two mutually exclusive ways to receive updates: getUpdates, where your bot uses long polling to ask Telegram for new updates, and setWebhook, where Telegram sends an HTTPS POST to your URL for every update. Pending updates wait for the bot, but for no longer than 24 hours.
| Long polling (getUpdates) | Webhook (setWebhook) | |
|---|---|---|
| How updates arrive | The bot asks Telegram | Telegram sends an HTTPS POST |
| Domain and HTTPS | Not needed | Needed, on port 443, 80, 88 or 8443 |
| Inbound ports to open | None | 80 and 443 for a reverse proxy |
getUpdates doesn't work while a webhook is set, and deleteWebhook switches a bot back to polling. Long polling needs no domain, certificate or open port, so start there.
Discord bot hosting 24/7 works differently. A Discord bot receives events through the Gateway, a persistent WebSocket connection it keeps open, sending heartbeats at the interval Discord sets and resuming after a disconnect to replay missed events. Libraries such as discord.js and discord.py handle this; your server just keeps the process running. Apps that only handle interactions, such as slash commands, can instead receive them over HTTPS, without a connected client.
Set up the VPS: a bot user, then Python or Node.js
Pick Ubuntu LTS or Debian, SSH in (new to SSH? read SSH kya hai) and create a normal user for the bot, so a bug in its code can't act as root:
sudo apt update && sudo apt upgrade -y sudo apt install -y git python3-venv sudo adduser --disabled-password bot sudo -iu bot git clone https://github.com/you/mybot.git ~/mybot
Python. Give each bot its own virtual environment:
cd ~/mybot python3 -m venv .venv .venv/bin/pip install -r requirements.txt
Node.js. Install a current LTS release, or pick our ready Node.js image (LTS with PM2), then run npm ci --omit=dev. Commit package-lock.json or pin versions in requirements.txt, so the server runs what you tested.
Keep the bot token in an environment file
The token is the bot's password. Telegram warns that it "can be used by anyone to control your bot", and Discord says never to share it or check it into version control. Keep it out of code and git, in a .env file only the bot user can read:
BOT_TOKEN=change-me-paste-your-token
chmod 600 ~/mybot/.env echo ".env" >> ~/mybot/.gitignore
Read it with os.environ["BOT_TOKEN"] in Python or process.env.BOT_TOKEN in Node.js. If it leaks, BotFather's /token command generates a new one for Telegram; for Discord, use Reset Token on your app's Bot page.
Run the bot 24/7 with systemd or pm2
systemd starts the bot at boot, restarts it after a crash and collects its logs. From your sudo account, create /etc/systemd/system/mybot.service:
[Unit] Description=My Telegram or Discord bot After=network-online.target Wants=network-online.target [Service] User=bot WorkingDirectory=/home/bot/mybot EnvironmentFile=/home/bot/mybot/.env Environment=PYTHONUNBUFFERED=1 ExecStart=/home/bot/mybot/.venv/bin/python bot.py Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target
For Node.js, set ExecStart to the path from which node plus your script, such as /usr/bin/node bot.js. Start the service and follow its logs with journalctl:
sudo systemctl daemon-reload sudo systemctl enable --now mybot systemctl status mybot sudo journalctl -u mybot -f
pm2 for Node.js. Use pm2 instead of a systemd unit, as the bot user. It restarts the bot after a crash, and pm2 startup prints a command to run from your sudo account so pm2 starts at boot. pm2 doesn't read .env itself, so load it with the dotenv package:
cd ~/mybot pm2 start bot.js --name mybot pm2 save pm2 startup pm2 logs mybot
Telegram webhooks behind Caddy
A webhook needs a public HTTPS address. Point a subdomain such as bot.example.com at the VPS (see DNS kya hai), have the bot listen on localhost, and let Caddy get and renew the Let's Encrypt certificate by itself:
bot.example.com {
reverse_proxy 127.0.0.1:8080
}
Open the web ports with ufw allow 80,443/tcp (see our firewall guide), then register the URL with a secret token. Most libraries do this for you; by hand:
set -a; . ~/mybot/.env; set +a curl "https://api.telegram.org/bot$BOT_TOKEN/setWebhook" \ -d "url=https://bot.example.com/telegram" \ -d "secret_token=change-me-random-letters-and-digits"
Telegram sends that secret in the X-Telegram-Bot-Api-Secret-Token header of every webhook request, so the bot can reject requests without it. Every RapidServer VPS also sits behind always-on DDoS filtering.
Updates and backups
- The server. Turn on
unattended-upgradeson Ubuntu and Debian, and reboot when a new kernel needs it; systemd or pm2 brings the bot back. - The bot. Run
git pull, reinstall dependencies, thensudo systemctl restart mybotorpm2 restart mybot. Read the library's changelog before a major version. - Backups. Every RapidServer VPS gets daily backups kept for 7 days, plus snapshots on demand for risky upgrades. Keep the token in a password manager, and copy bot data, such as a SQLite file, off the server.
Which VPS plan a bot needs
Most bots spend their time waiting for the next message, so a small bot fits the smallest plan: 2 dedicated vCPU and 4 GB RAM for ₹581/mo plus 18% GST, billed monthly.
Scale up when a Discord bot joins many servers (libraries usually cache server and member data in memory), when a Telegram bot serves many users at once, or when the bot also runs a database or image processing. Check free -m and the client-area graphs after the first week, then resize from the client area with the difference prorated. Rather not run Linux yourself? Add managed support.
Full root, dedicated vCPU and always-on DDoS filtering in Mumbai or Delhi NCR, ready a few minutes after payment, from ₹581/mo.
See VPS plansMumbai or Delhi NCR for bot hosting in India
For Discord bot hosting in India, or a Telegram bot, either city works: the bot talks to Telegram's or Discord's servers, not straight to your users. If the same VPS also serves a dashboard or API your users open, choose the city closest to them:
- Mumbai (BOM1) for western and southern India, typically 1–4 ms from Mumbai and Pune.
- Delhi NCR (DEL1) for Delhi NCR and the north, typically 1–8 ms. There is no separate Noida data center; Noida is served from DEL1.
Both cost the same, data and backups stay in India, and our team moves a VPS between them for free. See our data centers.
Questions
What is the best way to host a Telegram bot 24/7?+
Restart=on-failure, under its own user, with the token in an environment file. Start with long polling. A RapidServer VPS starts at ₹581/mo plus 18% GST.Can I host a Discord bot 24/7 for free?+
Should my Telegram bot use polling or a webhook?+
How much RAM does a Discord or Telegram bot need?+
Where should I keep my bot token?+
.env file with chmod 600, listed in .gitignore and loaded by systemd or dotenv, never in your code or git. If it leaks, make a new one with BotFather or Reset Token on Discord.Telegram bot 24 ghante kaise chalaye?+
.env file mein rakhiye (code ya git mein nahi), aur bot ko systemd service ya pm2 se chalaiye, taaki crash ya reboot ke baad woh apne aap wapas chalu ho jaye. RapidServer par VPS ₹581/mo plus 18% GST se shuru hota hai aur payment ke kuch minute baad ready ho jata hai.