This is a spring boot starter for Telegram Bot API. It's like Spring MVC but for Telegram!
<dependency>
<groupId>com.github.kshashov</groupId>
<artifactId>spring-boot-starter-telegram</artifactId>
<version>0.21</version>
</dependency>
implementation 'com.github.kshashov:spring-boot-starter-telegram:0.21'
The only thing you need to do after adding the dependency is to create a bot controller
@BotController
@SpringBootApplication
public class MyBot implements TelegramMvcController {
@Value("${bot.token}")
private String token;
@Override
public String getToken() {
return token;
}
@BotRequest(value = "/hello", type = {MessageType.CALLBACK_QUERY, MessageType.MESSAGE})
public BaseRequest hello(User user, Chat chat) {
return new SendMessage(chat.id(), "Hello, " + user.firstName() + "!");
}
@MessageRequest("/hello {name:[\\S]+}")
public String helloWithName(@BotPathVariable("name") String userName) {
// Return a string if you need to reply with a simple message
return "Hello, " + userName;
}
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.- Values of the path variables can be bound to the method arguments by the
@BotPathVariable
annotation.
- Values of the path variables can be bound to the method arguments by the
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 include all available parameters from the initial request, the path pattern and path variablesTelegramSession
- current session for the current chat (if any) or usercom.pengrad.telegrambot.TelegramBot
- bot instance that received the request- Nullable
String
,Integer
,Long
,Double
,Float
,BigInteger
,BigDecimal
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
com.pengrad.telegrambot.model.User
- Nullable
com.pengrad.telegrambot.model.Chat
- Nullable
com.pengrad.telegrambot.model.Message
- the first non-empty object, if any, amongupdate.message()
,update.editedMessage()
,update.channelPost()
,update.editedChannelPost()
- Nullable
com.pengrad.telegrambot.model.InlineQuery
,com.pengrad.telegrambot.model.ChosenInlineResult
,com.pengrad.telegrambot.model.CallbackQuery
,com.pengrad.telegrambot.model.ShippingQuery
,com.pengrad.telegrambot.model.PreCheckoutQuery
,com.pengrad.telegrambot.model.Poll
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
void
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 resultTelegramBotGlobalPropertiesConfiguration
to manually configure all enabled argument resolvers and result value handlers
By default, you can configure only these properties:
Property | Description | Default value |
---|---|---|
telegram.bot.core-pool-size | Core pool size for default pool executor | 15 |
telegram.bot.max-pool-size | Max pool size for default pool executor | 50 |
telegram.bot.session-seconds | Cache expiration time for the all beans inside session scope | 3600 |
telegram.bot.update-listener-sleep | Timeout between requests to Telegrams API if long polling is enabled (ms) | 300 |
telegram.bot.server-port | HTTP port that will be used to start embedded web server if webhooks is enabled | 8443 |
If it isn’t enough, you can use Java-based confirations:
TelegramBotGlobalPropertiesConfiguration
to configure global and bot specific settings:@Component public class MyBotConfiguration implements TelegramBotGlobalPropertiesConfiguration { ... @Override public void configure(TelegramBotGlobalProperties.Builder builder) { OkHttpClient okHttp = new OkHttpClient.Builder() .connectTimeout(12, TimeUnit.SECONDS) .build(); builder .configureBot(token, botBuilder -> { botBuilder .configure(builder1 -> builder1.okHttpClient(okHttp)); .withWebhook(new SetWebhook().url(url)); }) .configureBot(token2, botBuilder -> { botBuilder .configure(builder1 -> builder1.updateListenerSleep(200L)); }); } }
If you want to use webhooks instead of long polling, you need to provide webhook url:
//.setWebserverPort(8443) Here you can customize the port
.configureBot(token, botBuilder -> {
botBuilder
.withWebhook(new SetWebhook().url(url));
})
In this case the library
- starts local Javalin server on 8443 (by default) port.
- registers
{url}/{random_uuid}
webhook via Telegram API - adds
/{random_uuid}
endpoint to the local server
You can check the following metrics via jmx in the bot.metrics
domain:
Metric | Description |
---|---|
updates |
A number of updates received from Telegram |
processing.errors |
A number of exceptions thrown during updates processing |
no.handlers.errors |
A number of updates for which no suitable handlers were found |
handler.{handler_method_name}.errors |
A number of exceptions thrown during handler method execution |
handler.{handler_method_name}.successes |
A number of successful executions of handler method |
handler.{handler_method_name}.execution.time |
A time spent on successful handler method execution |
MIT License
Copyright (c) 2020 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