Why Vercel Fails with Telegram Bots — And How I Fixed It

Telegram bots are powerful tools — but deploying them can be tricky, especially on platforms like Vercel. In this blog, I’ll walk you through the exact problem I faced, why it happened, what background workers are, and how I solved it using a smarter hosting approach.


๐Ÿšจ 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

PlatformFree PlanBackground Worker SupportResult
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:

js
const 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

  1. ๐Ÿšซ Tried Vercel

    • Bot crashed or timed out.

    • Learned it doesn't support long-running scripts.

  2. ๐Ÿ’ก Discovered Background Workers

    • Required for polling-based bots.

  3. ๐Ÿ”„ Moved to Render

    • On free plan: added minimal HTTP server.

    • Later upgraded to use background worker properly.

  4. ๐Ÿงช 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



Comments

Popular posts from this blog

How to Upload Your Local Project to GitHub Properly

YouTube Videos, Translation, AI Tools Aur Hindi Mein Dekhne Ka Experience

How to Build a Telegram Bot Using Node.js – Step-by-Step Guide