alexandercerutti/node-telegram-keyboard-wrapper

Handling multiple chats?

timothyylim opened this issue ยท 3 comments

Thanks so much for this package! It's saved me :)

If I had two chats going on at the same time and I wanted to close a reply keyboard for just one of them, how could I do that?

Thanks

Hi! I'm really glad this helped you!

For the answer below, I'm assuming you are using yagop's node-telegram-bot-api.

You have to reply in the chat you want to close the keyboard and pass as last argument to sendMessage(), ReplyKeyboard.close() method (you need to have an instance of ReplyKeyboard).

You can take a look at the example bot Line 55 in example folders. ๐Ÿ˜Š

But does that mean I have to create a new instance of ReplyKeyboard for each different chat?

Yeah, about. You should have "common processes" for 1-on-1 chats. I mean, the keyboard instance should be created once for chat or once for all (so the keyboard structure is loaded once in your application). Since you bot will be able to have multiple chats at the same time, you know that each chat will follow a specific process, which should be equal but not the same of the other chat.

I.e. all the chats execute a /command (the same).
Chat 1 --> Step 1 โœ” --> Step 2 โœ” --> Step 3 โœ” --> Step 4 ๐Ÿ”˜
Chat 2 --> Step 1 โœ” --> Step 2 ๐Ÿ”˜
Chat 3 --> Step 1 โœ” --> Step 2a โœ” --> Step 3a ๐Ÿ”˜

Your chat may follow the same process (Chat 1 and Chat 2), but be at a different point, or diverge (Chat 3 from Chat 2 and Chat 1).

If you want to close your keyboard, you will presumably close it at Step 4 or, anyway, at the end of a process part which requires a keyboard to be used.

So, yes. In your Node application, you will have to define a keyboard in a global (or module) scope, to be used whenever you need it (e.g. each chat Step 2) or define and populate it once a chat arrives at step 2.

Still assuming you are using node-telegram-bot-api by yagop, these are the codes of the two cases described above:

const replykb = new ReplyKeyboard();
// ... populate keyboard

bot.onText(/\/summonKeyboard/, (msg) => {
    // opening the keyboard
    // choose which action to take based on conditions, such as a user is still interacting with bot
    bot.sendMessage(msg.from.id, "bla bla bla", replykb.open());
});

Or

bot.onText(/\/summonKeyboard/, (msg) => {
    const replykb = new ReplyKeyboard();
    // ... populate keyboard


    // opening the keyboard
    // choose which action to take based on conditions, such as a user is still interacting with bot
    bot.sendMessage(msg.from.id, "bla bla bla", replykb.open());
});

Your choice.

If you are going to handle, instead, reply keyboards within groups, you may need { selective: true } in your replykb.open(). If interested, I invite you to read about it on Telegram bot api doc