summaryrefslogtreecommitdiff
path: root/src/main/java/files/Bot.java
blob: fb39ebc13e49a197805aafb62710f7e2b52f14cb (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
package files;

import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.UpdatesListener;
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.SendPhoto;
import files.entity.TelegramUser;

import javax.persistence.EntityManager;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 -> {
                DataBase.saveUser(update);
                Bot.showRecentlyAddedAnime(telegramBot, update, (byte) 5);
            });
            return UpdatesListener.CONFIRMED_UPDATES_ALL;
        });
    }

    public static Keyboard getMainKeyboard() {
        return new ReplyKeyboardMarkup(
                new String[]{"Поиск аниме"},
                new String[]{"Случайное аниме"})
                .oneTimeKeyboard(true)
                .resizeKeyboard(true)
                .selective(true);
    }

    public static Keyboard getCarouselKeyboard() {
        return new InlineKeyboardMarkup(
                new InlineKeyboardButton("<-").callbackData("callback_data"),
                new InlineKeyboardButton("->").callbackData("callback_data"));
    }

    public static void showRecentlyAddedAnime(TelegramBot bot, Update update, byte index) {
        Anime[] anime = new Anime[0];
        try {
            anime = Parser.getAnimeAtMainMenu("https://animego.org/anime?sort=a.createdAt&direction=desc");
        } catch (IOException e) {
            e.printStackTrace();
        }
        bot.execute(new SendPhoto(update.message().chat().id(), anime[index].getImage()).caption(anime[index].showInfo()).replyMarkup(Bot.getCarouselKeyboard()));
    }
}