Loading repository data…
Loading repository data…
ndanhkhoi / repository
A simple-to-use library to create Telegram Bots in Java and Spring Boot
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
A simple-to-use library to create Telegram Bots in Java and Spring Boot with syntax like Spring MVC
Table of contents generated with markdown-toc
Just import add the library to your project with one of these options:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.ndanhkhoi</groupId>
<artifactId>simple-telegram-command-bot-spring-boot-starter</artifactId>
<version>2023.08.21</version>
</dependency>
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.ndanhkhoi:simple-telegram-command-bot-spring-boot-starter:2023.08.21'
}
Create spring boot application from Spring Initializr
Add Telegram Bot Java Library as dependency
Add bot's properties to your application.yml
khoinda:
bot:
username: { YOUR_BOT_USERNAME }
token: { YOUR_BOT_TOKEN }
bot-route-packages:
- { YOUR_BOT_ROUTES_PACKAGE }
@BotRoute
public class HelloWorldBotRoute {
@CommandDescription("Say hello world")
@CommandMapping(value = "/hi", allowAllUserAccess = true)
public String hi(Update update) {
return "Hello world";
}
}
An annotation indicates that a particular class serves the role of a router.
An annotation is used to map bot requests to route methods.
An annotation is used to describe command. You can see it which default /help command
You can authorize command with these properties in @CommandMapping annotaion:
allowAllUserAccess - boolean, if true all users/group can be call this commandallowAllGroupAccess - a flag to mark a command can be called by any groupsaccessUserIds - an array contains user id can call this commandaccessGroupIds - an array contains group id can call this commandaccessMemberIds - an array contains user id can call this command in the grouponlyAdmin - boolean, if true only admin of group can be call this commandonlyForGroup - boolean, a flag to mark a command can be called in groupsonlyForOwner - boolean, if true only bot's owner can be call this commandUpdate - An Update object of telegram bot APIMessage - A message object of telegram bot APIList<PhotoSize> - If message contains photo, it will be hold them, or else it will be return nullDocument - If message contains file, it will be hold them, or else it will be return null@CommandName - An annotation to mark a param in command method as a command name@CommandBody - An annotation to mark a param in command method as a command body@ChatId - An annotation to mark a param in command method as a chat id, can be used on Long type@SendUserId - An annotation to mark a param in command method as a user id, can be used on Long type@SendUsername - An annotation to mark a param in command method as a username, can be used on String typeString/Number - the text will be reply to user make a requestInputFile/File/byte[]/ByteArrayResource - the file will be reply to user make a requestBotApiMethod - it will be excuted automaticallyVoid - do nothingCollection<T> - with T is one of single value types, it will be do a same job with single value types for each
element in this collectionMono<T> - same as single value but for ReactiveFlux<T> - same as collection value but for Reactive/help - List of available command(s) for this chat/get_log_file - Get an application log file. This command must be called by owner of a bot
in khoinda.bot.bot-owner-chat-id in application.properties or application.ymlIf you want to send log when new update received, you can config your channel id to khoinda.bot.logging-chat-id in
application.properties or application.yml
Here is an example for handler of NoSuchElementException. When bot command request throw NoSuchElementException, it
will reply a text: "404 Not Found !"
@BotRouteAdvice
public class RouteAdvice {
@BotExceptionHandler(NoSuchElementException.class)
public String handleNoSuchElement(Update update, NoSuchElementException ex) {
return "404 Not Found !";
}
}
An annotation indicates that a particular class serves the role of a router for exception.
An annotation is used to mark method is a exception handler.
String - the text will be replied to user make a requestBotApiMethod - it will be excuted automaticallyYou can create a bean that inplements CallbackQuerySubscriber to trigger callback query:
@Component
public class CustomCallbackQuerySubscriber implements CallbackQuerySubscriber {
@Override
public void accept(Update update) throws Exception {
SendMessage sendMessage = new SendMessage();
sendMessage.setText("Callback query data: " + update.getCallbackQuery().getData());
sendMessage.setChatId(update.getCallbackQuery().getMessage().getChatId() + "");
BotDispatcher.getInstance().getSender().execute(sendMessage);
}
}
There are some beans that you can create an inplements to do your stuff
CallbackQuerySubscriber - handle callback query (like button pressed, etc, ...)CommandNotFoundUpdateSubscriber - handle unknown commandsNonCommandUpdateSubscriber - handle an update does not contain commandPreSubscriber - do your job before process commandPosSubscriber - do your job after process commandAfterRegisterBotSubscriber - do your job after register botBy default, you can configure only these properties:
| Property | Description | Default value |
|---|---|---|
| khoinda.bot.enable-auto-config | Enable bot auto configuration | true |
| khoinda.bot.username | Bot's username | |
| khoinda.bot.token | Bot's token | |
| khoinda.bot.logging-chat-id | Chat id can received logging when new Update recieved | |
| khoinda.bot.bot-owner-chat-id | Chat id of bot's owner | new ArrayList<>() |
| khoinda.bot.bot-route-packages | Package(s) name that includes BotRoute class | new ArrayList<>() |
| khoinda.bot.enable-update-trace | Enable /update_trace for owner | false |
| khoinda.bot.disable-default-commands | Disable /help, /start by default | false |
| khoinda.bot.show-command-menu | Show command on bot |