Bot class:
public class Bot extends TelegramLongPollingBot { private long chatId; Document document; Parser parser = new Parser(); public void onUpdateReceived(Update update) { update.getUpdateId(); chatId = update.getMessage().getChatId(); Message msg = update.getMessage(); if (msg != null && msg.hasText()) { //if(msg.getText().equals("/start")) { SendMessage sendMessage = new SendMessage().setChatId(chatId); try { sendMessage.setText(parser.ParseText(msg.getText())); execute(sendMessage); } catch (TelegramApiException e) { e.printStackTrace(); } SendPhoto sendPhoto = new SendPhoto().setChatId(chatId); try { sendPhoto.setPhoto(parser.ParseImg(msg.getText())); execute(sendPhoto); } catch (TelegramApiException e) { e.printStackTrace(); } } } public String getBotUsername() { return "@ParseSteamBot"; } @Override public String getBotToken() { return "..."; } }
Parser class:
public File ParseImg(String href) { File file; try { document = Jsoup.connect(href).get(); Elements img = document.getElementsByClass("game_header_image_full"); try(InputStream in = new URL(img.attr("src")).openStream()){ Files.copy(in, Paths.get("C\\images\\")); return new File("C\\images\\"); } catch(IOException e){ e.printStackTrace(); } } catch(IOException e){ System.out.println("Image not found..."); e.printStackTrace(); } return null; }
Errors occur in rows with a try ... catch block
.java.nio.file.NoSuchFileException: C\images at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230) at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434) at java.nio.file.Files.newOutputStream(Files.java:216) at java.nio.file.Files.copy(Files.java:3016) at Parser.ParseImg(Parser.java:60) at Bot.onUpdateReceived(Bot.java:36) at java.util.ArrayList.forEach(ArrayList.java:1249) at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27) at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:306) мая 06, 2019 6:55:59 PM org.telegram.telegrambots.meta.logging.BotLogger severe SEVERE: BOTSESSION java.lang.NullPointerException: file cannot be null! at java.util.Objects.requireNonNull(Objects.java:228) at org.telegram.telegrambots.meta.api.methods.send.SendPhoto.setPhoto(SendPhoto.java:111) at Bot.onUpdateReceived(Bot.java:36) at java.util.ArrayList.forEach(ArrayList.java:1249) at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27) at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:306)
Although at the first launch the code worked and uploaded one image. I think this is server blocking due to frequent requests.