diff options
author | AlisaLinUwU <alisalinuwu@gmail.com> | 2025-01-26 11:43:33 +0500 |
---|---|---|
committer | AlisaLinUwU <alisalinuwu@gmail.com> | 2025-01-26 11:43:33 +0500 |
commit | a27c7763ed6a4f094ab65c49f176a2d9bde09f89 (patch) | |
tree | 8c48e103f3bfc09d61ce2c231ba6d392221dbb0a /src/main/java/info/selflearner/ocr_telegram/Application.java |
Initializemain
Diffstat (limited to 'src/main/java/info/selflearner/ocr_telegram/Application.java')
-rw-r--r-- | src/main/java/info/selflearner/ocr_telegram/Application.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/main/java/info/selflearner/ocr_telegram/Application.java b/src/main/java/info/selflearner/ocr_telegram/Application.java new file mode 100644 index 0000000..eacdfd4 --- /dev/null +++ b/src/main/java/info/selflearner/ocr_telegram/Application.java @@ -0,0 +1,93 @@ +package info.selflearner.ocr_telegram; + +import com.pengrad.telegrambot.TelegramBot; +import com.pengrad.telegrambot.UpdatesListener; +import com.pengrad.telegrambot.model.PhotoSize; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.GetFile; +import com.pengrad.telegrambot.request.SendMessage; +import com.pengrad.telegrambot.response.GetFileResponse; +import info.selflearner.ocr_telegram.exception.PassportNotFoundException; +import info.selflearner.ocr_telegram.model.Passport; +import info.selflearner.ocr_telegram.util.FileContentReader; +import info.selflearner.ocr_telegram.util.OCR; + +import java.io.*; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; +import java.util.ResourceBundle; + +public class Application { + + public static void main(String[] args) { + TelegramBot bot = new TelegramBot(ResourceBundle.getBundle("secrets").getString("BOT_TOKEN")); + + bot.setUpdatesListener(updateList -> { + if (updateList.getFirst().message() != null && updateList.getFirst().message().text() != null) { + long chatId = updateList.getFirst().message().chat().id(); + + bot.execute(new SendMessage(chatId, "Message received: " + updateList.getFirst().message().text())); + } + + if (updateList.getFirst().message() != null && updateList.getFirst().message().photo() != null) { + long chatId = updateList.getFirst().message().chat().id(); + + PhotoSize[] photoSizes = updateList.getFirst().message().photo(); + PhotoSize highestQualityPhotoSize = photoSizes[photoSizes.length - 1]; + GetFile request = new GetFile(highestQualityPhotoSize.fileId()); + GetFileResponse getFileResponse = bot.execute(request); + + try { + URL photoURL = new URI(bot.getFullFilePath(getFileResponse.file())).toURL(); + + ReadableByteChannel readableByteChannel = Channels.newChannel(photoURL.openStream()); + File photo = new File(System.getProperty("java.io.tmpdir") + "/ocr-telegram/" + getFileResponse.file().filePath()); + File ocrFile = new File(System.getProperty("java.io.tmpdir") + "/ocr-telegram/ocr/" + getFileResponse.file().filePath().split("/")[1] + ".txt"); + + bot.execute(new SendMessage(chatId, "Processing, please wait...")); + + new File(System.getProperty("java.io.tmpdir") + "/ocr-telegram/photos/").mkdirs(); + new File(System.getProperty("java.io.tmpdir") + "/ocr-telegram/ocr/").mkdirs(); + + if (!photo.exists()) { + try (FileOutputStream fileOutputStream = new FileOutputStream(photo)) { + FileChannel fileChannel = fileOutputStream.getChannel(); + fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE); + } catch (IOException ioException) { + throw new RuntimeException(ioException); + } + } + if (!ocrFile.exists()) { + Passport passport = OCR.performPassport(photo); + + try (BufferedWriter writer = new BufferedWriter(new FileWriter(ocrFile))) { + writer.write(passport.toString()); + } + } + + bot.execute(new SendMessage(chatId, FileContentReader.read(ocrFile)).parseMode(ParseMode.Markdown)); + } catch (URISyntaxException | IOException | InterruptedException e) { + throw new RuntimeException(e); + } catch (PassportNotFoundException e) { + bot.execute(new SendMessage(chatId, "Passport not found!")); + } + } + + return UpdatesListener.CONFIRMED_UPDATES_ALL; + }, telegramException -> { + if (telegramException.response() != null) { + // got bad response from telegram + telegramException.response().errorCode(); + telegramException.response().description(); + } else { + // probably network error + telegramException.printStackTrace(); + } + }); + } + +}
\ No newline at end of file |