J
J
jenya77712018-05-24 10:45:16
Node.js
jenya7771, 2018-05-24 10:45:16

Correct bot example on node Telegram bot api?

Hello, I am writing a bot on nodeJs, and the question arose of how to store and process a message chain, for example, a user presses the button to assemble a car and I ask him to select a color, select a type, and so on. Where is it more correct to store the answer to each question and how to process it all?
I did like this, but as I understand it, it is completely wrong. Thank you.

bot.on("callback_query", function (msg) {

    if (callback === 'startSearch') {

      bot.sendMessage(id, "Первый вопрос: ", {
        reply_markup: {
          one_time_keyboard: true,
          resize_keyboard: true,
          keyboard: servises.telegram.keyboard(CITY, 3, false)
        }
      })
      .then(res => {


        servises.redis.updateObject(`user_${id}`, [
        {
          field: 'search',
          value: {
            level: 'startSearch'
          }
        }
        ])
        .catch(error => {
          console.log(error)
        })

        bot.answerCallbackQuery({
          callback_query_id: msg.id,
          text: "Поиск начался",
        });
      })
    }
})

And after when the user writes a message, I see if there is a mark in the radish that now you need to accept any answer to the question from the user
bot.on('message', (msg) => {

  const message = msg.text;

  if (message === 'roomType') {

    if (ROOM_TYPE.indexOf(message) != -1) {

      bot.sendMessage(id, "Второй вопрос: ", {
        reply_markup: {
          resize_keyboard: true,
          one_time_keyboard: true,
          keyboard: servises.telegram.keyboard(LEASE_TERM, 2, false)
        }
      })
      .then(res => {

        user.level = 'leaseTerm';
        user.roomType = message;

        servises.redis.updateObject(`user_${id}`, [
        {
          field: 'search',
          value: user
        }
        ])
        .catch(error => {
          console.log(error)
        })
      })
    } else {

      bot.sendMessage(id, "Такого варианта нет, выберите из предложеных:")
    }
  }
})

Can eat examples of implementation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2018-05-24
@jenya7771

Read about finite state machines. In short, you must store the user's state somewhere (analogous to sessions), and depending on the specific session, expect a specific data type.
They already threw off an example from the telegraph, it is well implemented there.
Z.Y. Throw out node-telegram-bot-api already in the trash)

A
Alexander Taratin, 2018-05-24
@Taraflex

https://github.com/telegraf/telegraf/blob/develop/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question