Currently delving into the world of JS and NodeJs to broaden my knowledge and gain experience. Working on a Telegram bot project as a way to practice and learn more about JavaScript, particularly focusing on event handling aspects. Encountered an issue with the event handler: I have a registration window with inline keyboard functionality triggered by the /start command which displays several buttons for 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' + 'You are not registered yet! Please select your gender to proceed with registration:', keyboardName(signup)); }); });
The database is in place, but the focus here is on the keyboard implementation for code readability purposes:
const keyboardName = (name) => {
return {
reply_markup: JSON.stringify ({
inline_keyboard: name
})
}
};
Here are the gender selection buttons:
const signup = [
[
{
text: '👨 I am a guy',
callback_data: 'signup_man'
},
{
text: '👩 I am a girl',
callback_data: 'signup_woman'
}
]
];
And here is the button click event handler:
bot.on('callback_query', async query => {
switch (query.data) {
case 'signup_man':
await bot.sendMessage(query.message.chat.id, 'Enter your age from 16 to 90');
bot.on('message', async msg => {
if (msg.text >= 1 && msg.text <= 90) {
return await bot.sendMessage(msg.chat.id, 'This is a test message');
} else
bot.sendMessage(msg.chat.id, 'Age must be between 16 and 90');
})
break;
case 'signup_woman':
bot.sendMessage(msg.chat.id, 'Nice dealing with you! Now, please enter your age in digits in the chat :)');
break;
}
});
The issue arises when using the bot with multiple accounts simultaneously. When entering the age (chat.id) during the signup process, multiple messages are sent to the chat, corresponding to the number of active accounts. Seeking insight into why this occurs and what might be causing it.
Tried various troubleshooting steps, but struggling to grasp the underlying logic behind this behavior.