V
V
Vladislav2021-07-23 10:09:33
JavaScript
Vladislav, 2021-07-23 10:09:33

What is the correct code architecture for Telegram bot on node-telegram-bot-api?

Hello! Google searched, but did not find a similar solution for the telegram bot architecture written on the node-telegram-bot-api library.

The bottom line is that 90% of my code is in one index.js file (I can only take out variables with values ​​and external wrapper functions) and when your application starts to exceed 2 thousand lines, it becomes costly to swipe up and down in length party girl.

The problem is that all commands or functions related to the bot (begin with the bot class nested with any of its methods), such as:

bot.on('message', msg => {
    const chatId = msg.chat.id
    switch (msg.text) {
        case kb.home.projects:
            bot.sendMessage(chatId, command_text__projects, {
                reply_markup: {
                    keyboard: keyboard.projects,
                    resize_keyboard: true,
                    one_time_keyboard: true
                }
            })
            break

or
bot.onText(/\/start/, msg => {
    const chatId = msg.chat.id
    if (msg.chat.username !== 'AveCardinal' ) {
        bot.sendMessage(chatId, command_text__start__error)
    } else if (msg.chat.username === 'AveCardinal') {
        bot.sendMessage(chatId, command_text__start_success, {
            reply_markup: {
                keyboard: keyboard.home,
                resize_keyboard: true
            }
        })
    }
})
are tied to the bot class, and even if you put the creation of the bot class in a separate file and do require in each of the separate files that contain the business logic associated with the methods of the bot class, then in the end the application does not work, because the code is broken, but not connected in one shared file.

Question: how to put the business logic of a separate bot.on block or any other method of the bot class into a separate file, and then combine all the code blocks into one file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2021-07-23
@vshvydky

the fact that the bot has a single entry point is normal, since you have one listener of one type of event.
for myself, when creating a similar project, I used the ideas of an http server with a route, middleware and handler handler.
In fact, the entire code is decomposed into handler files that are mapped to certain commands, passing between the route and the handler through security middleware, the
final picture can be as follows:
1. you write an engine that reproduces the processing of all logic for an event from a listener.
2. describe the route map from the series route.add("/start", startHandler), route.add("/get_price", authMW, getPriceHandler)
3. describe the handlers themselves, where you define the sufficient amount of parameters included in the function for your bot to work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question