From e400942a6952092de4390245816d343cd1726b08 Mon Sep 17 00:00:00 2001 From: uPagge Date: Thu, 5 Nov 2020 01:25:19 +0300 Subject: [PATCH] Init commit --- .gitignore | 111 +++++++++++++++++ bot/pom.xml | 61 ++++++++++ .../org/sadtech/telegram/bot/ProxyConfig.java | 31 +++++ .../org/sadtech/telegram/bot/TelegramBot.java | 18 +++ .../sadtech/telegram/bot/TelegramHookBot.java | 54 +++++++++ .../telegram/bot/TelegramPollingBot.java | 60 ++++++++++ .../MessageAutoresponderTelegram.java | 27 +++++ .../bot/config/TelegramPollingConfig.java | 29 +++++ .../bot/config/TelegramWebHookConfig.java | 26 ++++ .../bot/convert/CallbackQueryConvert.java | 23 ++++ .../bot/convert/MessageMailConvert.java | 29 +++++ .../telegram/bot/listen/EventDistributor.java | 11 ++ .../bot/listen/EventDistributorImpl.java | 37 ++++++ .../telegram/bot/listen/TelegramConnect.java | 113 ++++++++++++++++++ .../telegram/bot/listen/TelegramSender.java | 97 +++++++++++++++ .../bot/service/SendPreProcessing.java | 13 ++ pom.xml | 16 +++ 17 files changed, 756 insertions(+) create mode 100644 .gitignore create mode 100644 bot/pom.xml create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/ProxyConfig.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/TelegramBot.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/TelegramHookBot.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/TelegramPollingBot.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/autoresponder/MessageAutoresponderTelegram.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/config/TelegramPollingConfig.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/config/TelegramWebHookConfig.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/convert/CallbackQueryConvert.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/convert/MessageMailConvert.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/listen/EventDistributor.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/listen/EventDistributorImpl.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/listen/TelegramConnect.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/listen/TelegramSender.java create mode 100644 bot/src/main/java/org/sadtech/telegram/bot/service/SendPreProcessing.java create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb74627 --- /dev/null +++ b/.gitignore @@ -0,0 +1,111 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser diff --git a/bot/pom.xml b/bot/pom.xml new file mode 100644 index 0000000..b569cab --- /dev/null +++ b/bot/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + org.sadtech.telegram + telegram-bot + 0.0.1-DEVELOPER + jar + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + org.sadtech.social + social-bot + 1.0.0-RELEASE + + + + org.telegram + telegrambots + 4.9.1 + + + + + org.telegram + telegrambots-abilities + 4.6 + + + + org.telegram + telegrambotsextensions + 4.4.0.1 + + + + + + uPagge + Struchkov Mark + SADTECH + upagge@mail.ru + + + + \ No newline at end of file diff --git a/bot/src/main/java/org/sadtech/telegram/bot/ProxyConfig.java b/bot/src/main/java/org/sadtech/telegram/bot/ProxyConfig.java new file mode 100644 index 0000000..b157b95 --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/ProxyConfig.java @@ -0,0 +1,31 @@ +package org.sadtech.telegram.bot; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [30.01.2020] + */ +@Builder +@Setter +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class ProxyConfig { + + private String host; + private Integer port; + private String user; + private String password; + private Type type; + + public enum Type { + SOCKS5, SOCKS4, HTTP + } + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/TelegramBot.java b/bot/src/main/java/org/sadtech/telegram/bot/TelegramBot.java new file mode 100644 index 0000000..1c39f70 --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/TelegramBot.java @@ -0,0 +1,18 @@ +package org.sadtech.telegram.bot; + +import lombok.NonNull; +import org.sadtech.telegram.bot.listen.EventDistributorImpl; +import org.telegram.telegrambots.meta.bots.AbsSender; + +/** + * TODO: Добавить описание интерфейса. + * + * @author upagge [12.02.2020] + */ +public interface TelegramBot { + + AbsSender getAdsSender(); + + void initEventDistributor(@NonNull EventDistributorImpl eventDistributor); + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/TelegramHookBot.java b/bot/src/main/java/org/sadtech/telegram/bot/TelegramHookBot.java new file mode 100644 index 0000000..05fcdf0 --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/TelegramHookBot.java @@ -0,0 +1,54 @@ +//package org.sadtech.telegram.bot; +// +//import lombok.NonNull; +//import lombok.RequiredArgsConstructor; +//import org.sadtech.telegram.bot.config.TelegramWebHookConfig; +//import org.sadtech.telegram.bot.listen.EventDistributor; +//import org.sadtech.telegram.bot.listen.EventDistributorImpl; +//import org.telegram.telegrambots.bots.TelegramWebhookBot; +//import org.telegram.telegrambots.meta.api.methods.BotApiMethod; +//import org.telegram.telegrambots.meta.api.objects.Update; +//import org.telegram.telegrambots.meta.bots.AbsSender; +// +///** +// * TODO: Добавить описание класса. +// * +// * @author upagge [12.02.2020] +// */ +//@RequiredArgsConstructor +//public class TelegramHookBot extends TelegramWebhookBot implements TelegramBot { +// +// private final TelegramWebHookConfig webHookConfig; +// private EventDistributor eventDistributor; +// +// @Override +// public BotApiMethod onWebhookUpdateReceived(Update update) { +// return eventDistributor.processing(update); +// } +// +// @Override +// public String getBotUsername() { +// return webHookConfig.getBotUsername(); +// } +// +// @Override +// public String getBotToken() { +// return webHookConfig.getBotToken(); +// } +// +// @Override +// public String getBotPath() { +// return null; +// } +// +// @Override +// public AbsSender getAdsSender() { +// return this; +// } +// +// @Override +// public void initEventDistributor(@NonNull EventDistributorImpl eventDistributor) { +// this.eventDistributor = eventDistributor; +// } +// +//} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/TelegramPollingBot.java b/bot/src/main/java/org/sadtech/telegram/bot/TelegramPollingBot.java new file mode 100644 index 0000000..7ff6de6 --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/TelegramPollingBot.java @@ -0,0 +1,60 @@ +package org.sadtech.telegram.bot; + +import lombok.NonNull; +import org.sadtech.telegram.bot.config.TelegramPollingConfig; +import org.sadtech.telegram.bot.listen.EventDistributor; +import org.sadtech.telegram.bot.listen.EventDistributorImpl; +import org.telegram.telegrambots.bots.DefaultBotOptions; +import org.telegram.telegrambots.bots.TelegramLongPollingBot; +import org.telegram.telegrambots.meta.api.objects.Update; +import org.telegram.telegrambots.meta.bots.AbsSender; + +import java.util.Optional; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [15/07/2019] + */ +public class TelegramPollingBot extends TelegramLongPollingBot implements TelegramBot { + + private final TelegramPollingConfig telegramPollingConfig; + private EventDistributor eventDistributor; + + public TelegramPollingBot(TelegramPollingConfig telegramPollingConfig, DefaultBotOptions defaultBotOptions) { + super(defaultBotOptions); + this.telegramPollingConfig = telegramPollingConfig; + } + + public TelegramPollingBot(TelegramPollingConfig telegramPollingConfig) { + this.telegramPollingConfig = telegramPollingConfig; + } + + @Override + public void onUpdateReceived(Update update) { + if (eventDistributor != null) { + Optional.ofNullable(update).ifPresent(newUpdate -> eventDistributor.processing(update)); + } + } + + @Override + public String getBotUsername() { + return telegramPollingConfig.getBotUsername(); + } + + @Override + public String getBotToken() { + return telegramPollingConfig.getBotToken(); + } + + @Override + public AbsSender getAdsSender() { + return this; + } + + @Override + public void initEventDistributor(@NonNull EventDistributorImpl eventDistributor) { + this.eventDistributor = eventDistributor; + } + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/autoresponder/MessageAutoresponderTelegram.java b/bot/src/main/java/org/sadtech/telegram/bot/autoresponder/MessageAutoresponderTelegram.java new file mode 100644 index 0000000..f01ca8f --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/autoresponder/MessageAutoresponderTelegram.java @@ -0,0 +1,27 @@ +package org.sadtech.telegram.bot.autoresponder; + +import org.sadtech.autoresponder.repository.UnitPointerRepository; +import org.sadtech.social.bot.GeneralAutoResponder; +import org.sadtech.social.bot.domain.unit.MainUnit; +import org.sadtech.social.core.domain.content.Mail; +import org.sadtech.social.core.service.MessageService; +import org.sadtech.social.core.service.sender.Sending; + +import java.util.Set; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [18.08.2019] + */ +public class MessageAutoresponderTelegram extends GeneralAutoResponder { + + public MessageAutoresponderTelegram( + Set menuUnit, Sending sending, + MessageService messageService, + UnitPointerRepository unitPointerRepository + ) { + super(menuUnit, sending, messageService, unitPointerRepository); + } + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/config/TelegramPollingConfig.java b/bot/src/main/java/org/sadtech/telegram/bot/config/TelegramPollingConfig.java new file mode 100644 index 0000000..eb41ce9 --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/config/TelegramPollingConfig.java @@ -0,0 +1,29 @@ +package org.sadtech.telegram.bot.config; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import org.sadtech.telegram.bot.ProxyConfig; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [18.08.2019] + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class TelegramPollingConfig { + + @NonNull + private String botUsername; + + @NonNull + private String botToken; + + private ProxyConfig proxyConfig = new ProxyConfig(); + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/config/TelegramWebHookConfig.java b/bot/src/main/java/org/sadtech/telegram/bot/config/TelegramWebHookConfig.java new file mode 100644 index 0000000..d5e2862 --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/config/TelegramWebHookConfig.java @@ -0,0 +1,26 @@ +package org.sadtech.telegram.bot.config; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [12.02.2020] + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class TelegramWebHookConfig { + + @NonNull + private String botUsername; + + @NonNull + private String botToken; + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/convert/CallbackQueryConvert.java b/bot/src/main/java/org/sadtech/telegram/bot/convert/CallbackQueryConvert.java new file mode 100644 index 0000000..4d680e1 --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/convert/CallbackQueryConvert.java @@ -0,0 +1,23 @@ +package org.sadtech.telegram.bot.convert; + +import org.sadtech.social.core.domain.content.Mail; +import org.telegram.telegrambots.meta.api.objects.CallbackQuery; + +import java.time.LocalDateTime; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [02.02.2020] + */ +public class CallbackQueryConvert { + + public static Mail apply(CallbackQuery callbackQuery) { + final Mail mail = new Mail(); + mail.setText(callbackQuery.getData()); + mail.setPersonId(callbackQuery.getMessage().getChatId()); + mail.setAddDate(LocalDateTime.now()); + return mail; + } + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/convert/MessageMailConvert.java b/bot/src/main/java/org/sadtech/telegram/bot/convert/MessageMailConvert.java new file mode 100644 index 0000000..54fb355 --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/convert/MessageMailConvert.java @@ -0,0 +1,29 @@ +package org.sadtech.telegram.bot.convert; + +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; +import org.sadtech.social.core.domain.content.Mail; +import org.telegram.telegrambots.meta.api.objects.Message; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [18.08.2019] + */ +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class MessageMailConvert { + + public static Mail apply(Message message) { + Mail mail = new Mail(); + mail.setPersonId(message.getChatId()); + mail.setAddDate(LocalDateTime.now()); + mail.setText(message.getText()); + mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), ZoneId.systemDefault())); + return mail; + } + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/listen/EventDistributor.java b/bot/src/main/java/org/sadtech/telegram/bot/listen/EventDistributor.java new file mode 100644 index 0000000..cd4b44e --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/listen/EventDistributor.java @@ -0,0 +1,11 @@ +package org.sadtech.telegram.bot.listen; + +import lombok.NonNull; +import org.telegram.telegrambots.meta.api.objects.Update; + +public interface EventDistributor { + + void processing(@NonNull Update update); + +} + diff --git a/bot/src/main/java/org/sadtech/telegram/bot/listen/EventDistributorImpl.java b/bot/src/main/java/org/sadtech/telegram/bot/listen/EventDistributorImpl.java new file mode 100644 index 0000000..cb596ff --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/listen/EventDistributorImpl.java @@ -0,0 +1,37 @@ +package org.sadtech.telegram.bot.listen; + +import lombok.NonNull; +import org.sadtech.social.core.service.MailService; +import org.sadtech.telegram.bot.convert.CallbackQueryConvert; +import org.sadtech.telegram.bot.convert.MessageMailConvert; +import org.telegram.telegrambots.meta.api.objects.CallbackQuery; +import org.telegram.telegrambots.meta.api.objects.Message; +import org.telegram.telegrambots.meta.api.objects.Update; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [30.01.2020] + */ +public class EventDistributorImpl implements EventDistributor { + + private final MailService mailService; + + public EventDistributorImpl(TelegramConnect telegramConnect, MailService mailService) { + this.mailService = mailService; + telegramConnect.initEventDistributor(this); + } + + @Override + public void processing(@NonNull Update update) { + final Message message = update.getMessage(); + final CallbackQuery callbackQuery = update.getCallbackQuery(); + if (message != null) { + mailService.add(MessageMailConvert.apply(message)); + } + if (callbackQuery != null) { + mailService.add(CallbackQueryConvert.apply(callbackQuery)); + } + } + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/listen/TelegramConnect.java b/bot/src/main/java/org/sadtech/telegram/bot/listen/TelegramConnect.java new file mode 100644 index 0000000..981c616 --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/listen/TelegramConnect.java @@ -0,0 +1,113 @@ +package org.sadtech.telegram.bot.listen; + +import lombok.extern.slf4j.Slf4j; +import org.sadtech.telegram.bot.ProxyConfig; +import org.sadtech.telegram.bot.TelegramBot; +import org.sadtech.telegram.bot.TelegramPollingBot; +import org.sadtech.telegram.bot.config.TelegramPollingConfig; +import org.telegram.telegrambots.ApiContextInitializer; +import org.telegram.telegrambots.bots.DefaultBotOptions; +import org.telegram.telegrambots.meta.ApiContext; +import org.telegram.telegrambots.meta.TelegramBotsApi; +import org.telegram.telegrambots.meta.bots.AbsSender; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; + +import java.net.Authenticator; +import java.net.PasswordAuthentication; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [30.01.2020] + */ +@Slf4j +public class TelegramConnect { + + static { + ApiContextInitializer.init(); + } + + private TelegramBot telegramBot; + + public TelegramConnect(TelegramPollingConfig telegramPollingConfig) { + initLongPolling(telegramPollingConfig); + } + +// public TelegramConnect(TelegramWebHookConfig telegramWebHookConfig) { +// initWebHook(telegramWebHookConfig); +// } +// +// private void initWebHook(TelegramWebHookConfig telegramWebHookConfig) { +// TelegramBotsApi botapi = new TelegramBotsApi(); +// final TelegramWebhookBot telegramWebhookBot = new TelegramHookBot(telegramWebHookConfig); +// try { +// botapi.registerBot(telegramWebhookBot); +// this.telegramBot = (TelegramBot) telegramWebhookBot; +// } catch (TelegramApiRequestException e) { +// e.printStackTrace(); +// } +// } + + private void initLongPolling(TelegramPollingConfig telegramPollingConfig) { + final ProxyConfig proxyConfig = telegramPollingConfig.getProxyConfig(); + if (proxyConfig != null && proxyConfig.getPassword() != null) { + try { + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication( + proxyConfig.getUser(), + proxyConfig.getPassword().toCharArray() + ); + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + } + + TelegramBotsApi botapi = new TelegramBotsApi(); + try { + if (proxyConfig != null && proxyConfig.getHost() != null) { + System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); + System.setProperty("javax.net.debug", "all"); + log.info(System.getProperty("https.protocols")); + DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class); + botOptions.setProxyHost(proxyConfig.getHost()); + botOptions.setProxyPort(proxyConfig.getPort()); + botOptions.setProxyType(convertProxyType(proxyConfig.getType())); + final TelegramPollingBot bot = new TelegramPollingBot(telegramPollingConfig, botOptions); + botapi.registerBot(bot); + this.telegramBot = bot; + } else { + final TelegramPollingBot bot = new TelegramPollingBot(telegramPollingConfig); + botapi.registerBot(bot); + this.telegramBot = bot; + } + } catch (TelegramApiException e) { + log.error(e.getMessage()); + } + } + + private DefaultBotOptions.ProxyType convertProxyType(ProxyConfig.Type type) { + switch (type) { + case SOCKS5: + return DefaultBotOptions.ProxyType.SOCKS5; + case SOCKS4: + return DefaultBotOptions.ProxyType.SOCKS4; + case HTTP: + return DefaultBotOptions.ProxyType.HTTP; + default: + return DefaultBotOptions.ProxyType.NO_PROXY; + } + } + + AbsSender getAdsSender() { + return telegramBot.getAdsSender(); + } + + void initEventDistributor(EventDistributorImpl eventDistributor) { + telegramBot.initEventDistributor(eventDistributor); + } + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/listen/TelegramSender.java b/bot/src/main/java/org/sadtech/telegram/bot/listen/TelegramSender.java new file mode 100644 index 0000000..1e3f08d --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/listen/TelegramSender.java @@ -0,0 +1,97 @@ +package org.sadtech.telegram.bot.listen; + +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.sadtech.social.core.domain.BoxAnswer; +import org.sadtech.social.core.domain.keyboard.KeyBoard; +import org.sadtech.social.core.domain.keyboard.KeyBoardButton; +import org.sadtech.social.core.domain.keyboard.KeyBoardLine; +import org.sadtech.social.core.domain.keyboard.button.KeyBoardButtonText; +import org.sadtech.social.core.service.sender.SendType; +import org.sadtech.social.core.service.sender.Sending; +import org.sadtech.telegram.bot.service.SendPreProcessing; +import org.telegram.telegrambots.meta.api.methods.send.SendMessage; +import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup; +import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard; +import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton; +import org.telegram.telegrambots.meta.bots.AbsSender; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * TODO: Добавить описание класса. + * + * @author upagge [15/07/2019] + */ +@Slf4j +public class TelegramSender implements Sending { + + private final AbsSender absSender; + + @Setter + private SendPreProcessing sendPreProcessing; + + public TelegramSender(TelegramConnect telegramConnect) { + this.absSender = telegramConnect.getAdsSender(); + } + + public void send(Long integer, BoxAnswer boxAnswer) { + SendMessage sendMessage = new SendMessage(); + sendMessage.enableMarkdown(true); + sendMessage.setChatId(String.valueOf(integer)); + sendMessage.setText( + sendPreProcessing != null + ? sendPreProcessing.pretreatment(boxAnswer.getMessage()) + : boxAnswer.getMessage() + ); + sendMessage.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard())); + try { + absSender.execute(sendMessage); + } catch (TelegramApiRequestException e) { + log.error(e.getApiResponse()); + } catch (TelegramApiException e) { + log.error(e.getMessage()); + } + } + + private ReplyKeyboard convertKeyBoard(KeyBoard keyBoard) { + if (keyBoard != null) { + final InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup(); + inlineKeyboardMarkup.setKeyboard( + keyBoard.getKeyBoardLines() + .stream() + .map(this::convertLint) + .collect(Collectors.toList()) + ); + return inlineKeyboardMarkup; + } + return null; + } + + private List convertLint(KeyBoardLine line) { + return line.getKeyBoardButtons().stream().map(this::convertButton).collect(Collectors.toList()); + } + + private InlineKeyboardButton convertButton(KeyBoardButton button) { + InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); + switch (button.getType()) { + case TEXT: + KeyBoardButtonText keyBoardButtonText = (KeyBoardButtonText) button; + inlineKeyboardButton.setText(keyBoardButtonText.getLabel()); + inlineKeyboardButton.setCallbackData(keyBoardButtonText.getLabel()); + } + return inlineKeyboardButton; + } + + public void send(Long integer, Long integer1, BoxAnswer boxAnswer) { + + } + + public SendType getType() { + return SendType.PRIVATE; + } + +} diff --git a/bot/src/main/java/org/sadtech/telegram/bot/service/SendPreProcessing.java b/bot/src/main/java/org/sadtech/telegram/bot/service/SendPreProcessing.java new file mode 100644 index 0000000..fc6fd2f --- /dev/null +++ b/bot/src/main/java/org/sadtech/telegram/bot/service/SendPreProcessing.java @@ -0,0 +1,13 @@ +package org.sadtech.telegram.bot.service; + +/** + * // TODO: 18.09.2020 Добавить описание. + * + * @author upagge 18.09.2020 + */ +@FunctionalInterface +public interface SendPreProcessing { + + String pretreatment(String messageText); + +} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4219ff1 --- /dev/null +++ b/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + org.sadtech.telegram + telegram + pom + 0.0.1-DEV + + bot + + + + \ No newline at end of file