Skip to content

Commit

Permalink
Step 6 (#21)
Browse files Browse the repository at this point in the history
* JRTB-1: added repository layer.

* STEP_6
JRTB-5: added ability to subscribe on group
JRTB-6: added ability to see the list of the group subscription.

* STEP_6
  • Loading branch information
IvanLiVa authored Dec 20, 2023
1 parent bce0070 commit 47b990f
Show file tree
Hide file tree
Showing 38 changed files with 1,060 additions and 31 deletions.
35 changes: 18 additions & 17 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up MySQL
uses: mirromutth/[email protected]
with:
mysql version: '8.0.15'
mysql database: 'dev_jrtb_db'
mysql root password: 'root'
mysql user: 'dev_jrtb_db_user'
mysql password: 'dev_jrtb_db_password'
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
- uses: actions/checkout@v3
- name: Set up MySQL
uses: mirromutth/[email protected]
with:
mysql version: '8.0.15'
mysql database: 'dev_jrtb_db'
mysql root password: 'root'
mysql user: 'dev_jrtb_db_user'
mysql password: 'dev_jrtb_db_password'
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml




8 changes: 7 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ JRTB-0: added SpringBoot skeleton project
* JRTB-13: added deployment process to the project
* ## 0.4.0-SNAPSHOT

* JRTB-1: added repository layer.

* JRTB-1: added repository layer.
## 0.5.0-SNAPSHOT

* JRTB-5: added ability to subscribe on group
* JRTB-6: added ability to get a list of group subscriptions.

16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<description>Telegram bot for Javarush from community to community</description>
<properties>
<java.version>11</java.version>
<unirest.version>3.11.01</unirest.version>
<apache.commons.version>3.11</apache.commons.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -38,6 +40,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.version}</version>
</dependency>


<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
Expand All @@ -51,6 +60,13 @@
<artifactId>telegrambots-spring-boot-starter</artifactId>
<version>6.8.0</version>
</dependency>

