'Problem in Telegram bot logic on node-telegram-bot-api
Just starting to learn JS and NodeJs in general. I’m writing a TG bot just as a project to gain knowledge and experience in JS, in general, that’s not the point! Faced such a problem in the event handler: I have something like a registration window with keyboard_inline, where when the /start command comes out several buttons with a gender selection
bot.onText(/\/start/, async msg => { db.get("SELECT * FROM users WHERE user_id = ?", [msg.chat.id], async (no, yes) => { yes ? console.log('ss') : await bot.sendMessage(msg.chat.id, msg.chat.first_name + ',' + ' ' + 'добро пожаловать! 🙈\n\n' + 'Вы ещё у нас не зарегистрированы! Пожалуйста, выберите свой пол для продолжения регистрации:', keyboardName(signup)); }); });
There is a database, but this is not the point, the check is disabled there, it does not affect it in any way. But the actual call to the keyboardName function, made it to simplify the readability of the code
const keyboardName = (name) => {
return {
reply_markup: JSON.stringify ({
inline_keyboard: name
})
}
};
Here are the buttons:
const signup = [
[
{
text: '👨 Я парень',
callback_data: 'signup_man'
},
{
text: '👩 Я девушка',
callback_data: 'signup_woman'
}
]
];
And here is the button click event handler itself:
bot.on('callback_query', async query => {
switch (query.data) {
case 'signup_man':
await bot.sendMessage(query.message.chat.id, 'Введи возраст от 16 до 90');
bot.on('message', async msg => {
if (msg.text >= 1 && msg.text <= 90) {
return await bot.sendMessage(msg.chat.id, 'это тестовое сообщение');
} else
bot.sendMessage(msg.chat.id, 'Возраст от 16 до 90');
})
break;
case 'signup_woman':
bot.sendMessage(msg.chat.id, 'С тобой приятно иметь дело! А теперь введи свой возраст, просто в чат, цифрами :)');
break;
}
});
The problem is this: when you use the bot from one account, then all the logic and checks work properly, but if I launch several more accounts at the same time, then in the last paragraph, when entering the age of the user (chat.id), several messages will be sent to the chat ( commensurate with the number of accounts with a running bot and clicking on the button). I would like to understand what is wrong and why it happens)
Tried a lot of things, but I don't seem to understand the logic
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
