1

i`m trying to make telegram bot and dont understand why it works so. I have three js files index, quiz_creation, question_creation here is content index.js

const createQuiz = require('./quiz_creation');
bot.onText(/\/start/, (msg) => {
  const chatId = msg.chat.id;
  const options = {
    reply_markup: {
        inline_keyboard: [
            [{ text: 'Create Quiz!', callback_data: 'create' }],
            [{ text: 'Start Quiz', callback_data: 'start' }],
        ]
    }
  };
  bot.sendMessage(chatId, 'Welcome to the quizzzo bot!', options);
});

bot.on('callback_query', (query) => {
  const chatId = query.message.chat.id;
  const data = query.data;

  if (data === 'create') {
    createQuiz(bot, chatId);
  } else if (data === 'start') {
    //startQuiz(bot, chatId);
  }
});

quiz_creation.js:

const createQuestion = require('./question_creation');


const createQuiz = (bot, chatId) => {
  bot.sendMessage(chatId, 'What do you want to create?',{
    reply_markup: {
      inline_keyboard: [
        [{ text: 'By Date', callback_data: 'create_date' }],
        [{ text: 'By Number of Users', callback_data: 'create_users' }],
      ]
    }
  });
  
  bot.on('callback_query', (query) => {
    const chatId = query.message.chat.id;
    const data = query.data;
    bot.answerCallbackQuery(query.id)
  
    if (data === 'create_date') {
          
    } else if (data === 'create_users') {
          const message = 'Please send me the number of users for the quiz';
        bot.sendMessage(chatId, message, {reply_markup: {force_reply: true}}).then((msg) => {
          bot.onReplyToMessage(msg.chat.id, msg.message_id, (message) => {
            const number = Number(message.text);
            if (!Number.isNaN(number)) {
              bot.sendMessage(chatId, `Quiz will have ${number} users`);
                      createQuestion(bot, chatId);
              
            } else {
              bot.sendMessage(chatId, 'Invalid number format, please send a number');
            }
          });
        });
    } else if (data === 'question_one_from_some' || data === 'question_text_answer') {
    }

  });
};

module.exports = createQuiz;

and question_creation.js

const createQuestion = (bot, chatId) => {
    bot.on('callback_query', (query) => {

        const chatId = query.message.chat.id;
        const questionType = query.data;
        bot.answerCallbackQuery(query.id)
        if (questionType === 'question_one_from_some') {
            bot.sendMessage(chatId, 'Please send me the question', {reply_markup: {force_reply: true}}).then((msg) => {
                
            });
        } else if (questionType === 'question_text_answer') {
            
        }

      })
};

module.exports = createQuestion;

After first run messages from bot send twice and more more more... I understand that it happens cause bot.on fires for every time i press inline button but how to fix it? I tried remove listener for callback query but then inline buttons stop working at all

leshkacho
  • 43
  • 3

0 Answers0