简体   繁体   中英

How to execute same code for each user with node-telegram-bot-api?

To tell the truth I have no idea how to deal with the following issue.

Using telegram-node-bot-API I wrote a bot. The code is at the link: https://jsfiddle.net/gauo9p0q/

What I need now is:

When user A reaches the point where he is to share his phone I check if the phone is correct or not. If not ans user B start using the bot he gets the message that his phone is incorrect, nevertheless he haven't entered it so far. It happens because object order is the one for all users and the code is executed once for all the users what makes the part

else if (!re.test(msg.text) && order.status && msg.text != "Phone number" && msg.text != "Back" && order.type != "email") {
        bot.sendMessage(msg.chat.id, "Phone number is incorrect! Try again")
    }

be executed all the time even for user С.

How do I work with the whole my code and users A and B separately. In this specific case how do I create object order in a unique way for each user?

First temporarily remove all conditions and just use regex for validate phone number in a function and call that function.

 // THis is an example and you should customize that
bot.onText(/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/, (msg, match) => {

Extra Info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM