|
@@ -0,0 +1,121 @@
|
|
|
|
+package org.example.sweater.service;
|
|
|
|
+
|
|
|
|
+import org.example.sweater.config.TelegramBotProperties;
|
|
|
|
+import org.example.sweater.domain.User;
|
|
|
|
+import org.example.sweater.repos.UsersRepository;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;
|
|
|
|
+import org.telegram.telegrambots.longpolling.interfaces.LongPollingUpdateConsumer;
|
|
|
|
+import org.telegram.telegrambots.longpolling.starter.SpringLongPollingBot;
|
|
|
|
+import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
|
|
|
|
+import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
|
|
|
+import org.telegram.telegrambots.meta.api.objects.Update;
|
|
|
|
+import org.telegram.telegrambots.meta.api.objects.message.Message;
|
|
|
|
+import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
|
|
|
|
+import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
|
|
|
|
+import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardRow;
|
|
|
|
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
|
|
|
+import org.telegram.telegrambots.meta.generics.TelegramClient;
|
|
|
|
+
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
+
|
|
|
|
+@Component
|
|
|
|
+public class TelegramAccountActivation implements SpringLongPollingBot, LongPollingSingleThreadUpdateConsumer {
|
|
|
|
+ private final TelegramBotProperties properties;
|
|
|
|
+
|
|
|
|
+ private final TelegramClient telegramClient;
|
|
|
|
+ private final UsersRepository usersRepository;
|
|
|
|
+
|
|
|
|
+ public TelegramAccountActivation(TelegramBotProperties properties, UsersRepository usersRepository) {
|
|
|
|
+ this.properties = properties;
|
|
|
|
+ this.telegramClient = new OkHttpTelegramClient(properties.getToken());
|
|
|
|
+ this.usersRepository = usersRepository;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String getBotToken() {
|
|
|
|
+ return properties.getToken();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public LongPollingUpdateConsumer getUpdatesConsumer() {
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private User checkUserCode(String username, String uuid) {
|
|
|
|
+ User user = usersRepository.findByTelegramUsername(username);
|
|
|
|
+ if (user == null || !user.getTelegramActivationCode().equals(uuid)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ user.setActive(true);
|
|
|
|
+ user.setTelegramActivationCode(null);
|
|
|
|
+ usersRepository.save(user);
|
|
|
|
+ return user;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void consume(Update update) {
|
|
|
|
+ if (!update.hasMessage() || !update.getMessage().hasText()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Message message = update.getMessage();
|
|
|
|
+ String text = message.getText();
|
|
|
|
+ String startCommand = "/start";
|
|
|
|
+
|
|
|
|
+ if (!text.contains(startCommand)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String username = message.getFrom().getUserName();
|
|
|
|
+ Long chatId = message.getChatId();
|
|
|
|
+ Pattern uuidInMessagePattern = Pattern.compile("[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}", Pattern.CASE_INSENSITIVE);
|
|
|
|
+ Matcher matcher = uuidInMessagePattern.matcher(text);
|
|
|
|
+
|
|
|
|
+ if (!matcher.find()) {
|
|
|
|
+ try {
|
|
|
|
+ SendMessage answer = SendMessage.builder().chatId(chatId).text("Unexpected message format").build();
|
|
|
|
+ telegramClient.execute(answer);
|
|
|
|
+ } catch (TelegramApiException e) {
|
|
|
|
+ System.out.println("TelegramApiException: " + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int uuidStartIndex = matcher.start();
|
|
|
|
+ int uuidEndIndex = matcher.end();
|
|
|
|
+ String uuid = text.substring(uuidStartIndex, uuidEndIndex);
|
|
|
|
+ User user = checkUserCode(username, uuid);
|
|
|
|
+ try {
|
|
|
|
+ SendMessage answer;
|
|
|
|
+
|
|
|
|
+ if (user != null) {
|
|
|
|
+ InlineKeyboardButton goLoginButton = InlineKeyboardButton
|
|
|
|
+ .builder()
|
|
|
|
+ .text("Go login")
|
|
|
|
+// .url("http://localhost:8080/login")
|
|
|
|
+ .url("https://registry.thesol.ru")
|
|
|
|
+ .build();
|
|
|
|
+ InlineKeyboardMarkup keyboard = InlineKeyboardMarkup
|
|
|
|
+ .builder()
|
|
|
|
+ .keyboardRow(new InlineKeyboardRow(goLoginButton))
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ answer = SendMessage
|
|
|
|
+ .builder()
|
|
|
|
+ .text("User " + username + " was activated!")
|
|
|
|
+ .replyMarkup(keyboard)
|
|
|
|
+ .chatId(chatId)
|
|
|
|
+ .build();
|
|
|
|
+ } else {
|
|
|
|
+ answer = SendMessage.builder().text("Activation failed").chatId(chatId).build();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ telegramClient.execute(answer);
|
|
|
|
+ } catch (TelegramApiException e) {
|
|
|
|
+ System.out.println("TelegramApiException: " + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|