Skip to content

Commit

Permalink
added numbers support for path variables; fixed javadoc warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kshashov committed Sep 22, 2019
1 parent 6bef85f commit 8aa7a23
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 83 deletions.
53 changes: 37 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
# Spring-boot-starter for Telegram
# Spring Boot Starter for Telegram
[![Build Status](https://travis-ci.org/kshashov/spring-boot-starter-telegram.svg?branch=master)](https://travis-ci.org/kshashov/spring-boot-starter-telegram)
[![CircleCI](https://circleci.com/gh/kshashov/spring-boot-starter-telegram.svg?style=svg)](https://circleci.com/gh/kshashov/spring-boot-starter-telegram)
[![codecov](https://codecov.io/gh/kshashov/spring-boot-starter-telegram/branch/master/graph/badge.svg)](https://codecov.io/gh/kshashov/spring-boot-starter-telegram)

It's like Spring REST but for Telegram!

This is a starter for a spring boot application with the [Telegram Bot API](https://github.com/pengrad/java-telegram-bot-api/).
This is a spring boot starter for [Telegram Bot API](https://github.com/pengrad/java-telegram-bot-api/). It's like Spring MVC but for Telegram!

* [Download](#-Download)
* [Maven](#-Maven)
* [Gradle](#-Gradle)
* [Jar](#-Jar)
* [Getting Started](#-Getting-Started)
* [@BotController](#-@BotController)
* [@BotRequest](#-@BotRequest)
* [Request binding](#-Request-binding)
* [Supported arguments](#-Supported-arguments)
* [Supported return values](#-Supported-return-values)
* [How to support a new one](#-How-to-support-a-new-one)
* [Configurations](#-Configurations)
* [License](#-License)
* [Thanks](#-Thanks)

## Download
### Maven
```soon```
```xml
<dependency>
<groupId>com.github.kshashov</groupId>
<artifactId>spring-boot-starter-telegram</artifactId>
<version>0.15</version>
</dependency>
```
### Gradle
```soon```
```groovy
implementation 'com.github.kshashov:spring-boot-starter-telegram:0.15'
```
### Jar
Check [releases page](https://github.com/kshashov/spring-boot-starter-telegram/releases)

## Getting Started
The only thing you need to do after adding the dependency is to create a controller for your bot
The only thing you need to do after adding the dependency is to create a bot controller
```java
@SpringBootApplication
@BotController
Expand All @@ -31,19 +52,19 @@ public class MyBot implements TelegramMvcController {
}

// Indicate what type of request you want to handle
@BotRequest(value = "/click", messageType = {MessageType.CALLBACK_QUERY, MessageType.MESSAGE})
@BotRequest(value = "/click", type = {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
@BotPathVariable("first") Integer first,
@BotPathVariable("second") Integer second
) {
// Return a string if you need to reply with a simple message
return String.valueOf(Integer.parseInt(first) / ((double) Integer.parseInt(second)));
return String.valueOf(first / ((double) second));
}

public static void main(String[] args) {
Expand All @@ -60,7 +81,7 @@ It is supposed to use in combination with annotated handler methods based on the
### Request binding
There are two important parameters here:
* `value` or `path`: 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. Use `org.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.
* Values of the path variables can be bound to the method arguments by the `@BotPathVariable` annotation.
* `type`: the telegram request types to map. `MessageType.ANY` by default.

**Aliases**
Expand All @@ -71,9 +92,9 @@ If you want to handle only one type of telegram request, it is preferred to use
### Supported arguments

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 variables
* `TelegramRequest` - entity that include all available parameters from the initial request, the path pattern and path variables
* `TelegramSession` - current session for the current chat (if any) or user
* (Nullable) `String` marked with `BotPathVariable` annotation - value of the template variable from the path pattern
* (Nullable) `String`/`Integer`/`Long`/`Double`/`Float`/`BigInteger`/`BigDecimal` marked with `BotPathVariable` annotation - value of the template variable from the path pattern

`com.pengrad.telegrambot.model.`
* `Update` - the initial user request which is currently being processed
Expand All @@ -83,11 +104,11 @@ Some parameters may be nullable because they do not exist for all types of teleg
* (Nullable) `Message` - the first non-empty object, if any, among `update.message()`, `update.editedMessage()`, `update.channelPost()`, `update.editedChannelPost()`
* (Nullable) `InlineQuery`, `ChosenInlineResult`, `CallbackQuery`, `ShippingQuery`, `PreCheckoutQuery`, `Poll`

**Supported return values**
### Supported return values
* `String` - automatically converted into `com.pengrad.telegrambot.request.SendMessage`. Use only if the chat value is not null for the current telegram request
* `com.pengrad.telegrambot.request.BaseRequest`

**How to support a new one**
### 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 argument
Expand Down
3 changes: 0 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@
<id>attach-javadocs</id>
</execution>
</executions>
<configuration>
<additionalJOption>-Xdoclint:none</additionalJOption>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public @interface BotPathVariable {

/**
* Name of the template variable that should be bound to a method parameter
* @return Name of the template variable that should be bound to a method parameter.
*/
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,22 @@
* The primary mapping expressed by this annotation.
* <p>This is an alias for {@link #path}. For example
* {@code @BotRequest("/foo")} is equivalent to {@code @BotRequest(path="/foo")}.
* @return Request mapping templates.
*/
@AliasFor("path")
String[] value() default {};

/**
* The request mapping templates (e.g. "/foo"). Ant-style path patterns are also supported (e.g. "/foo *", "/foo
* {param:[0-9]}"). An empty pattern is matched for any request
* @return Request mapping templates (e.g. "/foo"). Ant-style path patterns are also supported (e.g. "/foo *", "/foo
* {param:[0-9]}"). An empty pattern is matched for any request.
*
* @see org.springframework.util.AntPathMatcher
*/
@AliasFor("value")
String[] path() default {};

/**
* The telegram request types to map.
* @return Telegram request types to map.
*/
MessageType[] type() default {MessageType.ANY};
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@

/**
* Alias for {@link BotRequest#value()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] value() default {};

/**
* Alias for {@link BotRequest#path()}.
* @return Request mapping templates.
*/
@AliasFor(annotation = BotRequest.class)
String[] path() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class BotHandlerMethodArgumentResolverComposite implements BotHandlerMeth

/**
* Add the given {@link BotHandlerMethodArgumentResolver}s.
* @param resolvers to add.
* @return current object instance
*/
public BotHandlerMethodArgumentResolverComposite addResolvers(List<? extends BotHandlerMethodArgumentResolver> resolvers) {
if (resolvers != null) {
Expand All @@ -48,6 +50,7 @@ public Object resolveArgument(MethodParameter parameter, TelegramRequest telegra

/**
* Find a registered {@link BotHandlerMethodArgumentResolver} that supports the given method parameter.
* @param parameter for which you need to find the argument resolver
*/
private BotHandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) {
BotHandlerMethodArgumentResolver result = this.argumentResolverCache.get(parameter);
Expand Down
Loading

0 comments on commit 8aa7a23

Please sign in to comment.