Skip to content

Commit

Permalink
Refactoring sendQuizStat in BotUpdate so that it not send 2 quizStat,
Browse files Browse the repository at this point in the history
improve handling topicChoice tag
and create logger in QuizBotExceptionHandler
  • Loading branch information
XD-cods committed Mar 21, 2024
1 parent 3edb193 commit 59105cf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
5 changes: 1 addition & 4 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 29 additions & 17 deletions src/main/java/org/example/BotUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public int process(List<Update> updates) throws NullPointerException {
bot.execute(new SendMessage(chatId, "Please finish this quiz"));
break;
}
if(userInfo.isTopicChosen()){
if (userInfo.isTopicChosen()) {
bot.execute(new SendMessage(chatId, "You are not chosen quiz"));
break;
}
Expand All @@ -83,7 +83,7 @@ public int process(List<Update> updates) throws NullPointerException {
bot.execute(new SendMessage(chatId, "Topic is not chosen, please use /choice command to choose"));
break;
}
if(userInfo.isTopicChosen()){
if (userInfo.isTopicChosen()) {
bot.execute(new SendMessage(chatId, "You are not chosen quiz"));
break;
}
Expand Down Expand Up @@ -145,29 +145,40 @@ private void sendQuizStats(Long chatId) {
UserQuizSession userQuizSession = users.get(chatId).getUserQuizSession();
int quizAmount = userQuizSession.getQuestionAmount();
int rightAnswerCounter = userQuizSession.getRightAnswerCounter();
String statMessageText = String.format("❓ <b>Question number:</b> %d\n\n✅ <b>Right answers:</b> %d\\%d\n\n",
quizAmount, rightAnswerCounter, quizAmount);
statMessageText += "Input /start_quiz to reset bot or chose quiz another quiz /choice";
String statMessageText = getQuizStatText(quizAmount, rightAnswerCounter);
userQuizSession.setQuizMode(false);
SendMessage statMessage = new SendMessage(chatId, statMessageText).parseMode(ParseMode.HTML);
bot.execute(statMessage);
users.get(chatId).setLastBotMessage(bot.execute(statMessage).message());
Message lastBotMessage = bot.execute(statMessage).message();
users.get(chatId).setLastBotMessage(lastBotMessage);
users.get(chatId).setUserQuizSession(null);
}

private void sendStatsCanceledQuiz(Long chatId){
private static String getQuizStatText(int quizAmount, int rightAnswerCounter) {
return String.format("❓ <b>Question number:</b> %d\n\n" +
"✅ <b>Right answers:</b> %d\\%d\n\n" +
"Input /start_quiz to reset bot or chose quiz another quiz /choice",
quizAmount, rightAnswerCounter, quizAmount);
}

private void sendStatsCanceledQuiz(Long chatId) {
UserQuizSession userQuizSession = users.get(chatId).getUserQuizSession();
int questionCount = userQuizSession.getQuestionCounter();
int rightAnswerCounter = userQuizSession.getRightAnswerCounter();
String statMessageText = String.format("❓<b>You canceled quiz</b>\n\n<b>The questions were:</b> %d\n\n✅ <b>Right answers:</b> %d\\%d\n\n",
questionCount, rightAnswerCounter, questionCount);
statMessageText += "Input /start_quiz to reset bot or chose quiz another quiz /choice";
String statMessageText = getCanceledQuizStatText(questionCount, rightAnswerCounter);
userQuizSession.setQuizMode(false);
SendMessage statMessage = new SendMessage(chatId, statMessageText).parseMode(ParseMode.HTML);
users.get(chatId).setLastBotMessage(bot.execute(statMessage).message());
users.get(chatId).setUserQuizSession(null);
}

private static String getCanceledQuizStatText(int questionCount, int rightAnswerCounter) {
String statMessageText = String.format("❓<b>You canceled quiz</b>\n\n<b>The questions were:</b> %d\n\n" +
"✅ <b>Right answers:</b> %d\\%d\n\n" +
"Input /start_quiz to reset bot or chose quiz another quiz /choice",
questionCount, rightAnswerCounter, questionCount);
return statMessageText;
}

private int handleCallback(CallbackQuery callbackQuery) {
Long userId = callbackQuery.from().id();
if (!isRegisterUser(userId)) {
Expand All @@ -180,9 +191,8 @@ private int handleCallback(CallbackQuery callbackQuery) {
return UpdatesListener.CONFIRMED_UPDATES_NONE;
}
if (callbackData.startsWith("topicChoice:")) {
String topicPrefix = "topicChoice:";
users.get(userId).setCurrentTopicName(callbackData.substring(topicPrefix.length()));
chooseQuiz(callbackQuery, userId);

chooseQuiz(callbackData, userId);
return UpdatesListener.CONFIRMED_UPDATES_ALL;
}
handlerQuizAnswer(callbackQuery, userId);
Expand Down Expand Up @@ -242,16 +252,18 @@ private boolean isRegisterUser(Long chatId) {
return users.containsKey(chatId);
}

private void clearLastMessageKeyboard(Long chatId){
private void clearLastMessageKeyboard(Long chatId) {
Message message = users.get(chatId).getLastBotMessage();
EditMessageText editMessage = new EditMessageText(chatId, message.messageId(), message.text());
editMessage.replyMarkup(new InlineKeyboardMarkup(new InlineKeyboardButton("").callbackData("deleted")));
bot.execute(editMessage);
}

private void chooseQuiz(CallbackQuery callbackQuery, Long chatId) {
private void chooseQuiz(String callbackData, Long chatId) {
UserInfo userInfo = users.get(chatId);
String topicName = callbackQuery.data();
String topicPrefix = "topicChoice:";
String topicName = callbackData.substring(topicPrefix.length());
userInfo.setCurrentTopicName(topicName);
clearLastMessageKeyboard(chatId);
userInfo.setChoiceQuiz(false);
bot.execute(new SendMessage(chatId, String.format("Quiz: %s\n write /start_quiz or /choice for choice any quiz",
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/example/QuizBotExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import com.pengrad.telegrambot.ExceptionHandler;
import com.pengrad.telegrambot.TelegramException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class QuizBotExceptionHandler implements ExceptionHandler {
Logger logger = LogManager.getLogger();
public QuizBotExceptionHandler() {
}

@Override
public void onException(TelegramException e) {
//todo logger
if (e.response() != null) {
e.response().errorCode();
e.response().description();
} else {
logger.fatal("Was throw exception in class QuizBotExceptionHandler");
throw new RuntimeException("Don't access to bot", e);
}
}
Expand Down

0 comments on commit 59105cf

Please sign in to comment.