package files; import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.UpdatesListener; import com.pengrad.telegrambot.model.CallbackQuery; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.model.request.InlineKeyboardButton; import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup; import com.pengrad.telegrambot.model.request.Keyboard; import com.pengrad.telegrambot.model.request.ReplyKeyboardMarkup; import com.pengrad.telegrambot.request.DeleteMessage; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.request.SendPhoto; import com.pengrad.telegrambot.response.BaseResponse; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class Bot { public static void main(String[] args) throws IOException { // Loading properties Properties properties = new Properties(); properties.load(new FileInputStream("app.properties")); // Creating telegram bot TelegramBot telegramBot = new TelegramBot(properties.getProperty("telegram_token")); telegramBot.setUpdatesListener(updates -> { updates.forEach(update -> { byte indexOfRecentlyAddedAnime = Anime.getIndexOfRecentlyAddedAnime(); if (update.callbackQuery() != null && update.message() != null) { CallbackQuery callbackQuery = update.callbackQuery(); String data = callbackQuery.data(); User user = callbackQuery.from(); int messageId = callbackQuery.message().messageId(); long chatId = callbackQuery.message().chat().id(); System.out.println(callbackQuery.toString()); if (data.equals("previous")) { Anime.decreaseIndexOfRecentlyAddedAnime(); if (indexOfRecentlyAddedAnime == -1) Anime.setIndexOfRecentlyAddedAnime((byte) 19); Bot.showRecentlyAddedAnimeCallBackQuery(telegramBot, callbackQuery, Anime.getIndexOfRecentlyAddedAnime()); } if (data.equals("next")) { Anime.increaseIndexOfRecentlyAddedAnime(); if (indexOfRecentlyAddedAnime == 20) Anime.setIndexOfRecentlyAddedAnime((byte) 0); Bot.showRecentlyAddedAnimeCallBackQuery(telegramBot, callbackQuery, Anime.getIndexOfRecentlyAddedAnime()); } DeleteMessage deleteMessage = new DeleteMessage(chatId, messageId); BaseResponse response = telegramBot.execute(deleteMessage); } DataBase.saveUser(update); indexOfRecentlyAddedAnime = 0; Anime.setIndexOfRecentlyAddedAnime((byte) 0); Bot.showRecentlyAddedAnime(telegramBot, update, (byte) indexOfRecentlyAddedAnime); }); return UpdatesListener.CONFIRMED_UPDATES_ALL; }); } public static Keyboard getMainKeyboard() { return new ReplyKeyboardMarkup( new String[]{"Поиск аниме"}, new String[]{"Недавно добавленные аниме"}, new String[]{"Случайное аниме"}) .oneTimeKeyboard(true) .resizeKeyboard(true) .selective(true); } public static Keyboard getCarouselKeyboard() { return new InlineKeyboardMarkup( new InlineKeyboardButton("<-").callbackData("previous"), new InlineKeyboardButton("->").callbackData("next")); } public static void showRecentlyAddedAnime(TelegramBot telegramBot, Update update, byte index) { Anime[] anime = new Anime[20]; try { anime = Parser.getAnimeAtMainMenu("https://animego.org/anime?sort=a.createdAt&direction=desc"); } catch (IOException e) { e.printStackTrace(); } telegramBot.execute(new SendPhoto(update.message().chat().id(), anime[index].getImage()).caption(anime[index].showInfo()).replyMarkup(Bot.getCarouselKeyboard())); } public static void showRecentlyAddedAnimeCallBackQuery(TelegramBot telegramBot, CallbackQuery callbackQuery, byte index) { Anime[] anime = new Anime[20]; try { anime = Parser.getAnimeAtMainMenu("https://animego.org/anime?sort=a.createdAt&direction=desc"); } catch (IOException e) { e.printStackTrace(); } telegramBot.execute(new SendPhoto(callbackQuery.message().chat().id(), anime[index].getImage()).caption(anime[index].showInfo()).replyMarkup(Bot.getCarouselKeyboard())); } }