Skip to content

Commit

Permalink
Alpha (#36)
Browse files Browse the repository at this point in the history
* Beta 5 add vagrantfile
* Beta 6 add log (#33)
* Add lombok logs
* Beta 7 add jakson (#34)
* Add jakson
* Add java 20
* Beta 8 move torrent from cli to web api (#35)
  • Loading branch information
halushko authored Oct 23, 2023
1 parent 8300559 commit 87f91e9
Show file tree
Hide file tree
Showing 77 changed files with 1,344 additions and 1,241 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-push-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ jobs:
file: ./config/dockerfiles/Dockerfile-${{ inputs.repository-name }}
push: true
builder: mybuilder
platforms: linux/amd64, linux/arm64, linux/386, linux/arm/v7, linux/arm/v6
platforms: linux/amd64, linux/arm64
tags: ${{ env.IMAGES }}
4 changes: 2 additions & 2 deletions .github/workflows/git-pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ jobs:
build_core:
uses: ./.github/workflows/java-build-core.yml
with:
java-version: '8'
java-version: '20'
build_jars:
strategy:
matrix:
service: [ bot, text, file, torrent ]
uses: ./.github/workflows/java-build-jars.yml
with:
repository-name: ${{ matrix.service }}
java-version: '8'
java-version: '20'
needs: build_core
4 changes: 2 additions & 2 deletions .github/workflows/git-push-in-alpha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ jobs:
build_core:
uses: ./.github/workflows/java-build-core.yml
with:
java-version: '8'
java-version: '20'
build_jars:
strategy:
matrix:
service: [ bot, text, file, torrent ]
uses: ./.github/workflows/java-build-jars.yml
with:
repository-name: ${{ matrix.service }}
java-version: '8'
java-version: '20'
needs: build_core
get_alpha_tags:
strategy:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/git-push-in-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:
build_core:
uses: ./.github/workflows/java-build-core.yml
with:
java-version: '8'
java-version: '20'
build_jars:
strategy:
matrix:
service: [ bot, text, file, torrent ]
uses: ./.github/workflows/java-build-jars.yml
with:
repository-name: ${{ matrix.service }}
java-version: '8'
java-version: '20'
needs: build_core
get_beta_tags:
strategy:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/git-push-in-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ jobs:
build_core:
uses: ./.github/workflows/java-build-core.yml
with:
java-version: '8'
java-version: '20'
build_jars:
strategy:
matrix:
service: [ bot, text, file, torrent ]
uses: ./.github/workflows/java-build-jars.yml
with:
repository-name: ${{ matrix.service }}
java-version: '8'
java-version: '20'
needs: build_core
get_master_tags:
strategy:
Expand Down
10 changes: 8 additions & 2 deletions bot/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
plugins {
id 'java'
id 'application'
id 'io.freefair.lombok' version '8.3'
}

group 'com.halushko.kotorrent'
version '1.0'

apply plugin: 'java'
apply plugin: 'application'

compileJava.options.encoding = 'UTF-8'

Expand Down Expand Up @@ -32,6 +36,8 @@ dependencies {
implementation 'org.json:json:+'
implementation 'org.slf4j:slf4j-log4j12:+'
implementation fileTree(dir: System.getenv('CORE_LOCATION'), include: ['*.jar'])
implementation 'org.projectlombok:lombok:+'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.1'
}

repositories {
Expand Down
37 changes: 18 additions & 19 deletions bot/src/main/java/com/halushko/kinocat/bot/KoTorrentBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.halushko.kinocat.bot.handlers.telegram.TextHandler;
import com.halushko.kinocat.bot.handlers.telegram.TorrentFileHandler;
import com.halushko.kinocat.core.handlers.telegram.UserMessageHandler;
import org.apache.log4j.Logger;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
Expand All @@ -15,40 +15,41 @@

import java.util.*;

import static com.halushko.kinocat.core.rabbit.RabbitJson.unNormalizeText;

@SuppressWarnings("deprecation")
@Slf4j
public class KoTorrentBot extends TelegramLongPollingBot {
public static KoTorrentBot BOT;

static {
for (Map.Entry<String, String> a : System.getenv().entrySet()) {
Logger.getRootLogger().debug(a.getKey() + " = [" + a.getValue() + "]");
log.debug(a.getKey() + " = [" + a.getValue() + "]");
}
}

public static final String BOT_NAME = System.getenv("BOT_NAME");

public static final String BOT_TOKEN = System.getenv("BOT_TOKEN");

public static final String BOT_TRUSTED_USERS = System.getenv("BOT_TRUSTED_USERS");

private static final Set<Long> trustedUserIds = new HashSet<>();

private static final Collection<UserMessageHandler> handlers = new ArrayList<UserMessageHandler>() {{
private static final Collection<UserMessageHandler> handlers = new ArrayList<>() {{
add(new MyPingHandler());
add(new TextHandler());
add(new TorrentFileHandler());
}};

public static void main(String[] args) {
Logger.getRootLogger().debug("Bot starting");
log.debug("Bot starting");
try {
TelegramBotsApi botapi = new TelegramBotsApi(DefaultBotSession.class);
BOT = new KoTorrentBot();
new Thread(new SendTextMessageToUser()).start();
botapi.registerBot(BOT);
Logger.getRootLogger().debug("Bot has been started");
log.debug("Bot has been started");
} catch (Exception e) {
Logger.getRootLogger().error("Bot start has been fail: ", e);
log.error("Bot start has been fail: ", e);
}
}

Expand All @@ -75,7 +76,7 @@ public static boolean validateUser(long userId) {
parseTrustedUsersEnv();
}
boolean valid = trustedUserIds.contains(userId);
Logger.getRootLogger().debug(String.format("[validateUser] The user %s is %svalid", userId, valid ? "" : "in"));
log.debug("[validateUser] The user {} is {}valid", userId, valid ? "" : "in");
return valid;
}

Expand All @@ -84,15 +85,14 @@ public static void sendText(long chatId, String str) {
return;
}
try {
final String text = unNormalizeText(str);
Logger.getRootLogger().debug(String.format("[BOT.sendText] Send text chatId:%s, text:%s", chatId, text));
log.debug("[BOT.sendText] Send text chatId:{}, text:{}", chatId, str);
BOT.execute(new SendMessage() {{
setChatId(chatId);
setText(text);
setText(str);
}}
);
} catch (TelegramApiException ex) {
Logger.getRootLogger().error("Can't send text message to user: ", ex);
log.error("Can't send text message to user: ", ex);
}
}

Expand All @@ -101,14 +101,13 @@ public static void sendText(long chatId, Collection<String> str) {
}

private static void parseTrustedUsersEnv() {
String[] userIds = BOT_TRUSTED_USERS.split(",");
for (String userId : userIds) {
Arrays.stream(BOT_TRUSTED_USERS.split(",")).forEach(userId -> {
try {
trustedUserIds.add(new Long(userId));
Logger.getRootLogger().warn(String.format("[BOT.parseTrustedUsersEnv] User ID '%s' is trusted", userId));
trustedUserIds.add(Long.parseLong(userId));
log.warn(String.format("[BOT.parseTrustedUsersEnv] User ID '%s' is trusted", userId));
} catch (Exception e) {
Logger.getRootLogger().warn(String.format("[BOT.parseTrustedUsersEnv] User ID '%s' is invalid", userId));
log.warn(String.format("[BOT.parseTrustedUsersEnv] User ID '%s' is invalid", userId));
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.handlers.input.InputMessageHandler;
import com.halushko.kinocat.core.rabbit.RabbitMessage;
import org.apache.log4j.Logger;
import com.halushko.kinocat.core.rabbit.SmartJson;
import lombok.extern.slf4j.Slf4j;

import static com.halushko.kinocat.bot.KoTorrentBot.sendText;
import static com.halushko.kinocat.core.rabbit.RabbitJson.unNormalizeText;

@Slf4j
public class SendTextMessageToUser extends InputMessageHandler {
@Override
protected void getDeliverCallbackPrivate(RabbitMessage message) {
protected void getDeliverCallbackPrivate(SmartJson message) {
long chatId = message.getUserId();
String text = unNormalizeText(message.getText());
Logger.getRootLogger().debug(String.format("[SendTextMessageToUser] Send text chatId:%s, text:%s", chatId, text));
String text = message.getText();
log.debug("[SendTextMessageToUser] Send text chatId:{}, text:{}", chatId, text);
sendText(chatId, text);
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.handlers.telegram.UserMessageHandler;
import com.halushko.kinocat.core.rabbit.RabbitJson;
import com.halushko.kinocat.core.rabbit.RabbitMessage;
import com.halushko.kinocat.core.rabbit.RabbitUtils;
import org.apache.log4j.Logger;
import com.halushko.kinocat.core.rabbit.SmartJson;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.objects.Update;

@Slf4j
public class TextHandler extends UserMessageHandler {
@Override
protected void readMessagePrivate(Update update) {
long chatId = update.getMessage().getChatId();
String message = RabbitJson.normalizedValue(update.getMessage().getText());
String message = update.getMessage().getText();

Logger.getRootLogger().debug(String.format("[TextHandler] chatId:%s, message:%s", chatId, message));
log.debug("[TextHandler] chatId:{}, message:{}", chatId, message);

RabbitMessage rm = new RabbitMessage(chatId, message);
SmartJson rm = new SmartJson(chatId, message);
RabbitUtils.postMessage(rm, Constants.Queues.Telegram.TELEGRAM_INPUT_TEXT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import com.halushko.kinocat.bot.KoTorrentBot;
import com.halushko.kinocat.core.cli.Constants;
import com.halushko.kinocat.core.handlers.telegram.UserMessageHandler;
import com.halushko.kinocat.core.rabbit.RabbitMessage;
import com.halushko.kinocat.core.rabbit.SmartJson;
import com.halushko.kinocat.core.rabbit.RabbitUtils;
import org.apache.log4j.Logger;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.GetFile;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import static com.halushko.kinocat.core.rabbit.RabbitJson.normalizedValue;
import static com.halushko.kinocat.core.rabbit.RabbitMessage.KEYS.*;
import static com.halushko.kinocat.core.rabbit.SmartJson.KEYS.*;

@Slf4j
public class TorrentFileHandler extends UserMessageHandler {
@Override
protected void readMessagePrivate(Update update) {
Expand All @@ -22,7 +22,7 @@ protected void readMessagePrivate(Update update) {
String mimeType = update.getMessage().getDocument().getMimeType();
String message = update.getMessage().getText();
String caption = update.getMessage().getCaption();
Logger.getRootLogger().debug(
log.debug(
String.format("[TorrentFileHandler] chatId:%s, uploadedFileId:%s, fileName:%s, message:%s, caption:%s",
chatId, uploadedFileId, update.getMessage().getDocument().getFileName(), message, caption
)
Expand All @@ -32,17 +32,17 @@ protected void readMessagePrivate(Update update) {
uploadedFile.setFileId(uploadedFileId);

try {
RabbitMessage rm = new RabbitMessage(chatId);
rm.addValue(FILE_PATH, KoTorrentBot.BOT.execute(uploadedFile).getFilePath());
rm.addValue("FILE_ID", update.getMessage().getDocument().getFileId());
rm.addValue(TEXT, normalizedValue(message));
rm.addValue("CAPTION", caption);
rm.addValue("SIZE", String.valueOf(fileSize));
rm.addValue("MIME_TYPE", mimeType);
SmartJson rm = new SmartJson(chatId).
addValue(FILE_PATH, KoTorrentBot.BOT.execute(uploadedFile).getFilePath()).
addValue("FILE_ID", update.getMessage().getDocument().getFileId()).
addValue(TEXT, message).
addValue("CAPTION", caption).
addValue("SIZE", String.valueOf(fileSize)).
addValue("MIME_TYPE", mimeType);

RabbitUtils.postMessage(rm, Constants.Queues.Telegram.TELEGRAM_INPUT_FILE);
} catch (TelegramApiException e) {
Logger.getRootLogger().error("[TorrentFileHandler] Error during file processing", e);
log.error("[TorrentFileHandler] Error during file processing", e);
KoTorrentBot.sendText(chatId, e.getMessage());
}
}
Expand Down
24 changes: 24 additions & 0 deletions bot/src/main/resources/log_properties/log4j-debug.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
log4j.logger.com.halushko.kinocat.core=ALL, CORE
log4j.appender.CORE=org.apache.log4j.RollingFileAppender
log4j.appender.CORE.File=/home/app/logs/bot/core.log
log4j.appender.CORE.MaxFileSize=10240Kb
log4j.appender.CORE.MaxBackupIndex=5
log4j.appender.CORE.layout=org.apache.log4j.PatternLayout
log4j.appender.CORE.layout.conversionPattern=%d{dd.MM.yyyy HH\\\:mm\\\:ss.SSS} [%-5p] <%t> <%c{2}> - %m%n

log4j.logger.com.halushko.kinocat.bot=ALL, BOT
log4j.appender.BOT=org.apache.log4j.RollingFileAppender
log4j.appender.BOT.File=/home/app/logs/bot/app.log
log4j.appender.BOT.MaxFileSize=10240Kb
log4j.appender.BOT.MaxBackupIndex=5
log4j.appender.BOT.layout=org.apache.log4j.PatternLayout
log4j.appender.BOT.layout.conversionPattern=%d{dd.MM.yyyy HH\\\:mm\\\:ss.SSS} [%-5p] <%t> <%c{2}> - %m%n

log4j.logger.com=ALL, OTHER
log4j.logger.org=ALL, OTHER
log4j.appender.OTHER=org.apache.log4j.RollingFileAppender
log4j.appender.OTHER.File=/home/app/logs/bot/other.log
log4j.appender.OTHER.MaxFileSize=10240Kb
log4j.appender.OTHER.MaxBackupIndex=5
log4j.appender.OTHER.layout=org.apache.log4j.PatternLayout
log4j.appender.OTHER.layout.conversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS} [%-5p] <%t> <%c{2}> - %m%n
24 changes: 24 additions & 0 deletions bot/src/main/resources/log_properties/log4j-info.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
log4j.logger.com.halushko.kinocat.core=INFO, CORE
log4j.appender.CORE=org.apache.log4j.RollingFileAppender
log4j.appender.CORE.File=/home/app/logs/bot/core.log
log4j.appender.CORE.MaxFileSize=10240Kb
log4j.appender.CORE.MaxBackupIndex=5
log4j.appender.CORE.layout=org.apache.log4j.PatternLayout
log4j.appender.CORE.layout.conversionPattern=%d{dd.MM.yyyy HH\\\:mm\\\:ss.SSS} [%-5p] <%t> <%c{2}> - %m%n

log4j.logger.com.halushko.kinocat.bot=INFO, BOT
log4j.appender.BOT=org.apache.log4j.RollingFileAppender
log4j.appender.BOT.File=/home/app/logs/bot/app.log
log4j.appender.BOT.MaxFileSize=10240Kb
log4j.appender.BOT.MaxBackupIndex=5
log4j.appender.BOT.layout=org.apache.log4j.PatternLayout
log4j.appender.BOT.layout.conversionPattern=%d{dd.MM.yyyy HH\\\:mm\\\:ss.SSS} [%-5p] <%t> <%c{2}> - %m%n

log4j.logger.com=INFO, OTHER
log4j.logger.org=INFO, OTHER
log4j.appender.OTHER=org.apache.log4j.RollingFileAppender
log4j.appender.OTHER.File=/home/app/logs/bot/other.log
log4j.appender.OTHER.MaxFileSize=10240Kb
log4j.appender.OTHER.MaxBackupIndex=5
log4j.appender.OTHER.layout=org.apache.log4j.PatternLayout
log4j.appender.OTHER.layout.conversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS} [%-5p] <%t> <%c{2}> - %m%n
3 changes: 3 additions & 0 deletions bot/src/main/resources/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

java -Dlog4j.configuration=file:/home/app/log4j-${LOGS_LEVEL}.properties -jar /home/app/app.jar
7 changes: 5 additions & 2 deletions config/dockerfiles/Dockerfile-bot
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
FROM openjdk:8-alpine
FROM openjdk:20-ea-35-jdk-slim
COPY bot-*.jar /home/app/app.jar
ENTRYPOINT ["java", "-jar", "/home/app/app.jar"]
COPY ./src/main/resources/log_properties /home/app
COPY ./src/main/resources/scripts /home/app
RUN chmod +X /home/app/*.sh
ENTRYPOINT ["sh", "/home/app/run.sh"]
7 changes: 5 additions & 2 deletions config/dockerfiles/Dockerfile-file
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
FROM openjdk:8-alpine
FROM openjdk:20-ea-35-jdk-slim
COPY file-*.jar /home/app/app.jar
ENTRYPOINT ["java", "-jar", "/home/app/app.jar"]
COPY ./src/main/resources/log_properties /home/app
COPY ./src/main/resources/scripts /home/app
RUN chmod +X /home/app/*.sh
ENTRYPOINT ["sh", "/home/app/run.sh"]
Loading

0 comments on commit 87f91e9

Please sign in to comment.