yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.31k stars 1.51k forks source link

If multiple users responding to callback_query at same moment, all of them will receive the result of first user who responded #916

Closed alexHlebnikov closed 2 years ago

alexHlebnikov commented 2 years ago

Hi. I'm implementing bot behaviour according to user actions. I faced this problem: if multiple users responding to callback_query at the same moment, all of them will receive the result of first user who responded. Example:

 await bot.getUpdates();
if (text === '/top') {
    return await printTop(chatId);
}
async function printTop(chatId: string) {
    if (!chatId) {
        throw new Error(`Error: chatId = ${chatId}`);
    }

    await bot.sendMessage(chatId, 'some info');

    return sendInfo(chatId);
}
const sendInfo = async (chatId: string) => {
    const topData = await getTop();
    await bot.sendMessage(chatId, `choose:`, {
        reply_markup: {
            inline_keyboard: topData,
        },
    });
};
bot.on('callback_query', async (msg) => {
        const { data } = msg;
        const chatId = msg.message?.chat.id.toString();

        try {
            if (!chatId || !data) {
                logger.error(`Error: chatId = ${chatId}, data = ${data} `);
                return;
            }

                    return await printSelected();
        } catch (e) {
            logger.error(`error`, e);
        }
    });

At the moment the first user clicks on inline_keyboard option by sendInfo(), all other users who clicked the same inline_keyboard at the same moment of time, but selected different option, will see in they TG bot reply option, selected by the first user.

Why is this happening and how to prevent this? I need to send to each user inline_keyboard option selected by that user and not by the first user who clicked.

alexHlebnikov commented 2 years ago

I figured out myself, closing the issue.