3

So, i'm actually tired to find any asks about that...

I need to get user message only after bot question and nowhere else like:

bot: What is your name?

user: Oleg

bot: Hi, Oleg

how it should work

I am also using require system with module.exports, so i'm really confused, how to deal with my problem

EXAMPLE CODE

const mw = require('./example_module');

bot.onText(/\/help/, async (data) => {
    try {
        mw.cout.userlog(data);
        await cw.help.main(bot, data);
    } catch (e) {
        mw.cout.err(e.name)
    }
});
saykono
  • 31
  • 1
  • 2

1 Answers1

7

you can do that with a database or just a JSON file by storing a user state property. For example, here you are asking the user their name. And you can set the state property for the user in your DB that "setName". And when the user replies, check DB and find what was the last state. Here we have set the state to "setName". Then do the rest.

Otherwise, just with the node-telegram-bot-api, you can do this but a slight difference is that you have to receive their name as a reply text. Here's the code:

bot.onText(/\/help/, async msg => {
    const namePrompt = await bot.sendMessage(msg.chat.id, "Hi, what's your name?", {
        reply_markup: {
            force_reply: true,
        },
    });
    bot.onReplyToMessage(msg.chat.id, namePrompt.message_id, async (nameMsg) => {
        const name = nameMsg.text;
        // save name in DB if you want to ...
        await bot.sendMessage(msg.chat.id, `Hello ${name}!`);
    });
});

And that's it.

Sreelal TS
  • 912
  • 6
  • 11