Send message and receive other mesage couple times
Question
When I try to send unformation about my Age it asking my Gen and then my height. And if I'm writing my height it's asking my gen 2 times and 1 time my age. How make it works like this: It asking something i answer something, if answere do doesn't fit, it asking this question again, if answer fits it send new questions wich work with same algorithm.?
Code: let weight, height, age, dailyNorm, A, proteine, fat, glucide, gen; let low=1.2, small=1.4, middle=1.6, big=1.7;
const TelegramBot = require('node-telegram-bot-api'); const token = '734206894:... '; const bot = new TelegramBot(token, {polling: true, onlyFirstMatch:true, });
bot.onText(//start/, (msg) => { bot.sendMessage(msg.chat.id,"Бот активирован🤖" , { "reply_markup": { "keyboard":[["Calculate🍎" ], ["Report🐞"]] }}); console.log (msg.text); });
function dataGen(a){
bot.sendMessage(a.chat.id,"Your gen(👦-m,👧-f)?"); console.log (1)
bot.on("message", (msg) => {
if (msg.text=="m"){ gen="m"; dataAge(msg);}
else if (msg.text=="f"){ gen="f"; dataAge(msg);}
else { dataGen(msg);}
console.log (2)
});
}
function dataAge(b){ bot.sendMessage(b.chat.id,"Your age👶?"); bot.on("message" , (msg) => { if (msg.text>1 && msg.text<100){age=msg.text; dataHeight(msg);} else{ dataAge(msg)} }); }
function dataHeight(c){ bot.sendMessage(c.chat.id,"You height📏?"); bot.on("message" , (msg) => { if (msg.text>100 && msg.text<300){ height=msg.text; } else{ dataHeight(msg);} }); }
bot.onText(/Calculate🍎/, (msg) => { dataGen(msg); })
I think I have the same issue: https://hastebin.com/jurodiduqa.js
It haves something to do with the chatId. Would be nice to see it fixed.
For the original issue, @inikolaesku, the problem is that .on() does not get removed but is a static listener and it is not linked to one specific message. So you should set only one or multiple (with completely different purposes) .on("message").
For you, @RFlintstone, you are registering a new .onText() every time a user sends a message. Please note that a message listener captures any kind of message.
If you want to capture a specific message, you should set a global .onText(/yourregex/, () => {...}) for each message.
.onText(/yourregex/, () => {...})
I know that but each time the 'chatId' is activated it will execute the reaction one more in the next mention
Ex:
1)
User: /echo Hi
Bot: Hi
2)
User: /echo Hi
Bot: Hi
Bot: Hi
3)
User: just some text
4)
User: /echo Hi
Bot: Hi
Bot: Hi
Bot: Hi
Bot: Hi