|
17 | 17 | import reactor.core.scheduler.Scheduler; |
18 | 18 | import ru.mifi.practice.voln.api.TelegramHandler; |
19 | 19 | import ru.mifi.practice.voln.entity.UserEntity; |
| 20 | +import ru.mifi.practice.voln.repository.MessageRepository; |
20 | 21 | import ru.mifi.practice.voln.repository.UserRepository; |
21 | 22 |
|
22 | 23 | import java.util.List; |
|
26 | 27 | @Service("TelegramHandler.Default") |
27 | 28 | public class TelegramHandlerService implements TelegramHandler { |
28 | 29 | private final UserRepository userRepository; |
| 30 | + private final MessageRepository messageRepository; |
29 | 31 | private final Scheduler telegramScheduler; |
30 | 32 |
|
| 33 | + private static void sendContactPermissions(TelegramLongPollingBot bot, Long chatId) { |
| 34 | + ReplyKeyboardMarkup markup = new ReplyKeyboardMarkup(List.of( |
| 35 | + new KeyboardRow(List.of(KeyboardButton.builder().text("Отправить карточку").requestContact(true).build())) |
| 36 | + )); |
| 37 | + try { |
| 38 | + bot.execute(SendMessage.builder().chatId(chatId) |
| 39 | + .text("Для регистрации отправьте свою персональную карточку").replyMarkup(markup).build()); |
| 40 | + } catch (TelegramApiException e) { |
| 41 | + if (log.isErrorEnabled()) { |
| 42 | + log.error("", e); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
31 | 47 | @Override |
32 | 48 | public void received(TelegramLongPollingBot bot, Update update) { |
33 | 49 | processing(bot, update) |
@@ -67,26 +83,20 @@ private Mono<Void> processing(TelegramLongPollingBot bot, Update update) { |
67 | 83 | if (entity.isTelegramRegistered()) { |
68 | 84 | sink.next(entity); |
69 | 85 | } else { |
70 | | - ReplyKeyboardMarkup markup = new ReplyKeyboardMarkup(List.of( |
71 | | - new KeyboardRow(List.of(KeyboardButton.builder().text("Отправить карточку").requestContact(true).build())) |
72 | | - )); |
73 | | - try { |
74 | | - bot.execute(SendMessage.builder().chatId(chatId) |
75 | | - .text("Для регистрации отправьте свою персональную карточку").replyMarkup(markup).build()); |
76 | | - } catch (TelegramApiException e) { |
77 | | - if (log.isErrorEnabled()) { |
78 | | - log.error("", e); |
79 | | - } |
80 | | - } |
| 86 | + sendContactPermissions(bot, chatId); |
81 | 87 | sink.complete(); |
82 | 88 | } |
83 | 89 | }) |
84 | 90 | .map(entity -> processing(bot, update, entity)) |
| 91 | + .switchIfEmpty(Mono.fromCallable(() -> { |
| 92 | + sendContactPermissions(bot, chatId); |
| 93 | + return Mono.empty(); |
| 94 | + })) |
85 | 95 | .then(); |
86 | 96 | } |
87 | 97 |
|
88 | 98 | @SuppressWarnings("PMD.UnusedFormalParameter") |
89 | 99 | private Mono<Void> processing(TelegramLongPollingBot bot, Update update, UserEntity entity) { |
90 | | - return Mono.empty(); |
| 100 | + return messageRepository.receivedMessage(entity.getId(), update.toString()); |
91 | 101 | } |
92 | 102 | } |
0 commit comments