From 3e094ec72abc94c48d0f63221665effd30ea2391 Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Sat, 8 Apr 2023 17:57:43 +0200 Subject: [PATCH] feat: complete show help button event * Code refactor so that help command and event use the same logic * Fix invasive string rename (ty intellij) * Fix Processor not returning right locale key name * Add tests and add todos for the one remaining --- .../callbackquery/CallbackQueryDI.java | 11 +++ .../telegram/callbackquery/Dispatcher.java | 22 ++--- .../telegram/callbackquery/NotFound.java | 38 ++++++++ .../callbackquery/NotFoundFactory.java | 5 + .../callbackquery/SelectLanguageTutorial.java | 16 +--- .../telegram/callbackquery/ShowHelp.java | 58 +++++++++++- .../mezzotre/telegram/callbackquery/Util.java | 29 ++++++ .../mezzotre/telegram/command/Help.java | 82 ++--------------- .../mezzotre/telegram/command/Processor.java | 2 +- .../mezzotre/telegram/model/Help.java | 91 +++++++++++++++++-- src/main/resources/i18n/message.properties | 6 +- .../resources/i18n/message_en_US.properties | 18 +++- src/main/resources/i18n/message_it.properties | 2 +- .../resources/i18n/message_it_IT.properties | 2 +- src/main/resources/template/telegram/help.vm | 1 + ...SelectLanguageTutorialIntegrationTest.java | 8 +- .../telegram/command/HelpIntegrationTest.java | 9 +- .../telegram/command/ProcessorTest.java | 38 ++++++++ 18 files changed, 311 insertions(+), 127 deletions(-) create mode 100644 src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/NotFound.java create mode 100644 src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/NotFoundFactory.java create mode 100644 src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Util.java create mode 100644 src/test/java/com/github/polpetta/mezzotre/telegram/command/ProcessorTest.java diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/CallbackQueryDI.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/CallbackQueryDI.java index 74c240c..34927ed 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/CallbackQueryDI.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/CallbackQueryDI.java @@ -2,6 +2,7 @@ package com.github.polpetta.mezzotre.telegram.callbackquery; import com.google.inject.AbstractModule; import com.google.inject.Provides; +import com.google.inject.assistedinject.FactoryModuleBuilder; import java.util.HashMap; import java.util.Map; import javax.inject.Named; @@ -9,6 +10,16 @@ import javax.inject.Singleton; public class CallbackQueryDI extends AbstractModule { + @Override + protected void configure() { + super.configure(); + + install( + new FactoryModuleBuilder() + .implement(Processor.class, NotFound.class) + .build(NotFoundFactory.class)); + } + @Provides @Singleton @Named("eventProcessors") diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Dispatcher.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Dispatcher.java index 3d0c3fc..469ed0a 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Dispatcher.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Dispatcher.java @@ -3,8 +3,8 @@ package com.github.polpetta.mezzotre.telegram.callbackquery; import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.BaseRequest; +import java.util.Map; import java.util.Optional; -import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import javax.inject.Inject; @@ -22,15 +22,18 @@ import javax.inject.Singleton; @Singleton public class Dispatcher { - private final Set tgEventProcessors; + private final Map tgEventProcessors; private final Executor threadPool; + private final NotFoundFactory notFoundFactory; @Inject public Dispatcher( - @Named("eventProcessors") Set tgEventProcessors, - @Named("eventThreadPool") Executor threadPool) { + @Named("eventProcessors") Map tgEventProcessors, + @Named("eventThreadPool") Executor threadPool, + NotFoundFactory notFoundFactory) { this.tgEventProcessors = tgEventProcessors; this.threadPool = threadPool; + this.notFoundFactory = notFoundFactory; } /** @@ -50,14 +53,11 @@ public class Dispatcher { .thenComposeAsync( ignored -> Optional.of(callbackQueryContext.getFields().getEvent()) - .flatMap( + .map( eventName -> - tgEventProcessors.stream() - // FIXME this is fucking stupid, why iterate over, just use a map! - // Make mapping at startup then we're gucci for the rest of the run - .filter(processor -> processor.getEventName().equals(eventName)) - .findAny()) - .map(processor -> processor.process(callbackQueryContext, update)) + tgEventProcessors + .getOrDefault(eventName, notFoundFactory.create(eventName)) + .process(callbackQueryContext, update)) .orElse(CompletableFuture.failedFuture(new EventProcessorNotFoundException())), threadPool); } diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/NotFound.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/NotFound.java new file mode 100644 index 0000000..605fd4e --- /dev/null +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/NotFound.java @@ -0,0 +1,38 @@ +package com.github.polpetta.mezzotre.telegram.callbackquery; + +import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; +import com.google.inject.assistedinject.Assisted; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.BaseRequest; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import javax.inject.Inject; +import org.slf4j.Logger; + +public class NotFound implements Processor { + + private final Logger log; + private final String eventName; + + @Inject + public NotFound(Logger logger, @Assisted String eventName) { + this.log = logger; + this.eventName = eventName; + } + + @Override + public String getEventName() { + return "eventNotFound"; + } + + @Override + public CompletableFuture>> process( + CallbackQueryContext callbackQueryContext, Update update) { + log.warn( + "A stray event was detected for callback " + + callbackQueryContext.getId() + + " event name " + + eventName); + return CompletableFuture.completedFuture(Optional.empty()); + } +} diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/NotFoundFactory.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/NotFoundFactory.java new file mode 100644 index 0000000..fac7c21 --- /dev/null +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/NotFoundFactory.java @@ -0,0 +1,5 @@ +package com.github.polpetta.mezzotre.telegram.callbackquery; + +public interface NotFoundFactory { + NotFound create(String eventName); +} diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorial.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorial.java index 7bb327d..c596a17 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorial.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorial.java @@ -3,10 +3,8 @@ package com.github.polpetta.mezzotre.telegram.callbackquery; import com.github.polpetta.mezzotre.i18n.TemplateContentGenerator; import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; import com.github.polpetta.mezzotre.orm.model.query.QCallbackQueryContext; -import com.github.polpetta.mezzotre.orm.model.query.QTgChat; import com.github.polpetta.mezzotre.util.UUIDGenerator; import com.github.polpetta.types.json.CallbackQueryMetadata; -import com.pengrad.telegrambot.model.Message; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineKeyboardButton; import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup; @@ -61,16 +59,7 @@ public class SelectLanguageTutorial implements Processor { CallbackQueryContext callbackQueryContext, Update update) { return CompletableFuture.supplyAsync( () -> - Optional.of(callbackQueryContext.getFields().getTelegramChatId()) - .map(Double::longValue) - // If we're desperate, search in the message for the chat id - .or( - () -> - Optional.ofNullable(update.callbackQuery().message()) - .map(Message::messageId) - .map(Long::valueOf)) - .filter(chatId -> chatId != 0L && chatId != Long.MIN_VALUE) - .flatMap(chatId -> new QTgChat().id.eq(chatId).findOneOrEmpty()) + Util.extractChat(callbackQueryContext, update) .map( tgChat -> { tgChat.setLocale( @@ -119,8 +108,7 @@ public class SelectLanguageTutorial implements Processor { + " entries regarding callback group " + callBackGroupToDelete); - final Optional messageId = - Optional.ofNullable(update.callbackQuery().message()).map(Message::messageId); + final Optional messageId = Util.extractMessageId(update); BaseRequest baseRequest; Optional helpButton = Optional.empty(); if (!tgChat.getHasHelpBeenShown()) { diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ShowHelp.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ShowHelp.java index c7d241f..baeee31 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ShowHelp.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ShowHelp.java @@ -1,15 +1,45 @@ package com.github.polpetta.mezzotre.telegram.callbackquery; import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; +import com.github.polpetta.mezzotre.telegram.model.Help; import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.InlineKeyboardButton; +import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup; +import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.BaseRequest; +import com.pengrad.telegrambot.request.EditMessageText; import com.pengrad.telegrambot.request.SendMessage; +import java.util.Map; import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; +import javax.inject.Inject; +import javax.inject.Named; class ShowHelp implements Processor { public static String EVENT_NAME = "showHelp"; + private final Executor threadPool; + private final Help modelHelp; + private final Map + tgCommandProcessors; + private final Map eventProcessor; + + // FIXME tests + @Inject + public ShowHelp( + @Named("eventThreadPool") Executor threadPool, + com.github.polpetta.mezzotre.telegram.model.Help modelHelp, + @Named("commandProcessor") + Map tgCommandProcessors, + @Named("eventProcessors") + Map + eventProcessor) { + this.threadPool = threadPool; + this.modelHelp = modelHelp; + this.tgCommandProcessors = tgCommandProcessors; + this.eventProcessor = eventProcessor; + } @Override public String getEventName() { @@ -19,8 +49,30 @@ class ShowHelp implements Processor { @Override public CompletableFuture>> process( CallbackQueryContext callbackQueryContext, Update update) { - // TODO implement this method and put `hasHelpBeenShown` in tgChat to false - return CompletableFuture.completedFuture( - Optional.of(new SendMessage(callbackQueryContext.getFields().getTelegramChatId(), "TODO"))); + return CompletableFuture.supplyAsync( + () -> + Util.extractChat(callbackQueryContext, update) + .map( + chat -> { + final String message = modelHelp.getMessage(chat, tgCommandProcessors); + final Optional messageId = Util.extractMessageId(update); + final InlineKeyboardButton[] buttons = + modelHelp.generateInlineKeyBoardButton(chat, eventProcessor); + BaseRequest request; + if (messageId.isPresent()) { + final EditMessageText editMessageText = + new EditMessageText(chat.getId(), messageId.get(), message); + editMessageText.replyMarkup(new InlineKeyboardMarkup(buttons)); + request = editMessageText; + } else { + final SendMessage sendMessage = + new SendMessage(chat.getId(), message).parseMode(ParseMode.Markdown); + sendMessage.replyMarkup(new InlineKeyboardMarkup(buttons)); + request = sendMessage; + } + + return request; + }), + threadPool); } } diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Util.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Util.java new file mode 100644 index 0000000..67ef62f --- /dev/null +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/Util.java @@ -0,0 +1,29 @@ +package com.github.polpetta.mezzotre.telegram.callbackquery; + +import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; +import com.github.polpetta.mezzotre.orm.model.TgChat; +import com.github.polpetta.mezzotre.orm.model.query.QTgChat; +import com.pengrad.telegrambot.model.Message; +import com.pengrad.telegrambot.model.Update; +import java.util.Optional; + +public class Util { + + public static Optional extractChat( + CallbackQueryContext callbackQueryContext, Update update) { + return Optional.of(callbackQueryContext.getFields().getTelegramChatId()) + .map(Double::longValue) + // If we're desperate, search in the message for the chat id + .or( + () -> + Optional.ofNullable(update.callbackQuery().message()) + .map(Message::messageId) + .map(Long::valueOf)) + .filter(chatId -> chatId != 0L && chatId != Long.MIN_VALUE) + .flatMap(chatId -> new QTgChat().id.eq(chatId).findOneOrEmpty()); + } + + public static Optional extractMessageId(Update update) { + return Optional.ofNullable(update.callbackQuery().message()).map(Message::messageId); + } +} diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/command/Help.java b/src/main/java/com/github/polpetta/mezzotre/telegram/command/Help.java index a7a6526..286b43a 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/command/Help.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/command/Help.java @@ -1,13 +1,6 @@ package com.github.polpetta.mezzotre.telegram.command; -import com.github.polpetta.mezzotre.i18n.TemplateContentGenerator; -import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; import com.github.polpetta.mezzotre.orm.model.TgChat; -import com.github.polpetta.mezzotre.telegram.callbackquery.Field; -import com.github.polpetta.mezzotre.util.Clock; -import com.github.polpetta.mezzotre.util.UUIDGenerator; -import com.github.polpetta.types.json.CallbackQueryMetadata; -import com.github.polpetta.types.json.ChatContext; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineKeyboardButton; import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup; @@ -19,38 +12,30 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; -import java.util.stream.Collectors; import javax.inject.Inject; import javax.inject.Named; -import org.apache.commons.lang3.tuple.Pair; public class Help implements Processor { private static final String TRIGGERING_STAGING_NAME = "/help"; - private final TemplateContentGenerator templateContentGenerator; private final Executor threadPool; - private final Clock clock; private final Map tgCommandProcessors; private final Map eventProcessor; - private final UUIDGenerator uuidGenerator; + private final com.github.polpetta.mezzotre.telegram.model.Help modelHelp; @Inject public Help( - TemplateContentGenerator templateContentGenerator, @Named("eventThreadPool") Executor threadPool, - Clock clock, @Named("commandProcessor") Map tgCommandProcessors, @Named("eventProcessors") Map eventProcessor, - UUIDGenerator uuidGenerator) { - this.templateContentGenerator = templateContentGenerator; + com.github.polpetta.mezzotre.telegram.model.Help modelHelp) { this.threadPool = threadPool; - this.clock = clock; this.tgCommandProcessors = tgCommandProcessors; this.eventProcessor = eventProcessor; - this.uuidGenerator = uuidGenerator; + this.modelHelp = modelHelp; } @Override @@ -62,66 +47,17 @@ public class Help implements Processor { public CompletableFuture>> process(TgChat chat, Update update) { return CompletableFuture.supplyAsync( () -> { - final String message = - templateContentGenerator.mergeTemplate( - velocityContext -> { - velocityContext.put( - "commands", - tgCommandProcessors.values().stream() - .distinct() - .map( - p -> - Pair.of( - p.getTriggerKeywords().stream() - .sorted() - .collect(Collectors.toList()), - p.getLocaleDescriptionKeyword())) - .collect(Collectors.toList())); - }, - chat.getLocale(), - "template/telegram/help.vm"); - - final ChatContext chatContext = chat.getChatContext(); - chatContext.setStage(TRIGGERING_STAGING_NAME); - chatContext.setStep(0); - chatContext.setPreviousMessageUnixTimestampInSeconds(clock.now()); - chat.setChatContext(chatContext); - chat.setHasHelpBeenShown(true); - chat.save(); + final String message = modelHelp.getMessage(chat, tgCommandProcessors); final SendMessage sendMessage = new SendMessage(chat.getId(), message).parseMode(ParseMode.Markdown); - final String callBackGroupId = uuidGenerator.generateAsString(); - final InlineKeyboardButton[] collect = - eventProcessor.values().stream() - .filter( - com.github.polpetta.mezzotre.telegram.callbackquery.Processor - ::canBeDirectlyInvokedByTheUser) - .filter(e -> e.getPrettyPrintLocaleKeyName().isPresent()) - .map( - eventProcessor -> { - final CallbackQueryContext callbackQueryContext = - new CallbackQueryContext( - uuidGenerator.generateAsString(), - callBackGroupId, - new CallbackQueryMetadata.CallbackQueryMetadataBuilder() - .withEvent(eventProcessor.getEventName()) - .withTelegramChatId(chat.getId()) - .withAdditionalProperty( - Field.ShowHelp.InvokedFromHelpMessage.getName(), true) - .build()); - callbackQueryContext.save(); + final InlineKeyboardButton[] buttons = + modelHelp.generateInlineKeyBoardButton(chat, eventProcessor); - return new InlineKeyboardButton( - templateContentGenerator.getString( - chat.getLocale(), - eventProcessor.getPrettyPrintLocaleKeyName().get())) - .callbackData(callbackQueryContext.getId()); - }) - .toArray(InlineKeyboardButton[]::new); - - sendMessage.replyMarkup(new InlineKeyboardMarkup(collect)); + if (buttons.length > 0) { + sendMessage.replyMarkup(new InlineKeyboardMarkup(buttons)); + } return Optional.of(sendMessage); }, diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/command/Processor.java b/src/main/java/com/github/polpetta/mezzotre/telegram/command/Processor.java index 393c3e5..ed5f028 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/command/Processor.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/command/Processor.java @@ -43,6 +43,6 @@ public interface Processor { * description */ default String getLocaleDescriptionKeyword() { - return this.getClass().getName().toLowerCase() + ".cmdDescription"; + return this.getClass().getSimpleName().toLowerCase() + ".cmdDescription"; } } diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/model/Help.java b/src/main/java/com/github/polpetta/mezzotre/telegram/model/Help.java index 7dd5e66..401c8e0 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/model/Help.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/model/Help.java @@ -1,18 +1,97 @@ package com.github.polpetta.mezzotre.telegram.model; -import com.github.polpetta.mezzotre.i18n.LocalizedMessageFactory; +import com.github.polpetta.mezzotre.i18n.TemplateContentGenerator; +import com.github.polpetta.mezzotre.orm.model.CallbackQueryContext; +import com.github.polpetta.mezzotre.orm.model.TgChat; +import com.github.polpetta.mezzotre.telegram.callbackquery.Field; +import com.github.polpetta.mezzotre.telegram.command.Processor; +import com.github.polpetta.mezzotre.util.Clock; +import com.github.polpetta.mezzotre.util.UUIDGenerator; +import com.github.polpetta.types.json.CallbackQueryMetadata; +import com.github.polpetta.types.json.ChatContext; +import com.pengrad.telegrambot.model.request.InlineKeyboardButton; +import java.util.Map; +import java.util.stream.Collectors; import javax.inject.Inject; +import org.apache.commons.lang3.tuple.Pair; +// FIXME tests! public class Help { - private final LocalizedMessageFactory localizedMessageFactory; + private static final String TRIGGERING_STAGING_NAME = "/help"; + + private final TemplateContentGenerator templateContentGenerator; + private final Clock clock; + private final UUIDGenerator uuidGenerator; @Inject - public Help(LocalizedMessageFactory localizedMessageFactory) { - this.localizedMessageFactory = localizedMessageFactory; + public Help( + TemplateContentGenerator templateContentGenerator, Clock clock, UUIDGenerator uuidGenerator) { + + this.templateContentGenerator = templateContentGenerator; + this.clock = clock; + this.uuidGenerator = uuidGenerator; } - public String generateErrorMessage() { - return ""; + public String getMessage(TgChat chat, Map tgCommandProcessors) { + final String message = + templateContentGenerator.mergeTemplate( + velocityContext -> { + velocityContext.put( + "commands", + tgCommandProcessors.values().stream() + .distinct() + .map( + p -> + Pair.of( + p.getTriggerKeywords().stream() + .sorted() + .collect(Collectors.toList()), + p.getLocaleDescriptionKeyword())) + .collect(Collectors.toList())); + }, + chat.getLocale(), + "template/telegram/help.vm"); + + // FIXME this shouldn't stay here. We need to move it into another method + final ChatContext chatContext = chat.getChatContext(); + chatContext.setStage(TRIGGERING_STAGING_NAME); + chatContext.setStep(0); + chatContext.setPreviousMessageUnixTimestampInSeconds(clock.now()); + chat.setChatContext(chatContext); + chat.setHasHelpBeenShown(true); + chat.save(); + return message; + } + + public InlineKeyboardButton[] generateInlineKeyBoardButton( + TgChat chat, + Map eventProcessors) { + final String callBackGroupId = uuidGenerator.generateAsString(); + return eventProcessors.values().stream() + .filter( + com.github.polpetta.mezzotre.telegram.callbackquery.Processor + ::canBeDirectlyInvokedByTheUser) + .filter(e -> e.getPrettyPrintLocaleKeyName().isPresent()) + .map( + eventProcessor -> { + final CallbackQueryContext callbackQueryContext = + new CallbackQueryContext( + uuidGenerator.generateAsString(), + callBackGroupId, + new CallbackQueryMetadata.CallbackQueryMetadataBuilder() + .withEvent(eventProcessor.getEventName()) + .withTelegramChatId(chat.getId()) + .withAdditionalProperty( + Field.ShowHelp.InvokedFromHelpMessage.getName(), true) + .build()); + callbackQueryContext.save(); + + return new InlineKeyboardButton( + templateContentGenerator.getString( + chat.getLocale(), eventProcessor.getPrettyPrintLocaleKeyName().get())) + .callbackData(callbackQueryContext.getId()); + }) + .toArray(InlineKeyboardButton[]::new); } } diff --git a/src/main/resources/i18n/message.properties b/src/main/resources/i18n/message.properties index b0cf9e5..d022a29 100644 --- a/src/main/resources/i18n/message.properties +++ b/src/main/resources/i18n/message.properties @@ -1,11 +1,11 @@ start.helloFirstName=Hello {0}! \ud83d\udc4b -start.description=This is {0}, a simple bot focused on DnD content management! Please start by choosing a selectLanguageTutorial down below \ud83d\udc47 +start.description=This is {0}, a simple bot focused on DnD content management! Please start by choosing a language down below \ud83d\udc47 start.cmdDescription=Trigger this very bot start.inlineKeyboardButtonName=Let''s begin! selectLanguageTutorial.inlineKeyboardButtonName=Select language selectLanguageTutorial.drinkAction=*Proceeds to drink a potion with a strange, multicolor liquid* -selectLanguageTutorial.setLanguage=Thanks! Now that I drank this modified potion of {0} that I''ve found at the "Crystal Fermentary" magic potion shop yesterday I can speak with you in the selectLanguageTutorial that you prefer! -selectLanguageTutorial.instructions=You can always change your selectLanguageTutorial settings by typing /selectLanguageTutorial in the chat. +selectLanguageTutorial.setLanguage=Thanks! Now that I drank this modified potion of {0} that I''ve found at the "Crystal Fermentary" magic potion shop yesterday I can speak with you in the language that you prefer! +selectLanguageTutorial.instructions=You can always change your language settings by typing /changeLanguage in the chat. changeLanguage.english=English changeLanguage.italian=Italian changeLanguage.cmdDescription=Select the new language I will use to speak to you diff --git a/src/main/resources/i18n/message_en_US.properties b/src/main/resources/i18n/message_en_US.properties index 296200c..d022a29 100644 --- a/src/main/resources/i18n/message_en_US.properties +++ b/src/main/resources/i18n/message_en_US.properties @@ -1,11 +1,19 @@ start.helloFirstName=Hello {0}! \ud83d\udc4b -start.description=This is {0}, a simple bot focused on DnD content management! Please start by choosing a selectLanguageTutorial down below \ud83d\udc47 +start.description=This is {0}, a simple bot focused on DnD content management! Please start by choosing a language down below \ud83d\udc47 +start.cmdDescription=Trigger this very bot +start.inlineKeyboardButtonName=Let''s begin! +selectLanguageTutorial.inlineKeyboardButtonName=Select language selectLanguageTutorial.drinkAction=*Proceeds to drink a potion with a strange, multicolor liquid* -selectLanguageTutorial.setLanguage=Thanks! Now that I drank this modified potion of {0} that I''ve found at the "Crystal Fermentary" magic potion shop yesterday I can speak with you in the selectLanguageTutorial that you prefer! -selectLanguageTutorial.instructions=You can always change your selectLanguageTutorial settings by typing /selectLanguageTutorial in the chat. -selectLanguageTutorial.english=English -selectLanguageTutorial.italian=Italian +selectLanguageTutorial.setLanguage=Thanks! Now that I drank this modified potion of {0} that I''ve found at the "Crystal Fermentary" magic potion shop yesterday I can speak with you in the language that you prefer! +selectLanguageTutorial.instructions=You can always change your language settings by typing /changeLanguage in the chat. +changeLanguage.english=English +changeLanguage.italian=Italian +changeLanguage.cmdDescription=Select the new language I will use to speak to you +changeLanguage.inlineKeyboardButtonName=Change language spell.speakWithAnimals=Speak with animals selectLanguageTutorial.showMeTutorialInlineKeyboardButtonName=Show me what you can do! help.notShownYet=It seems you haven''t checked out what I can do yet! To have a complete list of my abilities, type /help in chat at any time! help.buttonBelow=Alternatively, you can click the button down below. +help.description=Here is a list of what I can do +help.buttonsToo=You can do the same operations you''d do with the commands aforementioned by selecting the corresponding button below \ud83d\udc47 +help.cmdDescription=Print the help message diff --git a/src/main/resources/i18n/message_it.properties b/src/main/resources/i18n/message_it.properties index dcdfadc..01ae788 100644 --- a/src/main/resources/i18n/message_it.properties +++ b/src/main/resources/i18n/message_it.properties @@ -2,7 +2,7 @@ start.helloFirstName=Ciao {0}! \ud83d\udc4b start.description=Questo è {0}, un semplice bot che ci concenta sulla gestione di contenuto per DnD! Per favore comincia selezionando la lingua qui sotto \ud83d\udc47 selectLanguageTutorial.drinkAction=*Procede a bere una pozione al cui suo interno si trova uno strano liquido multicolore* selectLanguageTutorial.setLanguage=Grazie! Ora che ho bevuto questa posizione modificata di {0} che ho trovato ieri al negozio di pozioni magiche la "Cristalleria Fermentatrice" posso parlare con te nel linguaggio che preferisci! -selectLanguageTutorial.instructions=Puoi sempre cambiare le preferenze della tua lingua scrivendo /selectLanguageTutorial nella chat. +selectLanguageTutorial.instructions=Puoi sempre cambiare le preferenze della tua lingua scrivendo /changeLanguage nella chat. selectLanguageTutorial.english=Inglese selectLanguageTutorial.italian=Italiano spell.speakWithAnimals=Parlare con animali diff --git a/src/main/resources/i18n/message_it_IT.properties b/src/main/resources/i18n/message_it_IT.properties index dcdfadc..01ae788 100644 --- a/src/main/resources/i18n/message_it_IT.properties +++ b/src/main/resources/i18n/message_it_IT.properties @@ -2,7 +2,7 @@ start.helloFirstName=Ciao {0}! \ud83d\udc4b start.description=Questo è {0}, un semplice bot che ci concenta sulla gestione di contenuto per DnD! Per favore comincia selezionando la lingua qui sotto \ud83d\udc47 selectLanguageTutorial.drinkAction=*Procede a bere una pozione al cui suo interno si trova uno strano liquido multicolore* selectLanguageTutorial.setLanguage=Grazie! Ora che ho bevuto questa posizione modificata di {0} che ho trovato ieri al negozio di pozioni magiche la "Cristalleria Fermentatrice" posso parlare con te nel linguaggio che preferisci! -selectLanguageTutorial.instructions=Puoi sempre cambiare le preferenze della tua lingua scrivendo /selectLanguageTutorial nella chat. +selectLanguageTutorial.instructions=Puoi sempre cambiare le preferenze della tua lingua scrivendo /changeLanguage nella chat. selectLanguageTutorial.english=Inglese selectLanguageTutorial.italian=Italiano spell.speakWithAnimals=Parlare con animali diff --git a/src/main/resources/template/telegram/help.vm b/src/main/resources/template/telegram/help.vm index 38a935e..7033d0c 100644 --- a/src/main/resources/template/telegram/help.vm +++ b/src/main/resources/template/telegram/help.vm @@ -1,6 +1,7 @@ ${i18n.help.description}: #foreach(${command} in ${commands}) +## FIXME this is not displayed correctly in telegram! *#foreach(${key} in ${command.left}) ${key}#end: ${i18n.get(${command.right})} #end diff --git a/src/test/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorialIntegrationTest.java b/src/test/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorialIntegrationTest.java index 17758fe..c28e6cc 100644 --- a/src/test/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorialIntegrationTest.java +++ b/src/test/java/com/github/polpetta/mezzotre/telegram/callbackquery/SelectLanguageTutorialIntegrationTest.java @@ -86,7 +86,7 @@ class SelectLanguageTutorialIntegrationTest { + " posso parlare con te nel linguaggio che preferisci!\n" + "\n" + "Puoi sempre cambiare le preferenze della tua lingua scrivendo" - + " /selectLanguageTutorial nella chat.\n" + + " /changeLanguage nella chat.\n" + "\n" + "Sembra tu non abbia ancora visto cosa posso fare! Per avere una lista completa" + " delle mie abilità, scrivi /help nella chat in qualsiasi momento!" @@ -102,7 +102,7 @@ class SelectLanguageTutorialIntegrationTest { + " found at the \"Crystal Fermentary\" magic potion shop yesterday I can speak" + " with you in the language that you prefer!\n" + "\n" - + "You can always change your language settings by typing /selectLanguageTutorial" + + "You can always change your language settings by typing /changeLanguage" + " in the chat.\n" + "\n" + "It seems you haven't checked out what I can do yet! To have a complete list of" @@ -121,7 +121,7 @@ class SelectLanguageTutorialIntegrationTest { + " posso parlare con te nel linguaggio che preferisci!\n" + "\n" + "Puoi sempre cambiare le preferenze della tua lingua scrivendo" - + " /selectLanguageTutorial nella chat.\n\n", + + " /changeLanguage nella chat.\n\n", "en-US", "Mostrami cosa puoi fare!", true), @@ -133,7 +133,7 @@ class SelectLanguageTutorialIntegrationTest { + " found at the \"Crystal Fermentary\" magic potion shop yesterday I can speak" + " with you in the language that you prefer!\n" + "\n" - + "You can always change your language settings by typing /selectLanguageTutorial" + + "You can always change your language settings by typing /changeLanguage" + " in the chat.\n\n", "it-IT", "Show me what you can do!", diff --git a/src/test/java/com/github/polpetta/mezzotre/telegram/command/HelpIntegrationTest.java b/src/test/java/com/github/polpetta/mezzotre/telegram/command/HelpIntegrationTest.java index 29fbc77..3f472aa 100644 --- a/src/test/java/com/github/polpetta/mezzotre/telegram/command/HelpIntegrationTest.java +++ b/src/test/java/com/github/polpetta/mezzotre/telegram/command/HelpIntegrationTest.java @@ -169,15 +169,14 @@ class HelpIntegrationTest { events.put(dummyEvent1.getEventName(), dummyEvent1); events.put(dummyEvent2.getEventName(), dummyEvent2); - help = - new Help( + final com.github.polpetta.mezzotre.telegram.model.Help modelHelp = + new com.github.polpetta.mezzotre.telegram.model.Help( new TemplateContentGenerator(new LocalizedMessageFactory(velocityEngine)), - Executors.newSingleThreadExecutor(), fakeClock, - commands, - events, new UUIDGenerator()); + help = new Help(Executors.newSingleThreadExecutor(), commands, events, modelHelp); + final Update update = gson.fromJson( "{\n" diff --git a/src/test/java/com/github/polpetta/mezzotre/telegram/command/ProcessorTest.java b/src/test/java/com/github/polpetta/mezzotre/telegram/command/ProcessorTest.java new file mode 100644 index 0000000..3e1402a --- /dev/null +++ b/src/test/java/com/github/polpetta/mezzotre/telegram/command/ProcessorTest.java @@ -0,0 +1,38 @@ +package com.github.polpetta.mezzotre.telegram.command; + +import static org.junit.jupiter.api.Assertions.*; + +import com.github.polpetta.mezzotre.orm.model.TgChat; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.BaseRequest; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +@Execution(ExecutionMode.CONCURRENT) +class ProcessorTest { + + @Test + void shouldGetSimpleNameClassIfNotDefined() { + + class Test implements Processor { + + @Override + public Set getTriggerKeywords() { + return null; + } + + @Override + public CompletableFuture>> process(TgChat chat, Update update) { + return null; + } + } + + final Test test = new Test(); + + assertEquals("test.cmdDescription", test.getLocaleDescriptionKeyword()); + } +}