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.
Gordon Nicholas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.