summaryrefslogtreecommitdiff
path: root/src/main/java/files/Bot.java
blob: a533ebdb25de967cddf04e0d8a9df0f217acb2b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.request.SendMessage;
import com.pengrad.telegrambot.request.SendPhoto;

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 -> {
                if (UserCallbackQuery.checkIfNotNull(update.callbackQuery())) {
                    UserCallbackQuery.setInformation(update.callbackQuery());
                    UserCallbackQuery.execute(telegramBot);
                } else if (update.message() != null && update.message().text().equals("Поиск аниме")) {
                    Anime.setIndexOfAnime((byte) 0);
                    Anime.setIsSearchingAnAnime(true);
                    telegramBot.execute(new SendMessage(update.message().chat().id(), "Введи название аниме, которое хочешь найти..."));
                } else if (update.message() != null && update.message().text().equals("Недавно добавленные аниме")) {
                    Anime.setIndexOfAnime((byte) 0);
                    Anime.setIsSearchingAnAnime(false);
                    try {
                        Bot.showRecentlyAddedAnime(telegramBot, update, Anime.getIndexOfAnime());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else if (update.message() != null && update.message().text().equals("Новинки аниме")) {
                    Anime.setIndexOfAnime((byte) 0);
                    Anime.setIsSearchingAnAnime(false);
                    try {
                        Bot.showRecentlyReleasedAnime(telegramBot, update, Anime.getIndexOfAnime());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else if (update.message() != null && update.message().text().equals("/start")) {
//                    DataBase.saveUser(update);
                    Anime.setIsSearchingAnAnime(false);
                    telegramBot.execute(new SendMessage(update.message().chat().id(), ("Приветики " + update.message().from().firstName() + ", меня звать AniMarfo!\nЯ помогу тебе найти аниме, скачать его или же смотреть прямо тут.")).replyMarkup(MyKeyboard.getMain()));
                } else if (update.message() != null) {
                    if (Anime.getIsSearchingAnAnime()) {
                        Anime.setSearchedAnime(update.message().text());
                        Anime.setIsSearchingAnAnime(false);
                        try {
                            Bot.showSearchedAnime(telegramBot, update, Anime.getIndexOfAnime(), Anime.getSearchedAnime());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        telegramBot.execute(new SendMessage(update.message().chat().id(), "Прости, но я тебя не понимаю...\nВоспользуйся командами на твоей клавлиатуре!").replyMarkup(MyKeyboard.getMain()));
                    }
                }
            });
            return UpdatesListener.CONFIRMED_UPDATES_ALL;
        });
    }

    private static void showAnimeAtMainMenu(TelegramBot telegramBot, Update update, byte index, String url, String typeOfAnime) throws IOException {
        Anime[] anime = new Anime[Parser.getAnimeAtSearchSize(url)];
        try {
            anime = Parser.getAnimeAtMainMenu(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Anime.setList(anime);
        telegramBot.execute(new SendPhoto(update.message().chat().id(), anime[index].getImage()).caption(anime[index].showInfo()).replyMarkup(MyKeyboard.getCarousel(typeOfAnime)));
    }

    private static void showAnimeAtMainMenuCallBackQuery(TelegramBot telegramBot, CallbackQuery callbackQuery, byte index, String typeOfAnime, Anime[] list) {
        telegramBot.execute(new SendPhoto(callbackQuery.message().chat().id(), list[index].getImage()).caption(list[index].showInfo()).replyMarkup(MyKeyboard.getCarousel(typeOfAnime)));
   }

    public static void showSearchedAnime(TelegramBot telegramBot, Update update, byte index, String searchedAnime) throws IOException {
        if (Parser.getAnimeAtSearchSize("https://animego.org/search/all?q=" + searchedAnime) > 0) {
            Anime[] anime = new Anime[Parser.getAnimeAtSearchSize("https://animego.org/search/all?q=" + searchedAnime)];
            try {
                anime = Parser.getAnimeAtSearch("https://animego.org/search/all?q=" + searchedAnime);
            } catch (IOException e) {
                e.printStackTrace();
            }
            telegramBot.execute(new SendPhoto(update.message().chat().id(), anime[index].getImage()).caption(anime[index].showInfo()).replyMarkup(MyKeyboard.getCarousel("SearchedAnime")));
        } else {
            telegramBot.execute(new SendMessage(update.message().chat().id(), "Прости, но я не смог найти аниме с таким названием...\nДавай поищем другое аниме!"));
            Anime.setIsSearchingAnAnime(true);
        }
    }

    public static void showSearchedAnimeCallBackQuery(TelegramBot telegramBot, CallbackQuery callbackQuery, byte index, String searchedAnime) {
        Anime[] anime = new Anime[20];
        try {
            anime = Parser.getAnimeAtSearch("https://animego.org/search/all?q=" + searchedAnime);
        } catch (IOException e) {
            e.printStackTrace();
        }
        telegramBot.execute(new SendPhoto(callbackQuery.message().chat().id(), anime[index].getImage()).caption(anime[index].showInfo()).replyMarkup(MyKeyboard.getCarousel("SearchedAnime")));
    }

    public static void showRecentlyAddedAnime(TelegramBot telegramBot, Update update, byte index) throws IOException {
        showAnimeAtMainMenu(telegramBot, update, index, "https://animego.org/anime?sort=a.createdAt&direction=desc", "RecentlyAddedAnime");
    }

    public static void showRecentlyAddedAnimeCallBackQuery(TelegramBot telegramBot, CallbackQuery callbackQuery, byte index) {
        showAnimeAtMainMenuCallBackQuery(telegramBot, callbackQuery, index,"RecentlyAddedAnime", Anime.getList());
    }

    public static void showRecentlyReleasedAnime(TelegramBot telegramBot, Update update, byte index) throws IOException {
        showAnimeAtMainMenu(telegramBot, update, index, "https://animego.org/anime?sort=a.startDate&direction=desc", "RecentlyReleasedAnime");
    }

    public static void showRecentlyReleasedAnimeCallBackQuery(TelegramBot telegramBot, CallbackQuery callbackQuery, byte index) {
        showAnimeAtMainMenuCallBackQuery(telegramBot, callbackQuery, index,"RecentlyReleasedAnime", Anime.getList());
    }
}