Skip to content

Commit

Permalink
Update project (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
MykhailoTiutiun authored Aug 15, 2024
1 parent 3954949 commit f250466
Show file tree
Hide file tree
Showing 85 changed files with 1,963 additions and 1,054 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
secrets

### STS ###
.apt_generated
Expand Down
14 changes: 8 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FROM tomcat:10.1.11
FROM maven:3.9.7-eclipse-temurin-17 AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package

COPY ./target/RepCounterBot.war /usr/local/tomcat/webapps-javaee/ROOT.war

EXPOSE 8080

CMD ["catalina.sh", "run"]
FROM openjdk:17-jdk-alpine
LABEL maintainer="MykhailoTiutiun <[email protected]>"
COPY --from=build /home/app/target/RepCounterBot.jar RepCounterBot.jar
ENTRYPOINT ["java", "-jar", "/RepCounterBot.jar"]
35 changes: 29 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
version: "3.3"
services:
rep-counter-bot:
container_name: rep-counter-bot
image: rep-counter-bot
repcounterbot:
container_name: repcounterbot
build:
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
DATASOURCE_URI: "mongodb+srv://repcounterbot-app:[email protected]/?retryWrites=true&w=majority&appName=RepCounterBotCluster"
DATASOURCE_URL: ""
DATASOURCE_USER: ""
DATASOURCE_PASSWORD: ""
BOT_TOKEN: ""
restart: unless-stopped
deploy:
resources:
limits:
cpus: '1'
memory: 300M
networks:
- repcounterbot
- local

postgres:
container_name: postgres
image: postgres
restart: always
shm_size: 128mb
ports:
- 5432:5432
environment:
POSTGRES_DB: repcounterbot
POSTGRES_USER: ""
POSTGRES_PASSWORD: ""
volumes:
- repcounterbot-db:/var/lib/postgresql/data
networks:
- local

networks:
repcounterbot:
local:
driver: bridge

volumes:
repcounterbot-db:
19 changes: 14 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<groupId>com.mykhailotiutiun</groupId>
<artifactId>RepCounterBot</artifactId>
<version>1.0.1</version>
<packaging>war</packaging>
<packaging>jar</packaging>
<name>RepCounterBot</name>
<description>RepCounterBot</description>
<properties>
Expand All @@ -20,7 +20,11 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -75,9 +79,14 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
<version>2.22.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.22.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<finalName>RepCounterBot</finalName>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Getter
Expand All @@ -33,59 +26,8 @@ public RepCounterBot(RepCounterBotFacade repCounterBotFacade) {
this.repCounterBotFacade = repCounterBotFacade;
}


@Override
public BotApiMethod<?> onWebhookUpdateReceived(Update update) {
return repCounterBotFacade.handleUpdate(update);
}

public void sendMessage(String chatId, String textMessage) {
SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(chatId);
sendMessage.setText(textMessage);

try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}

public void sendMessage(SendMessage sendMessage) {
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}

public void sendInlineKeyBoardMessage(String chatId, String text, String buttonText, String callbackData) {
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
InlineKeyboardButton keyboardButton = new InlineKeyboardButton();

keyboardButton.setText(buttonText);

if (callbackData != null) {
keyboardButton.setCallbackData(callbackData);
}

List<InlineKeyboardButton> keyboardButtonsRow1 = new ArrayList<>();
keyboardButtonsRow1.add(keyboardButton);

List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
rowList.add(keyboardButtonsRow1);

inlineKeyboardMarkup.setKeyboard(rowList);

SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(chatId);
sendMessage.setText(text);
sendMessage.setReplyMarkup(inlineKeyboardMarkup);

try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import com.mykhailotiutiun.repcounterbot.botapi.handler.CallbackQueryHandler;
import com.mykhailotiutiun.repcounterbot.botapi.handler.MessageHandler;
import com.mykhailotiutiun.repcounterbot.cache.ChatDataCache;
import com.mykhailotiutiun.repcounterbot.cache.CurrentBotStateCache;
import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType;
import com.mykhailotiutiun.repcounterbot.constants.ChatState;
import com.mykhailotiutiun.repcounterbot.constants.MessageHandlerType;
import com.mykhailotiutiun.repcounterbot.service.LocaleMessageService;
import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
Expand All @@ -27,14 +26,14 @@ public class RepCounterBotFacade {
private final Map<MessageHandlerType, MessageHandler> messageHandlers = new HashMap<>();
private final Map<CallbackHandlerType, CallbackQueryHandler> callbackQueryHandlers = new HashMap<>();

private final LocaleMessageService localeMessageService;
private final ChatDataCache chatDataCache;
private final LocaleMessageUtil localeMessageUtil;
private final CurrentBotStateCache currentBotStateCache;

public RepCounterBotFacade(List<MessageHandler> messageHandlers, List<CallbackQueryHandler> callbackQueryHandlers, LocaleMessageService localeMessageService, ChatDataCache chatDataCache) {
this.localeMessageService = localeMessageService;
public RepCounterBotFacade(List<MessageHandler> messageHandlers, List<CallbackQueryHandler> callbackQueryHandlers, LocaleMessageUtil localeMessageUtil, CurrentBotStateCache currentBotStateCache) {
this.localeMessageUtil = localeMessageUtil;
messageHandlers.forEach(handler -> this.messageHandlers.put(handler.getHandlerType(), handler));
callbackQueryHandlers.forEach(handler -> this.callbackQueryHandlers.put(handler.getHandlerType(), handler));
this.chatDataCache = chatDataCache;
this.currentBotStateCache = currentBotStateCache;
}

public BotApiMethod<?> handleUpdate(Update update) {
Expand All @@ -43,14 +42,12 @@ public BotApiMethod<?> handleUpdate(Update update) {
} else {
return handleMessage(update.getMessage());
}


}

private BotApiMethod<?> handleCallbackQuery(CallbackQuery callbackQuery) {
CallbackQueryHandler callbackQueryHandler = choseCallbackQueryHandler(callbackQuery);
if (callbackQueryHandler == null) {
return new SendMessage(callbackQuery.getFrom().getId().toString(), localeMessageService.getMessage("reply.error", callbackQuery.getFrom().getId().toString()));
return new SendMessage(callbackQuery.getFrom().getId().toString(), localeMessageUtil.getMessage("reply.error", callbackQuery.getFrom().getId().toString()));
}

log.trace("CallbackQuery request from @{}, with text {}", callbackQuery.getFrom().getUserName(), callbackQuery.getData());
Expand All @@ -59,19 +56,19 @@ private BotApiMethod<?> handleCallbackQuery(CallbackQuery callbackQuery) {

private CallbackQueryHandler choseCallbackQueryHandler(CallbackQuery callbackQuery) {
if (callbackQuery.getData().contains("Main")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.MAIN_MENU_HANDLER);
} else if(callbackQuery.getData().contains("WorkoutWeek")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.WORKOUT_WEEK_HANDLER);
} else if (callbackQuery.getData().contains("WorkoutDay")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.WORKOUT_DAY_HANDLER);
} else if (callbackQuery.getData().contains("WorkoutExercise")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.WORKOUT_EXERCISE_HANDLER);
} else if (callbackQuery.getData().contains("WorkoutSet")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.WORKOUT_SET_HANDLER);
}

Expand All @@ -82,7 +79,7 @@ private CallbackQueryHandler choseCallbackQueryHandler(CallbackQuery callbackQue
private BotApiMethod<?> handleMessage(Message message) {
MessageHandler messageHandler = choseMessageHandler(message);
if (messageHandler == null) {
return new SendMessage(message.getChatId().toString(), localeMessageService.getMessage("reply.invalid-message", message.getChatId().toString()));
return new SendMessage(message.getChatId().toString(), localeMessageUtil.getMessage("reply.invalid-message", message.getChatId().toString()));
}

log.trace("Message request from @{}, with text {}", message.getFrom().getUserName(), message.getText());
Expand All @@ -98,19 +95,19 @@ private MessageHandler choseMessageHandler(Message message) {
case ("/start"):
case ("Choose a language"):
case ("Обрати мову"): {
chatDataCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
return messageHandlers.get(MessageHandlerType.MAIN_MENU_HANDLER);
}
case ("Поточний тиждень тренувань"):
case ("Current workout week"): {
chatDataCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
case ("Поточний тиждень"):
case ("Current week"): {
currentBotStateCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
return messageHandlers.get(MessageHandlerType.CURRENT_WEEK_HANDLER);
}
default:
break;
}

switch (chatDataCache.getChatDataCurrentBotState(message.getChatId().toString())) {
switch (currentBotStateCache.getChatDataCurrentBotState(message.getChatId().toString())) {
case SET_NAME_FOR_WORKOUT_DAY:
return messageHandlers.get(MessageHandlerType.WORKOUT_DAY_HANDLER);
case CREATE_WORKOUT_EXERCISE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;

public interface CallbackQueryHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mykhailotiutiun.repcounterbot.botapi.handler.CallbackQueryHandler;
import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType;
import com.mykhailotiutiun.repcounterbot.service.MainMenuService;
import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator;
import com.mykhailotiutiun.repcounterbot.service.UserService;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
Expand All @@ -11,20 +11,20 @@
@Component
public class MainMenuCallbackQueryHandler implements CallbackQueryHandler {

private final MainMenuService mainMenuService;
private final MainMenuMessageGenerator mainMenuMessageGenerator;
private final UserService userService;

public MainMenuCallbackQueryHandler(MainMenuService mainMenuService, UserService userService) {
this.mainMenuService = mainMenuService;
public MainMenuCallbackQueryHandler(MainMenuMessageGenerator mainMenuMessageGenerator, UserService userService) {
this.mainMenuMessageGenerator = mainMenuMessageGenerator;
this.userService = userService;
}

@Override
public SendMessage handleCallbackQuery(CallbackQuery callbackQuery) {
// 0 - command, 1 - userId, 2 - lang
String[] splintedMessage = callbackQuery.getData().split(":");
userService.setUserLang(splintedMessage[1], splintedMessage[2]);
return mainMenuService.getMainMenuMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getFrom().getFirstName());
userService.setUserLang(Long.valueOf(splintedMessage[1]), splintedMessage[2]);
return mainMenuMessageGenerator.getMainMenuMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getFrom().getFirstName());
}

@Override
Expand Down
Loading

0 comments on commit f250466

Please sign in to comment.