-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
383 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
start - начать/восстановить работу с ботом | ||
stop - приостановить работу с ботом | ||
addgroupsub - подписаться на группу статей | ||
deletegroupsub - отписаться от группы статей | ||
listgroupsub - список групп, на которые подписан | ||
help - получить помощь в работе со мной |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/AdminHelpCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.STAT; | ||
import static java.lang.String.format; | ||
|
||
/** | ||
* Admin Help {@link Command}. | ||
*/ | ||
public class AdminHelpCommand implements Command { | ||
|
||
public static final String ADMIN_HELP_MESSAGE = format("✨<b>Доступные команды админа</b>✨\n\n" | ||
+ "<b>Получить статистику</b>\n" | ||
+ "%s - статистика бота\n", | ||
STAT.getCommandName()); | ||
|
||
private final SendBotMessageService sendBotMessageService; | ||
|
||
public AdminHelpCommand(SendBotMessageService sendBotMessageService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
} | ||
|
||
@Override | ||
public void execute(Update update) { | ||
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), ADMIN_HELP_MESSAGE); | ||
|
||
} | ||
} |
41 changes: 31 additions & 10 deletions
41
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,58 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.command.annotation.AdminCommand; | ||
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; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.*; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.*; | ||
import static java.util.Objects.nonNull; | ||
|
||
public class CommandContainer { | ||
private final Map<String, Command> commandMap; | ||
private final Command unknownCommand; | ||
private final List<String> admins; | ||
|
||
|
||
public CommandContainer(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService, JavaRushGroupClient javaRushGroupClient, GroupSubService groupSubService) { | ||
|
||
public CommandContainer(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService, | ||
JavaRushGroupClient javaRushGroupClient, GroupSubService groupSubService, List<String> admins, StatisticsService statisticsService) { | ||
this.admins = admins; | ||
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, statisticsService)), | ||
Map.entry(ADD_GROUP_SUB.getCommandName(), new AddGroupSubCommand(sendBotMessageService, javaRushGroupClient, groupSubService)), | ||
Map.entry(LIST_GROUP_SUB.getCommandName(), new ListGroupSubCommand(sendBotMessageService, telegramUserService)), | ||
Map.entry(DELETE_GROUP_SUB.getCommandName(), new DeleteGroupSubCommand(sendBotMessageService, groupSubService, telegramUserService))); | ||
Map.entry(DELETE_GROUP_SUB.getCommandName(), new DeleteGroupSubCommand(sendBotMessageService, groupSubService, telegramUserService)), | ||
Map.entry(ADMIN_HELP.getCommandName(), new AdminHelpCommand(sendBotMessageService))); | ||
|
||
|
||
unknownCommand = new UnknownCommand(sendBotMessageService); | ||
} | ||
|
||
|
||
public Command retrieveCommand(String commandIdentifier, String username) { | ||
Command orDefault = commandMap.getOrDefault(commandIdentifier, unknownCommand); | ||
|
||
unknownCommand = new UnknownCommand(sendBotMessageService); | ||
if (isAdminCommand(orDefault)) { | ||
if (admins.contains(username)) { | ||
return orDefault; | ||
} else { | ||
return unknownCommand; | ||
} | ||
} | ||
return orDefault; | ||
} | ||
|
||
public Command retrieveCommand(String commandIdentifier) { | ||
return commandMap.getOrDefault(commandIdentifier, unknownCommand); | ||
private boolean isAdminCommand(Command command) { | ||
AdminCommand adminCommandAnnotation = command.getClass().getAnnotation(AdminCommand.class); | ||
return adminCommandAnnotation != null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 30 additions & 7 deletions
37
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/StatCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,49 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.command.annotation.AdminCommand; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.dto.StatisticDTO; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.StatisticsService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Statistics {@link Command}. | ||
*/ | ||
@AdminCommand | ||
public class StatCommand implements Command { | ||
|
||
private final TelegramUserService telegramUserService; | ||
private final StatisticsService statisticsService; | ||
private final SendBotMessageService sendBotMessageService; | ||
|
||
public final static String STAT_MESSAGE = "Javarush Telegram Bot использует %s человек."; | ||
public final static String STAT_MESSAGE = "✨<b>Подготовил статистику</b>✨\n" + | ||
"- Количество активных пользователей: %s\n" + | ||
"- Количество неактивных пользователей: %s\n" + | ||
"- Среднее количество групп на одного пользователя: %s\n\n" + | ||
"<b>Информация по активным группам</b>:\n" + | ||
"%s"; | ||
|
||
@Autowired | ||
public StatCommand(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService) { | ||
public StatCommand(SendBotMessageService sendBotMessageService, StatisticsService statisticsService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
this.telegramUserService = telegramUserService; | ||
this.statisticsService = statisticsService; | ||
} | ||
|
||
@Override | ||
public void execute(Update update) { | ||
int activeUserCount = telegramUserService.retrieveAllActiveUsers().size(); | ||
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), String.format(STAT_MESSAGE, activeUserCount)); | ||
StatisticDTO statisticDTO = statisticsService.countBotStatistic(); | ||
|
||
String collectedGroups = statisticDTO.getGroupStatDTOs().stream() | ||
.map(it -> String.format("%s (id = %s) - %s подписчиков", it.getTitle(), it.getId(), it.getActiveUserCount())) | ||
.collect(Collectors.joining("\n")); | ||
|
||
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), String.format(STAT_MESSAGE, | ||
statisticDTO.getActiveUserCount(), | ||
statisticDTO.getInactiveUserCount(), | ||
statisticDTO.getAverageGroupCountByUser(), | ||
collectedGroups)); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...n/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/annotation/AdminCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command.annotation; | ||
|
||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.command.Command; | ||
|
||
import java.lang.annotation.Retention; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* Mark if {@link Command} can be viewed only by admins. | ||
*/ | ||
@Retention(RUNTIME) | ||
public @interface AdminCommand { | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/dto/GroupStatDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.dto; | ||
|
||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
/** | ||
* DTO for showing group id and title without data | ||
*/ | ||
@Data | ||
|
||
@EqualsAndHashCode(exclude = {"title", "activeUserCount"}) | ||
public class GroupStatDTO { | ||
|
||
private final Integer id; | ||
private final String title; | ||
private final Integer activeUserCount; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/dto/StatisticDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.dto; | ||
|
||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* DTO for getting bot statistics. | ||
*/ | ||
@Data | ||
@EqualsAndHashCode | ||
public class StatisticDTO { | ||
private final int activeUserCount; | ||
private final int inactiveUserCount; | ||
private final List<GroupStatDTO> groupStatDTOs; | ||
private final double averageGroupCountByUser; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/StatisticsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.service; | ||
|
||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.dto.StatisticDTO; | ||
|
||
/** | ||
* Service for getting bot statistics. | ||
*/ | ||
public interface StatisticsService { | ||
StatisticDTO countBotStatistic(); | ||
} |
41 changes: 41 additions & 0 deletions
41
...ain/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/StatisticsServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.service; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.dto.GroupStatDTO; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.dto.StatisticDTO; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.TelegramUser; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.springframework.util.CollectionUtils.isEmpty; | ||
|
||
|
||
@Service | ||
public class StatisticsServiceImpl implements StatisticsService { | ||
|
||
private final GroupSubService groupSubService; | ||
private final TelegramUserService telegramUserService; | ||
|
||
public StatisticsServiceImpl(GroupSubService groupSubService, TelegramUserService telegramUserService) { | ||
this.groupSubService = groupSubService; | ||
this.telegramUserService = telegramUserService; | ||
} | ||
|
||
@Override | ||
public StatisticDTO countBotStatistic() { | ||
List<GroupStatDTO> groupStatDTOS = groupSubService.findAll().stream() | ||
.filter(it -> !isEmpty(it.getUsers())) | ||
.map(groupSub -> new GroupStatDTO(groupSub.getId(), groupSub.getTitle(), groupSub.getUsers().size())) | ||
.collect(Collectors.toList()); | ||
List<TelegramUser> allInActiveUsers = telegramUserService.findAllInActiveUsers(); | ||
List<TelegramUser> allActiveUsers = telegramUserService.findAllActiveUsers(); | ||
|
||
double groupsPerUser = getGroupsPerUser(allActiveUsers); | ||
return new StatisticDTO(allActiveUsers.size(), allInActiveUsers.size(), groupStatDTOS, groupsPerUser); | ||
} | ||
|
||
private double getGroupsPerUser(List<TelegramUser> allActiveUsers) { | ||
return (double) allActiveUsers.stream().mapToInt(it -> it.getGroupSubs().size()).sum() / allActiveUsers.size(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.