FreelanceBot
165 строк · 6.1 Кб
1package telegramBot.bot;
2
3
4import org.telegram.telegrambots.meta.TelegramBotsApi;
5import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
6import telegramBot.command.*;
7import telegramBot.entity.Subscription;
8import telegramBot.entity.User;
9import telegramBot.enums.Language;
10import telegramBot.messages.SubscriptionMessage;
11import telegramBot.service.*;
12
13import org.springframework.beans.factory.annotation.Autowired;
14import org.springframework.stereotype.Component;
15import org.telegram.telegrambots.bots.TelegramLongPollingBot;
16import org.telegram.telegrambots.meta.api.objects.Update;
17import telegramBot.util.BotUtil;
18import telegramBot.validation.CommandValidation;
19
20import java.util.*;
21
22
23@Component
24public class TelegramBot extends TelegramLongPollingBot {
25private final static Map<String, List<String>> commands = new HashMap<>();
26private static final String COMMAND_PREFIX = "/";
27private final CommandContainer commandContainer;
28
29private final MessageService messageService;
30
31@Autowired
32private CommandValidation validation;
33
34@Autowired
35private OrderService orderService;
36
37@Autowired
38private SubscriptionService subscriptionService;
39
40@Autowired
41private UserService userService;
42
43@Autowired
44public TelegramBot(TelegramBotsApi botsApi) throws TelegramApiException {
45botsApi.registerBot(this);
46this.messageService = new MessageService(this);
47this.commandContainer = new CommandContainer(this.messageService);
48}
49
50@Override
51public String getBotUsername() {
52return BotUtil.BOT_USERNAME;
53}
54
55@Override
56public String getBotToken() {
57return BotUtil.BOT_TOKEN;
58}
59
60@Override
61public void onUpdateReceived(Update update) {
62String inputCommand = "", chatId = "";
63User user;
64if (update.hasMessage() && update.getMessage().hasText()) {
65chatId = update.getMessage().getChatId().toString();
66user = userService.createOrGet(chatId);
67commands.putIfAbsent(chatId, new ArrayList<>());
68String message = update.getMessage().getText().trim();
69if(message.startsWith(COMMAND_PREFIX)){
70inputCommand = message.split(" ")[0].toLowerCase(Locale.ROOT);
71Command command = this.commandContainer.retrieveCommand(inputCommand);
72if(command instanceof SubscriptionCommand){
73((SubscriptionCommand) command)
74.setUserService(this.userService)
75.execute(update);
76}
77
78else if(command instanceof StopCommand){
79((StopCommand) command)
80.setUserService(this.userService)
81.execute(update);
82}
83
84else if(command instanceof RestartCommand){
85((RestartCommand) command)
86.setUserService(this.userService)
87.execute(update);
88}
89
90else {
91command.execute(update);
92commands.get(chatId).add(inputCommand);
93}
94}
95else {
96CommandName lastCommand = lastCommand(chatId);
97if(lastCommand.equals(CommandName.ADD)){
98if(validation.addCommand(update)){
99String value = update.getMessage().getText();
100Language language = Language.ignoreCaseValueOf(value);
101Subscription subscription = this.subscriptionService
102.getByLanguage(language);
103this.userService.addSubscription(user, subscription);
104this.messageService.sendResponse(chatId, SubscriptionMessage.
105getMessage(lastCommand));
106clearCommands(chatId);
107}
108}
109else if(lastCommand.equals(CommandName.REMOVE)){
110if(validation.removeCommand(update)){
111String value = update.getMessage().getText();
112Language language = Language.ignoreCaseValueOf(value);
113Subscription subscription = this.subscriptionService
114.getByLanguage(language);
115this.userService.removeSubscription(user, subscription);
116this.messageService.sendResponse(chatId, SubscriptionMessage.
117getMessage(lastCommand));
118clearCommands(chatId);
119}
120}
121else if(lastCommand.equals(CommandName.LATEST)){
122if(validation.latestCommand(update)) {
123String latestOrdersMessage = this.messageService
124.getLatestOrdersMessage(update, this.orderService);
125this.messageService.sendResponse(chatId, latestOrdersMessage);
126clearCommands(chatId);
127}
128}
129
130else if(unknownInput(chatId)){
131this.commandContainer.retrieveCommand(CommandName.
132UNKNOWN.getName()).execute(update);
133clearCommands(chatId);
134}
135}
136
137}
138}
139
140
141
142
143
144private CommandName lastCommand(String chatId) {
145int size = commands.get(chatId).size();
146if (size == 0) return CommandName.UNKNOWN;
147int lastIndex = size - 1;
148String command = commands.get(chatId).get(lastIndex);
149return CommandName.commandValueOf(command);
150}
151
152public boolean unknownInput(String chatId) {
153if (commands.get(chatId).isEmpty()) return true;
154CommandName lastCommand = lastCommand(chatId);
155return !(lastCommand.equals(CommandName.ADD) ||
156lastCommand.equals(CommandName.REMOVE) ||
157lastCommand.equals(CommandName.LATEST));
158}
159
160private void clearCommands(String chatId){
161commands.get(chatId).clear();
162}
163
164
165}
166