Telegram bot API creating double messages when sending notifications

I have a function that sends notifications through Telegram bot API, but I keep getting duplicate messages showing up in the chat. This happens randomly, not every time I send a message.

Here’s my current code:

export async function sendTelegramNotification(messageText: string) {
    const telegramBot = new TelegramBot(BOT_API_KEY, {polling: false});
    const targetChatId = parseInt(CHAT_ROOM_ID);
    
    try {
        await telegramBot.sendMessage(targetChatId, messageText, {parse_mode: 'Markdown'});
    } catch (err) {
        console.error(`Telegram error: ${err}`);
    }
}

I looked through the official docs but couldn’t find anything about this duplicate message issue. Has anyone experienced this before? What could be causing the bot to send the same message twice?

this looks like a race condition. if you’re hitting this function multiple times fast, telegram’s probably queuing them weird and processing duplicates. add a flag or mutex before sending - i just use a set to track recent messages and skip duplicates. also double-check whatever’s calling this function - might be firing twice somewhere.

This usually happens because of network timeouts or retry mechanisms. The Telegram API doesn’t respond fast enough, so your app thinks the request failed and automatically retries. I’ve seen this a lot with serverless functions or bad network connections. Add a longer timeout to your bot config and set up error handling that can tell the difference between real failures and slow responses. Check if you’ve got middleware or frameworks automatically retrying failed requests too. Setting {request: {timeout: 30000}} in your bot options usually fixes it.

Had this exact issue six months ago with my notification system. Problem was I kept creating new TelegramBot instances every time the function ran - that causes conflicts when multiple requests hit at once. Create one bot instance outside your function and reuse it instead. Also check if your function’s getting triggered multiple times. My webhook was firing twice because of a misconfigured endpoint. Adding simple deduplication with message timestamps or unique IDs also helped - stops identical messages from going out within a short window.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.