tocOn this page 7 sections
To host a Telegram bot 24/7 on a Windows VPS, run it in long polling mode, which needs no open ports or TLS certificate. Wrap it as a Windows service so it starts at boot, restarts after crashes and keeps running when you close your RDP window. Keep the bot token out of your code, write logs to disk, and make sure only one copy of the bot is ever polling.
Polling or webhook: decide first
Telegram delivers updates to a bot in one of two ways, and the choice shapes the rest of your setup.
- Long polling (getUpdates): your bot opens an outbound HTTPS connection to Telegram and waits for new messages. The VPS needs no inbound firewall rules, no domain and no certificate. This is the right default for most bots on a VPS.
- Webhook (setWebhook): Telegram pushes each update to a public HTTPS URL you control. This needs a domain, a valid certificate and an open port that Telegram accepts (443, 80, 88 or 8443). It suits high-traffic bots or bots that already sit behind IIS or another reverse proxy.
The two modes are mutually exclusive. While a webhook is registered, getUpdates calls fail, so if you switch from webhook to polling, call deleteWebhook first.
Check the token from PowerShell
Before you deploy any code, confirm the VPS can reach the Bot API with the token you got from @BotFather. Open PowerShell in your RDP session:
$token = Read-Host "Bot token"
Invoke-RestMethod "https://api.telegram.org/bot$token/getMe"
Invoke-RestMethod "https://api.telegram.org/bot$token/getWebhookInfo"
getMe should return your bot's username. getWebhookInfo should show an empty URL if you plan to use polling. If the first call times out, check outbound HTTPS from the VPS before you debug your bot code.
Lay out the bot on disk
Give the bot its own folder and its own runtime environment so updates to other software on the server can't break it. For a Python bot:
New-Item -ItemType Directory C:\Bots\tgbot, C:\Bots\tgbot\logs
cd C:\Bots\tgbot
py -m venv .venv
.\.venv\Scripts\pip install -r requirements.txt
Have the bot read its token from an environment variable such as TELEGRAM_BOT_TOKEN instead of a hard-coded string. Anyone holding the token controls the bot, so keep it out of source control and out of screenshots. Run the bot once in the foreground, send it a message from your phone, and stop it with Ctrl+C before you continue.
Run it as a Windows service
A bot started in an RDP console dies when that session logs off or the server reboots for updates. A service avoids both problems. NSSM (the Non-Sucking Service Manager) is the simplest way to turn a script into a service. Our guide to running a Python bot as a Windows service with NSSM covers installing it. With NSSM on your PATH, run this from an elevated PowerShell:
nssm install TelegramBot C:\Bots\tgbot\.venv\Scripts\python.exe bot.py
nssm set TelegramBot AppDirectory C:\Bots\tgbot
nssm set TelegramBot AppEnvironmentExtra TELEGRAM_BOT_TOKEN=123456:your-token
nssm set TelegramBot Start SERVICE_DELAYED_AUTO_START
nssm set TelegramBot AppExit Default Restart
nssm set TelegramBot AppRestartDelay 10000
nssm set TelegramBot AppStdout C:\Bots\tgbot\logs\bot.log
nssm set TelegramBot AppStderr C:\Bots\tgbot\logs\bot.log
nssm set TelegramBot AppRotateFiles 1
nssm set TelegramBot AppRotateBytes 10485760
nssm start TelegramBot
Delayed auto-start gives networking time to come up after a reboot. The restart delay stops a crashing bot from hammering the API in a tight loop. Log rotation keeps the disk from filling up. Node.js bots work the same way: point NSSM at node.exe and your entry script.
Check the result with Get-Service TelegramBot, then sign out of RDP (don't just close the window) and message the bot again. If it answers, it is independent of your session.
Use a low-privilege account
NSSM services run as LocalSystem by default, which is far more access than a chat bot needs. Create a dedicated local user and assign it in the service's Log On tab or with nssm set TelegramBot ObjectName. Grant that account access only to C:\Bots\tgbot. Then lock the folder down so other users can't read the stored token from the service registry entry or your config files.
Avoid the 409 Conflict error
The most common Telegram-specific failure is a 409 Conflict stating the request was terminated by another getUpdates request. It means two processes are polling with the same token. Typical causes:
- A test copy still running in an RDP console alongside the service.
- The same bot running on your laptop or a second server.
- A leftover webhook, which produces a similar conflict.
Find stray copies with Get-Process python, node -ErrorAction SilentlyContinue. Keep exactly one instance per token. Use a separate bot from @BotFather for development.
Keep it healthy long term
- Handle network errors in code. Wrap the polling loop so a timeout or brief outage is logged and retried rather than ending the process. NSSM is the safety net, not the plan.
- Persist state to disk. Store user settings and queues in SQLite or files, not memory, so a restart loses nothing.
- Patch on your schedule. Set Windows Update active hours, and confirm the service comes back after the next reboot.
- Harden the box. The token is only as safe as the server; work through our Windows Server VPS hardening checklist.
If you run several bots, the same pattern applies to each. For more ideas on what to run, see our page on Windows VPS for automation and bots.
FAQ
Do I need to open a firewall port for a Telegram bot?
Not with long polling. The bot only makes outbound HTTPS requests to Telegram. You need an inbound port, with a valid certificate, only if you register a webhook.
How much VPS do I need for a Telegram bot?
A simple bot uses very little CPU and memory, so a small plan is usually enough. Size up if the bot does heavy work such as image processing, AI calls with local models, or large databases.
Can I use Task Scheduler instead of a service?
Yes. A task set to run at startup whether or not a user is logged on will work. A service gives you cleaner automatic restarts and status checks, which is why we recommend it for a bot that must stay up 24/7.
Run this on your own Windows VPS
Get a Windows VPS from WindowsVPS in about 60 seconds, with full Administrator access over RDP, NVMe storage and the Windows license included.
Written by the WindowsVPS Team
We run Windows Server VPS hosting and write these guides from the same setups our customers use: RDP, PowerShell, trading terminals and always-on bots. Questions about a guide? Contact us.