Telegram Bot Responds Slowly on Initial /start Command – Deployed on Vercel

I have a Telegram bot built using Node.js, Telegraf, and deployed on Vercel. The bot implements a referral system where it checks if a user is registered in the backend. If the user is not registered, it registers them as a referral and proceeds with the onboarding process.

However, I am facing an issue where the bot responds slowly when I first use the /start command (especially after no actions have been performed for a long time). Subsequent commands work fine and are fast. Here’s the behavior:

  • Slow response on the first command after inactivity.
  • Once the bot responds, subsequent actions are quick and responsive.
  • If the bot responds slowly and the user gets registered during this time, the referral system fails to record the referral properly since the user is already registered by the time the bot processes the request.

The overall referral system and bot logic work well once the delay issue is overcome. But this initial response delay is causing problems with referral registration.

**Here’s my implementation:
**

  • I am using Telegraf to handle bot commands.
  • Deployed on Vercel.
  • Referral system checks if the user exists in the backend via an API call.
  • On initial start after inactivity, there’s a noticeable lag before the bot responds to /start.
const { Telegraf } = require("telegraf");
const axios = require("axios");
const dotenv = require("dotenv");
dotenv.config();
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);

bot.start(async (ctx) => {
  const referrerID = ctx.startPayload;
  const tgUserID = ctx.from.id.toString(); 

  const tgUsername =
    ctx.from.username && ctx.from.username.trim() !== ""
      ? ctx.from.username
      : "Unknown";
  const isPremium = ctx.from.is_premium ? true : false; 

  if (!referrerID) {
    await ctx.replyWithPhoto(
      { url: "https://i.postimg.cc" },
      {
        caption: `
🚀 **Be a part of our Club** 🚀
        `,
        parse_mode: "Markdown",
        reply_markup: {
          inline_keyboard: [
            [{ text: "Subscribe Channel", url: "https://t.me/<name>" }],
            [
              {
                text: "Launch Mini App",
                web_app: { url: "https://frontend.vercel.app" },
              },
            ],
          ],
        },
      }
    );
    return;
  }

  try {
    const userResponse = await axios.get(
      `${process.env.BACKEND_URL}/user/${tgUserID}`
    );

    if (!userResponse.data) {
      try {
        await axios.put(
          `${process.env.BACKEND_URL}/register`,
          {
            referredID: tgUserID,
            isPremium: isPremium,
            username: tgUsername,
          }
        );
      } catch (referralError) {}
    }
  } catch (error) {
    if (error.response && error.response.status === 404) {
      try {
        await axios.put(
          `${process.env.BACKEND_URL}/referrals`,
          {
            referredID: tgUserID,
            isPremium: isPremium,
            username: tgUsername,
          }
        );
      } catch (referralError) {}
    }
  }

  await ctx.replyWithPhoto(
    {
      url: "https://i.postimg.cc",
    },
    {
      caption: `
🚀 **Be a part of our Club*** 🚀
      `,
      parse_mode: "Markdown",
      reply_markup: {
        inline_keyboard: [
          [{ text: "Subscribe Channel", url: "https://t.me" }],
          [
            {
              text: "Launch Mini App",
              web_app: { url: "https://frontend.vercel.app" },
            },
          ],
        ],
      },
    }
  );
});

const startBot = async (app) => {
  try {
    const webhookInfo = await bot.telegram.getWebhookInfo();
    if (!webhookInfo.url) {
      await bot.telegram.setWebhook(
        `${process.env.WEBHOOK_URL}/bot${process.env.TELEGRAM_BOT_TOKEN}`
      );
    }

    app.post(`/bot${process.env.TELEGRAM_BOT_TOKEN}`, (req, res) => {
      bot.handleUpdate(req.body, res);
    });
  } catch (error) {}
};

module.exports = startBot;

I deployed a Telegram bot using Node.js and Telegraf on Vercel. The bot implements a referral system, where it checks if a user is already registered in the backend. If not, the bot registers them as a referral only if param from telegram is valid.

I was expecting the bot to respond immediately when a user sends the /start command and properly register referrals. However, I am experiencing a significant delay the first time the bot is triggered, especially after a long period of inactivity. The referral system doesn’t work correctly if the user gets registered faster than the bot responds to the /start command. After the first response, the bot works fine for all subsequent commands.

New contributor

