Init commit

This commit is contained in:
uPagge 2020-11-05 01:25:19 +03:00
commit e400942a69
No known key found for this signature in database
GPG Key ID: A76EB343414E022E
17 changed files with 756 additions and 0 deletions

111
.gitignore vendored Normal file
View File

@ -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

61
bot/pom.xml Normal file
View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sadtech.telegram</groupId>
<artifactId>telegram-bot</artifactId>
<version>0.0.1-DEVELOPER</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.sadtech.social</groupId>
<artifactId>social-bot</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>4.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.telegram/telegrambots-abilities -->
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-abilities</artifactId>
<version>4.6</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambotsextensions</artifactId>
<version>4.4.0.1</version>
</dependency>
</dependencies>
<developers>
<developer>
<id>uPagge</id>
<name>Struchkov Mark</name>
<organization>SADTECH</organization>
<email>upagge@mail.ru</email>
</developer>
</developers>
</project>

View File

@ -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
}
}

View File

@ -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);
}

View File

@ -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;
// }
//
//}

View File

@ -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;
}
}

View File

@ -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<Mail> {
public MessageAutoresponderTelegram(
Set<MainUnit> menuUnit, Sending sending,
MessageService<Mail> messageService,
UnitPointerRepository unitPointerRepository
) {
super(menuUnit, sending, messageService, unitPointerRepository);
}
}

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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));
}
}
}

View File

@ -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);
}
}

View File

@ -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<InlineKeyboardButton> 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;
}
}

View File

@ -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);
}

16
pom.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sadtech.telegram</groupId>
<artifactId>telegram</artifactId>
<packaging>pom</packaging>
<version>0.0.1-DEV</version>
<modules>
<module>bot</module>
</modules>
</project>