TutorialsAugust 4, 20264 min read

How to Run a Telegram Bot 24/7 on a VPS

Deploy a Telegram bot on a Linux VPS and keep it online 24/7: Python or Node, polling vs webhooks, a safe token, and a systemd service that auto-restarts.

NBy Nxeon

A Telegram bot should run somewhere that never sleeps, and a VPS is the natural home — always on, with a stable IP that also lets you use webhooks. This guide deploys a Telegram bot (Python or Node) and keeps it online 24/7. The same server habits apply as for hosting a Discord bot on a VPS 24/7.

Get a bot token first

Message @BotFather in Telegram, create a bot, and copy the token it gives you. Keep it secret — anyone with the token controls your bot.

Polling vs webhooks

  • Polling: the bot repeatedly asks Telegram for updates. Dead simple — no domain, no SSL, works behind any firewall. Ideal to start.
  • Webhooks: Telegram pushes updates to a public HTTPS URL on your server. More efficient at scale, but needs a domain and a valid TLS certificate.

Start with polling; move to webhooks only when you need the efficiency. If you go webhook, you'll want HTTPS — see set up Nginx with free SSL using Let's Encrypt.

Step 1: Prepare the server

sudo adduser bot
su - bot
sudo apt update && sudo apt upgrade -y

Harden it with securing your first Linux VPS: 10 steps.

Step 2a: A Python bot (python-telegram-bot)

sudo apt install -y python3 python3-venv python3-pip git
cd ~ && git clone https://github.com/yourname/your-telegram-bot.git
cd your-telegram-bot
python3 -m venv venv && source venv/bin/activate
pip install python-telegram-bot python-dotenv

A minimal bot.py using polling:

import os
from dotenv import load_dotenv
from telegram.ext import Application, CommandHandler

load_dotenv()

async def start(update, context):
    await update.message.reply_text("Hello from my VPS bot!")

app = Application.builder().token(os.environ["TELEGRAM_TOKEN"]).build()
app.add_handler(CommandHandler("start", start))
app.run_polling()

Step 2b: A Node bot (Telegraf)

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
npm install telegraf dotenv
require("dotenv").config();
const { Telegraf } = require("telegraf");
const bot = new Telegraf(process.env.TELEGRAM_TOKEN);
bot.start((ctx) => ctx.reply("Hello from my VPS bot!"));
bot.launch();
The Nxeon control panel showing live CPU, memory, and network metrics for a VPS
The Nxeon control panel showing live CPU, memory, and network metrics for a VPS

Step 3: Store the token safely

echo "TELEGRAM_TOKEN=your-bot-token-here" > .env
chmod 600 .env

Test the bot by hand (python bot.py or node index.js), message it /start, then stop it.

Step 4: Run it 24/7 with systemd

Create /etc/systemd/system/telegrambot.service (Python shown):

[Unit]
Description=Telegram Bot
After=network.target

[Service]
Type=simple
User=bot
WorkingDirectory=/home/bot/your-telegram-bot
ExecStart=/home/bot/your-telegram-bot/venv/bin/python /home/bot/your-telegram-bot/bot.py
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now telegrambot
sudo journalctl -u telegrambot -f

For a Node bot, PM2 is a great alternative — the workflow is identical to how to keep a Discord bot online with PM2.

Step 5: Updates and monitoring

cd ~/your-telegram-bot
git pull
sudo systemctl restart telegrambot

Watch resource use with the tips in how to monitor server resources on Linux. Telegram bots are light — a 1 GB VPS runs several comfortably.

Switching to webhooks

Once your bot is popular, webhooks are more efficient than polling. You need a domain pointing at your VPS and HTTPS. With python-telegram-bot:

app.run_webhook(
    listen="0.0.0.0",
    port=8443,
    url_path=os.environ["TELEGRAM_TOKEN"],
    webhook_url="https://bot.example.com/" + os.environ["TELEGRAM_TOKEN"]
)

Put Nginx or Caddy in front for TLS and proxy to the bot's port — see set up Caddy for automatic HTTPS on a VPS. Open only the HTTPS port publicly and keep the bot's internal port on localhost.

Beyond simple commands

Real bots do more than reply to /start. python-telegram-bot and Telegraf both support message handlers, inline keyboards, and callback queries for buttons. Split handlers into separate modules as the bot grows so a single file doesn't become unmanageable.

Troubleshooting

  • Silent after switching to webhooks: the webhook URL must be public HTTPS with a valid certificate; verify with Telegram's getWebhookInfo.
  • Two instances fighting: you can't poll and use a webhook at once, and you can't run two pollers on one token — run exactly one instance.
  • Bot stops after logout: it isn't under systemd or PM2 — set up the service as above.

FAQ

Polling or webhooks — which should I use?

Start with polling: it needs no domain or SSL and works anywhere. Switch to webhooks (HTTPS URL + certificate) only when you need higher efficiency at scale.

How do I keep the bot running after logout?

Run it as a systemd service or under PM2. Both detach it from your SSH session and restart it on crash and reboot.

Where do I get a Telegram bot token?

From @BotFather in the Telegram app. Create a bot, copy the token, and store it in a .env file with chmod 600.

How much does hosting a Telegram bot cost?

Very little — a small 1 GB VPS runs one or several bots for a few dollars a month.

Keep your Telegram bot online for good on an NVMe VPS from Nxeon — full root access, stable IP for webhooks, and Python/Node ready to go.

#telegram bot#vps#python#systemd#24/7#seobatch

Deploy your first server in under a minute

Creating an account is free and takes no card details. You pay when you deploy — choose a billing term and pay from your wallet or by card at checkout.