Answer the question
In order to leave comments, you need to log in
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: "Поиск начался",
});
})
}
})
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, "Такого варианта нет, выберите из предложеных:")
}
}
})
Answer the question
In order to leave comments, you need to log in
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question