summaryrefslogtreecommitdiff
path: root/src/main/java/com/mavlushechka/animarfo/telegram/bot/Bot.java
blob: cc24a72cbd167256789be26c105f264873c238bb (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
package com.mavlushechka.animarfo.telegram.bot;

import com.mavlushechka.animarfo.App;
import com.mavlushechka.animarfo.anime.Anime;
import com.mavlushechka.animarfo.parser.Parser;
import com.mavlushechka.animarfo.telegram.user.TelegramUser;
import com.mavlushechka.animarfo.telegram.user.callbackquery.UsersCallbackQuery;
import com.mavlushechka.animarfo.telegram.user.keyboard.UserKeyboard;
import com.mavlushechka.animarfo.telegram.user.message.UserMessage;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.CallbackQuery;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SendPhoto;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Bot {
    public final static TelegramBot TELEGRAM_BOT = new TelegramBot(App.PROPERTIES.getProperty("BOT_TOKEN"));
    public static TelegramUser telegramUser = new TelegramUser();
    private static final Logger LOGGER = Logger.getLogger(Bot.class.getName());

    static {
        LOGGER.setLevel(Level.ALL);
    }

    public static void start() {
        LOGGER.fine("Bot is started");
        TELEGRAM_BOT.setUpdatesListener(updates -> {
            updates.forEach(update -> {
                if (update.callbackQuery() != null) {
                    try {
                        UsersCallbackQuery.execute(update.callbackQuery());
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                } else if (update.message() != null) {
                    telegramUser = TelegramUser.getInstance(update.message().from(), update.message().date());

                    try {
                        UserMessage.execute(update.message());
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
            });
            return UpdatesListener.CONFIRMED_UPDATES_ALL;
        });
    }

    public static void sendAnime() throws IOException {
        String searchedAnime = Anime.searched;
        byte animeAtSearchSize = Parser.getSizeOfSearchedAnime("https://anime.anidub.life/?do=search&mode=advanced&subaction=search&titleonly=3&story=" + searchedAnime);

        if (animeAtSearchSize > 0) {
            Anime[] anime = new Anime[animeAtSearchSize];
            byte index = Anime.index;
            try {
                anime = Parser.getAnime("https://anime.anidub.life/?do=search&mode=advanced&subaction=search&titleonly=3&story=" + searchedAnime);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

            Anime.size = animeAtSearchSize;
            Anime.setList(anime);
            TELEGRAM_BOT.execute(new SendPhoto(UserMessage.chatId, anime[index].getImage()).caption(anime[index].showInfo()).parseMode(ParseMode.HTML).replyMarkup(UserKeyboard.getCarousel()));
        } else {
            TELEGRAM_BOT.execute(new SendMessage(UserMessage.chatId, "Прости, но я не смог найти аниме с таким названием...\nДавай поищем другое аниме!"));
            Anime.isSearching = true;
        }
    }

    public static void sendAnime(String url) {
        Anime[] anime = new Anime[28];
        byte index = Anime.index;
        try {
            anime = Parser.getAnime(url);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        Anime.setList(anime);
        TELEGRAM_BOT.execute(new SendPhoto(UserMessage.chatId, anime[index].getImage()).caption(anime[index].showInfo()).parseMode(ParseMode.HTML).replyMarkup(UserKeyboard.getCarousel()));
    }

    public static void sendAnime(CallbackQuery callbackQuery) {
        Anime[] anime = Anime.list;
        byte index = Anime.index;

        TELEGRAM_BOT.execute(new SendPhoto(callbackQuery.message().chat().id(), anime[index].getImage()).caption(anime[index].showInfo()).parseMode(ParseMode.HTML).replyMarkup(UserKeyboard.getCarousel()));
    }
}