<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>${unirest.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.bot;
import com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandContainer;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClient;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.GroupSubService;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageServiceImpl;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -32,8 +34,10 @@ public String getBotToken() {


@Autowired
public JavarushTelegramBot(TelegramUserService telegramUserService) {
this.commandContainer = new CommandContainer(new SendBotMessageServiceImpl(this), telegramUserService);

public JavarushTelegramBot(TelegramUserService telegramUserService, JavaRushGroupClient groupClient, GroupSubService groupSubService) {
this.commandContainer = new CommandContainer(new SendBotMessageServiceImpl(this), telegramUserService,groupClient,groupSubService);

}
public void onUpdateReceived(Update update) {
if(update.hasMessage() && update.getMessage().hasText()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.command;

import com.github.JBolivarLi.javarushtelegrambot.bot.command.Command;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClient;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupDiscussionInfo;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupRequestArgs;
import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.GroupSubService;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService;
import org.telegram.telegrambots.meta.api.objects.Update;

import java.util.stream.Collectors;

import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.ADD_GROUP_SUB;
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandUtils.getChatId;
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandUtils.getMessage;
import static java.util.Objects.isNull;
import static org.apache.commons.lang3.StringUtils.SPACE;


/**
* Add Group subscription {@link Command}.
*/
//todo add unit test for the command logic.
public class AddGroupSubCommand implements Command {

private final SendBotMessageService sendBotMessageService;
private final JavaRushGroupClient javaRushGroupClient;
private final GroupSubService groupSubService;

public AddGroupSubCommand(SendBotMessageService sendBotMessageService, JavaRushGroupClient javaRushGroupClient,
GroupSubService groupSubService) {
this.sendBotMessageService = sendBotMessageService;
this.javaRushGroupClient = javaRushGroupClient;
this.groupSubService = groupSubService;
}
private boolean isNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
}
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}


@Override
public void execute(Update update) {
if (getMessage(update).equalsIgnoreCase(ADD_GROUP_SUB.getCommandName())) {
sendGroupIdList(getChatId(update));
return;
}
String groupId = getMessage(update).split(SPACE)[1];
String chatId = getChatId(update);
if (isNumeric(groupId)) {
GroupDiscussionInfo groupById = javaRushGroupClient.getGroupById(Integer.parseInt(groupId));
if (isNull(groupById.getId())) {
sendGroupNotFound(chatId, groupId);
}
GroupSub savedGroupSub = groupSubService.save(chatId, groupById);
sendBotMessageService.sendMessage(chatId, "Подписал на группу " + savedGroupSub.getTitle());
} else {
sendNotValidGroupID(chatId, groupId);
}
}

private void sendGroupNotFound(String chatId, String groupId) {
String groupNotFoundMessage = "Нет группы с ID = \"%s\"";
sendBotMessageService.sendMessage(chatId, String.format(groupNotFoundMessage, groupId));
}

private void sendNotValidGroupID(String chatId, String groupId) {
String groupNotFoundMessage = "Неправильный ID группы = \"%s\"";
sendBotMessageService.sendMessage(chatId, String.format(groupNotFoundMessage, groupId));
; }

private void sendGroupIdList(String chatId) {
String groupIds = javaRushGroupClient.getGroupList(GroupRequestArgs.builder().build()).stream()
.map(group -> String.format("%s - %s \n", group.getTitle(), group.getId()))
.collect(Collectors.joining());

String message = "Чтобы подписаться на группу - передай комадну вместе с ID группы. \n" +
"Например: /addGroupSub 16 \n\n" +
"я подготовил список всех групп - выбирай какую хочешь :) \n\n" +
"имя группы - ID группы \n\n" +
"%s";

sendBotMessageService.sendMessage(chatId, String.format(message, groupIds));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.command;

import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClient;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.GroupSubService;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageServiceImpl;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService;
Expand All @@ -13,12 +15,17 @@ public class CommandContainer {
private final Map<String, Command> commandMap;
private final Command unknownCommand;

public CommandContainer(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService) {
commandMap = Map.ofEntries(Map.entry(START.getCommandName(), new StartCommand(sendBotMessageService,telegramUserService)),

public CommandContainer(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService, JavaRushGroupClient javaRushGroupClient, GroupSubService groupSubService) {
commandMap =
Map.ofEntries(Map.entry(START.getCommandName(), new StartCommand(sendBotMessageService,telegramUserService)),
Map.entry(STOP.getCommandName(), new StopCommand(sendBotMessageService,telegramUserService)),
Map.entry(HELP.getCommandName(), new HelpCommand(sendBotMessageService)),
Map.entry(NO.getCommandName(), new NoCommand(sendBotMessageService)),
Map.entry(STAT.getCommandName(),new StatCommand(sendBotMessageService,telegramUserService)));
Map.entry(STAT.getCommandName(),new StatCommand(sendBotMessageService,telegramUserService)),
Map.entry(ADD_GROUP_SUB.getCommandName(),new AddGroupSubCommand(sendBotMessageService, javaRushGroupClient, groupSubService)),
Map.entry(LIST_GROUP_SUB.getCommandName(),new ListGroupSubCommand(sendBotMessageService,telegramUserService)));


unknownCommand = new UnknownCommand(sendBotMessageService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ public enum CommandName {
STOP("/stop"),
HELP("/help"),
NO("nocommand"),
STAT("/stat");

STAT("/stat"),
ADD_GROUP_SUB("/addgroupsub"),
LIST_GROUP_SUB("/listgroupsub");


private final String commandName;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.command;


import org.telegram.telegrambots.meta.api.objects.Update;

/**
* Utils class for Commands.
*/
public class CommandUtils {

/**
* Retrieve chatId from {@link Update} object.
*
* @param update provided {@link Update}
* @return chatID from the provided {@link Update} object.
*/
public static String getChatId(Update update) {
return update.getMessage().getChatId().toString();
}

/**
* Retrieve text of the message from {@link Update} object.
*
* @param update provided {@link Update}
* @return the text of the message from the provided {@link Update} object.
*/
public static String getMessage(Update update) {
return update.getMessage().getText();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ public class HelpCommand implements Command {

private final SendBotMessageService sendBotMessageService;

public static final String HELP_MESSAGE = String.format("✨<b>Дотупные команды</b>✨\n\n"
+ "<b>Начать\\закончить работу с ботом</b>\n"

public static final String HELP_MESSAGE = String.format("✨Дотупные команды✨\n\n"

+ "Начать\\закончить работу с ботом:\n"
+ "%s - начать работу со мной\n"
+ "%s - приостановить работу со мной\n\n"

+ "Работа с подписками на группы:\n"
+ "%s - подписаться на группу статей\n"
+ "%s - получить список групп, на которые подписан\n\n"

+ "%s - получить помощь в работе со мной\n"
+ "%s - получить статистику \n",
START.getCommandName(), STOP.getCommandName(), HELP.getCommandName(), STAT.getCommandName());
+ "%s - получить мою статистику использования\n",
START.getCommandName(), STOP.getCommandName(), ADD_GROUP_SUB.getCommandName(),
LIST_GROUP_SUB.getCommandName(), HELP.getCommandName(), STAT.getCommandName());



public HelpCommand(SendBotMessageService sendBotMessageService) {
this.sendBotMessageService = sendBotMessageService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.command;

import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.TelegramUser;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService;
import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService;
import org.telegram.telegrambots.meta.api.objects.Update;
import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub;
import java.util.stream.Collectors;
import javax.ws.rs.NotFoundException;

import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandUtils.getChatId;

/**
* {@link Command} for getting list of {@link GroupSub}.
*/
public class ListGroupSubCommand implements Command {

private final SendBotMessageService sendBotMessageService;
private final TelegramUserService telegramUserService;

public ListGroupSubCommand(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService) {
this.sendBotMessageService = sendBotMessageService;
this.telegramUserService = telegramUserService;
}

@Override
public void execute(Update update) {
//todo add exception handling
TelegramUser telegramUser = telegramUserService.findByChatId(getChatId(update))
.orElseThrow(NotFoundException::new);

String message = "Я нашел все подписки на группы: \n\n";
String collectedGroups = telegramUser.getGroupSubs().stream()
.map(it -> "Группа: " + it.getTitle() + " , ID = " + it.getId() + " \n")
.collect(Collectors.joining());

sendBotMessageService.sendMessage(telegramUser.getChatId(), message + collectedGroups);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient;
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.*;


import java.util.List;

/**
* Client for Javarush Open API corresponds to Groups.
*/
public interface JavaRushGroupClient {

/**
* Get all the {@link GroupInfo} filtered by provided {@link GroupRequestArgs}.
*
* @param requestArgs provided {@link GroupRequestArgs}.
* @return the collection of the {@link GroupInfo} objects.
*/
List<GroupInfo> getGroupList(GroupRequestArgs requestArgs);

/**
* Get all the {@link GroupDiscussionInfo} filtered by provided {@link GroupRequestArgs}.
*
* @param requestArgs provided {@link GroupRequestArgs}
* @return the collection of the {@link GroupDiscussionInfo} objects.
*/
List<GroupDiscussionInfo> getGroupDiscussionList(GroupRequestArgs requestArgs);

/**
* Get count of groups filtered by provided {@link GroupRequestArgs}.
*
* @param countRequestArgs provided {@link GroupsCountRequestArgs}.
* @return the count of the groups.
*/
Integer getGroupCount(GroupsCountRequestArgs countRequestArgs);

/**
* Get {@link GroupDiscussionInfo} by provided ID.
*
* @param id provided ID.
* @return {@link GroupDiscussionInfo} object.
*/
GroupDiscussionInfo getGroupById(Integer id);
}

Loading

0 comments on commit 47b990f

Please sign in to comment.