Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Empty Message Error in Telegram Quiz Bot When Sending Questions #768

Open
Azizjon003 opened this issue Jan 11, 2025 · 0 comments
Open

Comments

@Azizjon003
Copy link

Azizjon003 commented Jan 11, 2025

Bug Description

When sending quiz questions to QuizBot, sometimes the function throws an error: "The message cannot be empty unless a file is provided"

Current Behavior

  • Function fails with error: "Error sending quiz: Error: The message cannot be empty"
  • Quiz sending process stops
  • Question object is logged but message isn't sent

Steps to Reproduce

  1. Load questions from JSON file
  2. Start sending quiz questions to QuizBot
  3. Error occurs when trying to send specific questions

Error Log

Error sending quiz: Error: The message cannot be empty unless a file is provided
Question index: 1
Question: {
  index: 2,
  question: "Tashkilot tarmoq resurslaridan...",
  answers: [...],
  correct_index: 0,
  type: 'short'
}

code


async function sendQuiz(client, index) {
  const question = questions[index];
  try {
    if (!question || !question.question || !question.answers) {
      console.error("Invalid question format at index:", index);
      return;
    }
    if (question.type === "long") {
      const questionText = html.escape(question.question);
      let answers = [...question.answers];
      answers = shuffleArray(answers);
      const correctAnswer = question.answers[question.correct_index];
      const correctIndex = answers.indexOf(correctAnswer);
      answers = answers.map((answer) => html.escape(answer));

      await client.sendMessage("QuizBot", {
        message:
          <b>${questionText}</b>\n\n +
          A) ${answers[0]}\n +
          B) ${answers[1]}\n +
          C) ${answers[2]}\n +
          D) ${answers[3]}\n\n +
          ${COPYRIGHT},
        parse_mode: "HTML",
      });

      await sleep(400);

      await client.sendMessage("QuizBot", {
        poll: {
          question: "Javob:",
          answers: ["A", "B", "C", "D"],
          correct_option_id: correctIndex,
          type: "quiz",
          is_anonymous: false,
          explanation: COPYRIGHT,
          explanation_parse_mode: "HTML",
        },
      });
    } else {
      if (
        !Array.isArray(question.answers) ||
        question.correct_index === undefined
      ) {
        console.error("Missing required question data at index:", index);
        return;
      }
      await client.sendMessage("QuizBot", {
        poll: {
          question: question.question,
          answers: question.answers,
          correct_option_id: question.correct_index,
          type: "quiz",
          is_anonymous: false,
          explanation: COPYRIGHT,
          explanation_parse_mode: "HTML",
        },
      });
    }
    await sleep(500);
  } catch (err) {
    if (err.name === "FloodWaitError") {
      console.log(FloodWait error: waiting ${err.seconds} seconds);
      await sleep(err.seconds * 1000);
      await sendQuiz(client, index);
    } else {
      console.error("Error sending quiz:", err);
      console.log("Question index:", index);
      console.log("Question:", question);
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant