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