Why Vercel Fails with Telegram Bots — And How I Fixed It
- Get link
- X
- Other Apps
๐จ 1. The Problem with Vercel and Telegram Bots
๐ง A. How Vercel Works
-
Vercel is optimized for serverless web applications like Next.js, React, and static sites.
-
It runs short-lived serverless functions that start on HTTP requests and stop after a response.
-
Designed for websites and APIs — not long-running scripts.
๐ค B. How Telegram Bots Work
-
Bots typically use long polling — keeping a connection open with Telegram to wait for messages.
-
Requires a 24/7 running process — something Vercel doesn't allow.
๐ฅ C. The Core Problem
-
Vercel kills long-running processes — including your Telegram bot.
-
Symptoms you may encounter:
-
The bot doesn't respond at all.
-
Crashes after a few seconds.
-
Error messages like:
"background worker not supported" or
"no open ports detected."
-
๐งฐ 2. What is a Background Worker?
A background worker is a continuously running process used for tasks like:
-
Listening for Telegram messages
-
Running CRON jobs
-
Performing background calculations
Unlike web services, background workers don’t depend on HTTP requests.
✅ Supported by platforms like:
-
Render (paid plans)
-
Railway
-
Fly.io
๐ธ 3. Why Free Hosting Plans Fail
| Platform | Free Plan | Background Worker Support | Result |
|---|---|---|---|
| Vercel | ✅ Yes | ❌ No | ❌ Bot fails to run |
| Render | ✅ Yes | ❌ (Web only) | ⚠️ Workaround needed |
| Render (Paid) | ๐ฐ Yes | ✅ Yes | ✅ Best Option |
| Railway | ✅ Yes | ✅ Yes | ✅ Recommended |
| Fly.io | ✅ Yes | ✅ Yes | ✅ Recommended๐ ️ |
4. How I Fixed It
✅ A. Switched to a Host That Supports Background Workers
I moved the bot to Render (alternatively Railway or Fly.io) to run the polling logic 24/7.
๐งช B. Render (Free Plan) Hack: Add a Minimal HTTP Server
Since Render free plan only supports web services, I added a tiny Express.js server like this:
jsconst express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Bot is running'));
app.listen(process.env.PORT || 3000);
This keeps the service alive by “fooling” Render into treating the bot like a web app.
๐ฐ C. Upgraded to a Paid Plan (Optional)
-
Upgrading on Render allowed me to run the bot script as a background worker directly.
-
No HTTP server needed — just run
node bot.js.
๐ช 5. My Step-by-Step Journey
-
๐ซ Tried Vercel
-
Bot crashed or timed out.
-
Learned it doesn't support long-running scripts.
-
-
๐ก Discovered Background Workers
-
Required for polling-based bots.
-
-
๐ Moved to Render
-
On free plan: added minimal HTTP server.
-
Later upgraded to use background worker properly.
-
-
๐งช Tested on Railway and Fly.io
-
Both support background workers well — even on free tiers (with usage limits).
-
๐ 6. Key Takeaways
-
Vercel ≠ good for polling-based bots
-
Polling bots need background workers, not serverless functions.
-
Render free plan hack: Use an Express server to keep the app alive.
-
Railway/Fly.io = easier and cleaner setup.
๐ Useful References
- Get link
- X
- Other Apps
Comments
Post a Comment