-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated project version and added to RELEASE_NOTES (#17)
* updated project version and added to RELEASE_NOTES * updated project version and added to RELEASE_NOTES
- Loading branch information
Showing
23 changed files
with
489 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
JRTB-2: added stub telegram bot | ||
JRTB-0: added SpringBoot skeleton project | ||
|
||
## 0.2.0 | ||
* impelements command pattern for handling Telegram bot command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
/** | ||
* Command interface for handling telegram-bot commands. | ||
*/ | ||
public interface Command { | ||
void execute(Update update); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageServiceImpl; | ||
|
||
import java.util.Map; | ||
|
||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.*; | ||
|
||
public class CommandContainer { | ||
private final Map<String, Command> commandMap; | ||
private final Command unknownCommand; | ||
|
||
public CommandContainer(SendBotMessageService sendBotMessageService) { | ||
commandMap = Map.ofEntries(Map.entry(START.getCommandName(), new StartCommand(sendBotMessageService)), | ||
Map.entry(STOP.getCommandName(), new StopCommand(sendBotMessageService)), | ||
Map.entry(HELP.getCommandName(), new HelpCommand(sendBotMessageService)), | ||
Map.entry(NO.getCommandName(), new NoCommand(sendBotMessageService))); | ||
|
||
unknownCommand = new UnknownCommand(sendBotMessageService); | ||
} | ||
|
||
public Command retrieveCommand(String commandIdentifier) { | ||
return commandMap.getOrDefault(commandIdentifier, unknownCommand); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
public enum CommandName { | ||
|
||
START("/start"), | ||
STOP("/stop"), | ||
HELP("/help"), | ||
NO("nocommand"); | ||
|
||
private final String commandName; | ||
|
||
CommandName(String commandName) { | ||
this.commandName = commandName; | ||
} | ||
|
||
public String getCommandName() { | ||
return commandName; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/HelpCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.*; | ||
|
||
|
||
public class HelpCommand implements Command { | ||
|
||
private final SendBotMessageService sendBotMessageService; | ||
|
||
public static final String HELP_MESSAGE = String.format("✨<b>Дотупные команды</b>✨\n\n" | ||
|
||
+ "<b>Начать\\закончить работу с ботом</b>\n" | ||
+ "%s - начать работу со мной\n" | ||
+ "%s - приостановить работу со мной\n\n" | ||
+ "%s - получить помощь в работе со мной\n", | ||
START.getCommandName(), STOP.getCommandName(), HELP.getCommandName()); | ||
|
||
public HelpCommand(SendBotMessageService sendBotMessageService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
} | ||
|
||
@Override | ||
public void execute(Update update) { | ||
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), HELP_MESSAGE); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/NoCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
public class NoCommand implements Command { | ||
|
||
private final SendBotMessageService sendBotMessageService; | ||
|
||
public static final String NO_MESSAGE = "Я поддерживаю команды, начинающиеся со слеша(/).\n" | ||
+ "Чтобы посмотреть список команд введите /help"; | ||
|
||
public NoCommand(SendBotMessageService sendBotMessageService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
} | ||
|
||
@Override | ||
public void execute(Update update) { | ||
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), NO_MESSAGE); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/StartCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
public class StartCommand implements Command { | ||
|
||
private final SendBotMessageService sendBotMessageService; | ||
|
||
public final static String START_MESSAGE = "Привет. Я JBolivar Telegram Bot. Я помогу тебе быть в курсе последних " + | ||
"статей тех авторов, котрые тебе интересны. Я еще маленький и только учусь."; | ||
|
||
// Здесь не добавляем сервис через получение из Application Context. | ||
// Потому что если это сделать так, то будет циклическая зависимость, которая | ||
// ломает работу приложения. | ||
public StartCommand(SendBotMessageService sendBotMessageService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
} | ||
|
||
@Override | ||
public void execute(Update update) { | ||
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), START_MESSAGE); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/StopCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
public class StopCommand implements Command { | ||
|
||
private final SendBotMessageService sendBotMessageService; | ||
|
||
public static final String STOP_MESSAGE = "Деактивировал все ваши подписки \uD83D\uDE1F."; | ||
|
||
public StopCommand(SendBotMessageService sendBotMessageService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
} | ||
|
||
@Override | ||
public void execute(Update update) { | ||
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), STOP_MESSAGE); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/UnknownCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
public class UnknownCommand implements Command{ | ||
public static final String UNKNOWN_MESSAGE = "Не понимаю вас \uD83D\uDE1F, напишите /help чтобы узнать что я понимаю."; | ||
|
||
private final SendBotMessageService sendBotMessageService; | ||
|
||
public UnknownCommand(SendBotMessageService sendBotMessageService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
} | ||
|
||
@Override | ||
public void execute(Update update) { | ||
sendBotMessageService.sendMessage(update.getMessage().getChatId().toString(), UNKNOWN_MESSAGE); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ain/java/com/github/JBolivarLi/javarushtelegrambot/bot/service/SendBotMessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.service; | ||
|
||
public interface SendBotMessageService { | ||
/** | ||
* Send message via telegram bot. | ||
* | ||
* @param chatId provided chatId in which messages would be sent. | ||
* @param message provided message to be sent. | ||
*/ | ||
void sendMessage(String chatId, String message); | ||
} |
32 changes: 32 additions & 0 deletions
32
...java/com/github/JBolivarLi/javarushtelegrambot/bot/service/SendBotMessageServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.service; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.bot.JavarushTelegramBot; | ||
import org.jvnet.hk2.annotations.Service; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
|
||
@Service | ||
public class SendBotMessageServiceImpl implements SendBotMessageService{ | ||
private final JavarushTelegramBot javarushBot; | ||
|
||
@Autowired | ||
public SendBotMessageServiceImpl(JavarushTelegramBot javarushBot) { | ||
this.javarushBot = javarushBot; | ||
} | ||
|
||
@Override | ||
public void sendMessage(String chatId, String message) { | ||
SendMessage sendMessage = new SendMessage(); | ||
sendMessage.setChatId(chatId); | ||
sendMessage.enableHtml(true); | ||
sendMessage.setText(message); | ||
|
||
try { | ||
javarushBot.execute(sendMessage); | ||
} catch (TelegramApiException e) { | ||
//todo add logging to the project. | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.bot.JavarushTelegramBot; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.command.Command; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageServiceImpl; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; | ||
import org.telegram.telegrambots.meta.api.objects.Message; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
|
||
/** | ||
* Abstract class for testing {@link Command}s. | ||
*/ | ||
abstract class AbstractCommandTest { | ||
|
||
protected JavarushTelegramBot javarushBot = Mockito.mock(JavarushTelegramBot.class); | ||
protected SendBotMessageService sendBotMessageService = new SendBotMessageServiceImpl(javarushBot); | ||
|
||
abstract String getCommandName(); | ||
|
||
abstract String getCommandMessage(); | ||
|
||
abstract Command getCommand(); | ||
|
||
@Test | ||
public void shouldProperlyExecuteCommand() throws TelegramApiException { | ||
//given | ||
Long chatId = 1234567824356L; | ||
|
||
Update update = new Update(); | ||
Message message = Mockito.mock(Message.class); | ||
Mockito.when(message.getChatId()).thenReturn(chatId); | ||
Mockito.when(message.getText()).thenReturn(getCommandName()); | ||
update.setMessage(message); | ||
|
||
SendMessage sendMessage = new SendMessage(); | ||
sendMessage.setChatId(chatId.toString()); | ||
sendMessage.setText(getCommandMessage()); | ||
sendMessage.enableHtml(true); | ||
|
||
//when | ||
getCommand().execute(update); | ||
|
||
//then | ||
Mockito.verify(javarushBot).execute(sendMessage); | ||
} | ||
} |
Oops, something went wrong.