It's like Spring REST but for Telegram!
This is a starter for a spring boot application with the Telegram Bot API.
soon
soon
Check releases page
The only thing you need to do after adding the dependency is to create a controller for your bot
@SpringBootApplication
@BotController
public class MyBot implements TelegramMvcController {
@Value("${bot.token}")
private String token;
@Override
public String getToken() {
return token;
}
// Indicate what type of request you want to handle
@BotRequest(value = "/click", messageType = {MessageType.CALLBACK_QUERY, MessageType.MESSAGE})
public BaseRequest divide(User user, Chat chat) {
return new SendMessage(chat.id(), "Hello, " + user.firstName() + "!");
}
// Use useful arguments including variables from the request pattern
@MessageRequest(value = "/divide {first:[0-9]} {second:[0-9]}")
public BaseRequest divide(
@BotPathVariable("first") String first,
@BotPathVariable("second") String second
) {
// Return a string if you need to reply with a simple message
return String.valueOf(Integer.parseInt(first) / ((double) Integer.parseInt(second)));
}
public static void main(String[] args) {
SpringApplication.run(MyBot.class);
}
}
The bot will be registered automatically on startup.
Telegram requests are handled by the controllers that implemented TelegramMvcController
interface and are marked by the @BotController
annotation.
It is supposed to use in combination with annotated handler methods based on the BotRequest
annotation.
There are two important parameters here:
value
orpath
: The request mapping templates (e.g./foo
). Ant-style path patterns are supported (e.g./foo *
,/foo param:[0-9]
). If the telegram request matched with several patterns at once, the result pattern will be selected randomly. Useorg.springframework.util.AntPathMatcher
. An empty pattern is matched for any request.- A
String
value of a path variable can be bound to a method argument by the@BotPathVariable
annotation.
- A
type
: the telegram request types to map.MessageType.ANY
by default.
Aliases
If you want to handle only one type of telegram request, it is preferred to use one of the telegram method specific variants @MessageRequest
, @EditedMessageRequest
, @ChannelPostRequest
, @EditedChannelPostRequest
, @InlineQueryRequest
, @CallbackQueryRequest
, @ChosenInlineResultRequest
, @ShippingQueryRequest
, @PreCheckoutQueryRequest
, @PollRequest
.
Some parameters may be nullable because they do not exist for all types of telegram requests
TelegramRequest
- entity that inlude all available parameters from the initial request, the path pattern and path variablesTelegramSession
- current session for the current chat (if any) or user- (Nullable)
String
marked withBotPathVariable
annotation - value of the template variable from the path pattern
com.pengrad.telegrambot.model.
Update
- the initial user request which is currently being processed- (Nullable)
String
- the first non-empty object, if any, amongmessage.text()
,inlineQuery.query()
,chosenInlineResult.query()
,callbackQuery.data()
,shippingQuery.invoicePayload()
,prepreCheckoutQuery.invoicePayload()
- (Nullable)
User
- (Nullable)
Chat
- (Nullable)
Message
- the first non-empty object, if any, amongupdate.message()
,update.editedMessage()
,update.channelPost()
,update.editedChannelPost()
- (Nullable)
InlineQuery
,ChosenInlineResult
,CallbackQuery
,ShippingQuery
,PreCheckoutQuery
,Poll
Supported return values
String
- automatically converted intocom.pengrad.telegrambot.request.SendMessage
. Use only if the chat value is not null for the current telegram requestcom.pengrad.telegrambot.request.BaseRequest
How to support a new one
If you want to add additional arguments or result values types for your controller methods, you should declare a new component:
BotHandlerMethodArgumentResolver
to support an additional type of method argumentBotHandlerMethodReturnValueHandler
to support an additional type of method resultHandlerAdapter
to manually configure all enabled argument resolvers and result value handlers
By default, you can configure only these properties:
property | description |
---|---|
telegram.bot.corePoolSize | Core pool size for default pool executor |
telegram.bot.maxPoolSize | Max pool size for default pool executor |
telegram.bot.sessionSeconds | Cache expiration time for the all beans inside session scope |
If it isn’t enough, you can declare the following components with your implementations:
org.springframework.core.task.TaskExecutor
to configure default TaskExecutor instancejava.util.function.Consumer<okhttp3.OkHttpClient.Builder>
to configure default OkHttpClient instanceTelegramBotProperty
to manage the bot specific settings:@Bean public TelegramBotProperty botProperty2() { // Do not forget about token! // Other fields are optional return TelegramBotProperty .builder() .token(token) .okHttpClient(new OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) .build()) .build(); }
MIT License
Copyright (c) 2019 Kirill Shashov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
- @OlegNyr for his java-telegram-bot-mvc repo