|
| 1 | +package ru.javazen.telegram.bot.help; |
| 2 | + |
| 3 | +import org.springframework.stereotype.Component; |
| 4 | +import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup; |
| 5 | +import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton; |
| 6 | +import org.yaml.snakeyaml.Yaml; |
| 7 | +import ru.javazen.telegram.bot.util.MessageData; |
| 8 | + |
| 9 | +import javax.annotation.PostConstruct; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +@Component |
| 17 | +public class HelpInfoProvider { |
| 18 | + |
| 19 | + private static final String HELP_FILE = "help/help.yml"; |
| 20 | + |
| 21 | + private Help help; |
| 22 | + |
| 23 | + public MessageData getHelpMessage() { |
| 24 | + StringBuilder responseText = new StringBuilder(help.getWelcome()); |
| 25 | + responseText.append("\n"); |
| 26 | + |
| 27 | + List<List<InlineKeyboardButton>> buttonsLines = new ArrayList<>(); |
| 28 | + List<InlineKeyboardButton> buttons = new ArrayList<>(); |
| 29 | + |
| 30 | + for (Help.Feature feature : help.getFeatures()) { |
| 31 | + responseText.append(" *").append(feature.getTitle()).append("*") |
| 32 | + .append(" - ").append(feature.getDescription()).append("\n"); |
| 33 | + |
| 34 | + InlineKeyboardButton button = new InlineKeyboardButton(); |
| 35 | + button.setText(feature.getTitle()); |
| 36 | + button.setCallbackData("help:feature:" + feature.getTitle()); |
| 37 | + buttons.add(button); |
| 38 | + if (buttons.size() >= 3) { |
| 39 | + buttonsLines.add(buttons); |
| 40 | + buttons = new ArrayList<>(); |
| 41 | + } |
| 42 | + } |
| 43 | + if (buttons.size() > 0) { |
| 44 | + buttonsLines.add(buttons); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + InlineKeyboardMarkup inlineKeyboard = new InlineKeyboardMarkup(); |
| 49 | + inlineKeyboard.setKeyboard(buttonsLines); |
| 50 | + |
| 51 | + return new MessageData(responseText.toString(), inlineKeyboard); |
| 52 | + } |
| 53 | + |
| 54 | + public MessageData getFeatureInformation(String featureName) { |
| 55 | + for (Help.Feature feature : help.getFeatures()) { |
| 56 | + if (feature.getTitle().equals(featureName)) { |
| 57 | + String text = "`" + feature.getTitle() + "` - " + feature.getDescription(); |
| 58 | + text += "\n"; |
| 59 | + text += "\n"; |
| 60 | + text += feature.getDetails(); |
| 61 | + |
| 62 | + InlineKeyboardMarkup inlineKeyboard = new InlineKeyboardMarkup(); |
| 63 | + InlineKeyboardButton button = new InlineKeyboardButton(); |
| 64 | + button.setText("<< Вернуться к списку"); |
| 65 | + button.setCallbackData("help:list"); |
| 66 | + inlineKeyboard.setKeyboard(Collections.singletonList(Collections.singletonList(button))); |
| 67 | + |
| 68 | + return new MessageData(text, inlineKeyboard); |
| 69 | + } |
| 70 | + } |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + @PostConstruct |
| 75 | + protected void loadHelp() throws IOException { |
| 76 | + Yaml yaml = new Yaml(); |
| 77 | + |
| 78 | + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); |
| 79 | + try (InputStream in = classloader.getResourceAsStream(HELP_FILE)) { |
| 80 | + help = yaml.loadAs(in, Help.class); |
| 81 | + |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments