Chạy Bot Telegram 24/7 Trên VPS - Hướng Dẫn Deploy Bot Miễn Phí, Không Cần Máy Tính

📅 05/2026 · ⏱ 11 phút đọc · 🏷 MMO

Bạn đang chạy bot Telegram trên máy tính và phải mở 24/24? Tốn điện, máy nóng, mạng lag? Deploy bot lên VPS là giải pháp hoàn hảo: bot chạy vĩnh viễn, ổn định, chi phí chỉ từ 40K/tháng. Cùng làm ngay trên VPS TrumVPS!

Tại Sao Nên Chạy Bot Telegram Trên VPS?

Có 3 cách chạy bot Telegram phổ biến:

💡 Thực tế: Với VPS 40K/tháng của TrumVPS, bạn có thể chạy cùng lúc 5-10 bot Telegram mà không lo giật lag. Rẻ hơn tiền điện để máy tính chạy 24/7!

Yêu Cầu

Phần 1: Tạo Bot Telegram Với @BotFather

Trước khi deploy lên VPS, bạn cần tạo bot và lấy token:

  1. Mở Telegram, tìm @BotFather
  2. Gửi lệnh /newbot
  3. Đặt tên bot (vd: "My Auto Reply Bot")
  4. Đặt username bot (phải kết thúc bằng bot, vd: myautoreply_bot)
  5. BotFather trả về HTTP API Token — lưu token này!

Ví dụ token: 7123456789:AAHxKxJzLmNqOpQrStUvWxYzAbCdEfGhIjK

⚠️ Bảo mật: Token bot chính là "chìa khóa" điều khiển bot. Không chia sẻ, không commit lên GitHub public!

Phần 2: Viết Bot Python Đơn Giản

Cài Đặt Python & Thư Viện

SSH vào VPS và cài đặt:

apt update && apt install -y python3 python3-pip python3-venv

Tạo thư mục bot và môi trường ảo:

mkdir ~/mybot && cd ~/mybot

python3 -m venv venv && source venv/bin/activate

pip install python-telegram-bot

Code Bot Echo (Auto Reply)

Tạo file bot.py:

nano bot.py

Dán code sau (thay YOUR_TOKEN bằng token từ BotFather):

from telegram import Update

from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes

TOKEN = "YOUR_TOKEN_HERE"

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):

    await update.message.reply_text("Chào bạn! Tôi là bot chạy trên VPS TrumVPS 🚀")

async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):

    await update.message.reply_text(f"Bạn nói: {update.message.text}")

app = Application.builder().token(TOKEN).build()

app.add_handler(CommandHandler("start", start))

app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))

app.run_polling()

Lưu file (Ctrl+O, Enter, Ctrl+X). Test bot:

python3 bot.py

Nếu thấy "Application started", bot đã chạy! Gửi tin nhắn cho bot trên Telegram để test.

Phần 3: Chạy Bot Vĩnh Viễn Với systemd

Khi chạy bot bằng python3 bot.py, bot sẽ tắt khi bạn đóng SSH. Để bot chạy 24/7, dùng systemd service:

Tạo Service File

nano /etc/systemd/system/telegram-bot.service

Dán nội dung sau:

[Unit]

Description=Telegram Bot Service

After=network.target

[Service]

Type=simple

User=root

WorkingDirectory=/root/mybot

Environment=PATH=/root/mybot/venv/bin

ExecStart=/root/mybot/venv/bin/python3 /root/mybot/bot.py

Restart=always

RestartSec=10

[Install]

WantedBy=multi-user.target

Kích Hoạt Service

systemctl daemon-reload

systemctl enable telegram-bot

systemctl start telegram-bot

systemctl status telegram-bot

Nếu thấy active (running) — bot đã chạy vĩnh viễn, tự động restart nếu crash, tự chạy lại khi reboot VPS!

Các Lệnh Quản Lý Bot

Phần 4: Bot Node.js (JavaScript)

Nếu bạn thích JavaScript hơn Python:

apt install -y nodejs npm

mkdir ~/mybot-js && cd ~/mybot-js

npm init -y && npm install node-telegram-bot-api

Tạo file bot.js:

const TelegramBot = require('node-telegram-bot-api');

const TOKEN = 'YOUR_TOKEN_HERE';

const bot = new TelegramBot(TOKEN, { polling: true });

bot.onText(/\/start/, (msg) => {

  bot.sendMessage(msg.chat.id, 'Chào bạn! Bot Node.js trên VPS TrumVPS 🚀');

});

bot.on('message', (msg) => {

  if (!msg.text.startsWith('/')) {

    bot.sendMessage(msg.chat.id, `Bạn nói: ${msg.text}`);

  }

});

Tạo systemd service tương tự, thay ExecStart thành /usr/bin/node /root/mybot-js/bot.js.

Phần 5: Bot Nâng Cao - Ý Tưởng Cho MMO & Dev

Với VPS, bạn có thể build những bot phức tạp hơn:

💡 Mẹo MMO: Chạy nhiều bot trên 1 VPS? Dùng Docker! Mỗi bot chạy trong container riêng, không xung đột thư viện, dễ scale. Xem bài hướng dẫn Cài Docker & Portainer trên VPS.

Giám Sát Bot Với PM2 (Thay Thế systemd)

PM2 là process manager cho Node.js/Python, cung cấp dashboard web, log viewer, auto-restart:

npm install -g pm2

pm2 start bot.py --interpreter python3 --name "telegram-bot"

pm2 save && pm2 startup

pm2 status — xem danh sách bot đang chạy

pm2 logs telegram-bot — xem log real-time

pm2 monit — dashboard CPU/RAM usage

Xử Lý Lỗi Thường Gặp

Cần VPS ổn định chạy bot 24/7?

TrumVPS - Uptime 99.9%, IP sạch, không chặn Telegram API - Chỉ từ 40K/tháng!

Mã giảm giá: 42089QPI03TI (-9%)

THUÊ VPS NGAY