Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequential execution of functions with node-telegram-bot-api

When I try to send information about my Age it is 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 to make it work like this: It should ask something. then if I answer something and the answer is not good, it should ask this question again, if the answer is good it should ask new questions using the same algorithm.

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,"Bot activated🤖" , { "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👶?");
like image 776
Ваня Н Avatar asked May 23 '26 20:05

Ваня Н


1 Answers

For sending Sequential Question:

var answerCallbacks = {};

bot.on('message', function (message) {
    var callback = answerCallbacks[message.chat.id];
    if (callback) {
        delete answerCallbacks[message.chat.id];
        return callback(message);
    }
});

bot.onText(/questions/, function (message,match) {
    bot.sendMessage(message.chat.id, "Enter your name").then(function () {
        answerCallbacks[message.chat.id] = function (answer) {
            var name = answer.text;
            bot.sendMessage(message.chat.id, "Enter your address").then(function () {
                answerCallbacks[message.chat.id] = function (answer) {
                    var address = answer.text;
                    bot.sendMessage(message.chat.id, "Enter your phone ").then(function () {
                        answerCallbacks[message.chat.id] = function (answer) {
                            var phone = answer.text;
                            bot.sendMessage(message.chat.id, name + address + phone + " saved!");
                        }
                    });
                }
            });
        }
    });
});
like image 134
Saeed Heidarizarei Avatar answered May 26 '26 10:05

Saeed Heidarizarei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!