Gordon Nicholas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Telegram Bot Responds Slowly on Initial /start Command – Deployed on Vercel

I have a Telegram bot built using Node.js, Telegraf, and deployed on Vercel. The bot implements a referral system where it checks if a user is registered in the backend. If the user is not registered, it registers them as a referral and proceeds with the onboarding process.

However, I am facing an issue where the bot responds slowly when I first use the /start command (especially after no actions have been performed for a long time). Subsequent commands work fine and are fast. Here’s the behavior:

  • Slow response on the first command after inactivity.
  • Once the bot responds, subsequent actions are quick and responsive.
  • If the bot responds slowly and the user gets registered during this time, the referral system fails to record the referral properly since the user is already registered by the time the bot processes the request.

The overall referral system and bot logic work well once the delay issue is overcome. But this initial response delay is causing problems with referral registration.

**Here’s my implementation:
**

  • I am using Telegraf to handle bot commands.
  • Deployed on Vercel.
  • Referral system checks if the user exists in the backend via an API call.
  • On initial start after inactivity, there’s a noticeable lag before the bot responds to /start.
const { Telegraf } = require("telegraf");
const axios = require("axios");
const dotenv = require("dotenv");
dotenv.config();
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);

bot.start(async (ctx) => {
  const referrerID = ctx.startPayload;
  const tgUserID = ctx.from.id.toString(); 

  const tgUsername =
    ctx.from.username && ctx.from.username.trim() !== ""
      ? ctx.from.username
      : "Unknown";
  const isPremium = ctx.from.is_premium ? true : false; 

  if (!referrerID) {
    await ctx.replyWithPhoto(
      { url: "https://i.postimg.cc" },
      {
        caption: `
🚀 **Be a part of our Club** 🚀
        `,
        parse_mode: "Markdown",
        reply_markup: {
          inline_keyboard: [
            [{ text: "Subscribe Channel", url: "https://t.me/<name>" }],
            [
              {
                text: "Launch Mini App",
                web_app: { url: "https://frontend.vercel.app" },
              },
            ],
          ],
        },
      }
    );
    return;
  }

  try {
    const userResponse = await axios.get(
      `${process.env.BACKEND_URL}/user/${tgUserID}`
    );

    if (!userResponse.data) {
      try {
        await axios.put(
          `${process.env.BACKEND_URL}/register`,
          {
            referredID: tgUserID,
            isPremium: isPremium,
            username: tgUsername,
          }
        );
      } catch (referralError) {}
    }
  } catch (error) {
    if (error.response && error.response.status === 404) {
      try {
        await axios.put(
          `${process.env.BACKEND_URL}/referrals`,
          {
            referredID: tgUserID,
            isPremium: isPremium,
            username: tgUsername,
          }
        );
      } catch (referralError) {}
    }
  }

  await ctx.replyWithPhoto(
    {
      url: "https://i.postimg.cc",
    },
    {
      caption: `
🚀 **Be a part of our Club*** 🚀
      `,
      parse_mode: "Markdown",
      reply_markup: {
        inline_keyboard: [
          [{ text: "Subscribe Channel", url: "https://t.me" }],
          [
            {
              text: "Launch Mini App",
              web_app: { url: "https://frontend.vercel.app" },
            },
          ],
        ],
      },
    }
  );
});

const startBot = async (app) => {
  try {
    const webhookInfo = await bot.telegram.getWebhookInfo();
    if (!webhookInfo.url) {
      await bot.telegram.setWebhook(
        `${process.env.WEBHOOK_URL}/bot${process.env.TELEGRAM_BOT_TOKEN}`
      );
    }

    app.post(`/bot${process.env.TELEGRAM_BOT_TOKEN}`, (req, res) => {
      bot.handleUpdate(req.body, res);
    });
  } catch (error) {}
};

module.exports = startBot;

I deployed a Telegram bot using Node.js and Telegraf on Vercel. The bot implements a referral system, where it checks if a user is already registered in the backend. If not, the bot registers them as a referral only if param from telegram is valid.

I was expecting the bot to respond immediately when a user sends the /start command and properly register referrals. However, I am experiencing a significant delay the first time the bot is triggered, especially after a long period of inactivity. The referral system doesn’t work correctly if the user gets registered faster than the bot responds to the /start command. After the first response, the bot works fine for all subsequent commands.

New contributor

Gordon Nicholas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật