From 6b76e79f4dde3e41e0c2baebd87dc75860130648 Mon Sep 17 00:00:00 2001 From: Roman_Meerson Date: Tue, 17 Apr 2018 13:52:08 +0400 Subject: [PATCH] Added spring boot starter module --- pom.xml | 1 + telegrambots-spring-boot-starter/README.md | 63 +++++ telegrambots-spring-boot-starter/pom.xml | 238 ++++++++++++++++++ .../starter/EnableTelegramBots.java | 10 + .../TelegramBotStarterConfiguration.java | 55 ++++ 5 files changed, 367 insertions(+) create mode 100644 telegrambots-spring-boot-starter/README.md create mode 100644 telegrambots-spring-boot-starter/pom.xml create mode 100644 telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/EnableTelegramBots.java create mode 100644 telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java diff --git a/pom.xml b/pom.xml index dc751700..fe5346ee 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ telegrambots-meta telegrambots-extensions telegrambots-abilities + telegrambots-spring-boot-starter diff --git a/telegrambots-spring-boot-starter/README.md b/telegrambots-spring-boot-starter/README.md new file mode 100644 index 00000000..0d67db99 --- /dev/null +++ b/telegrambots-spring-boot-starter/README.md @@ -0,0 +1,63 @@ +
+ abilitybots + +[![Build Status](https://travis-ci.org/rubenlagus/TelegramBots.svg?branch=master)](https://travis-ci.org/rubenlagus/TelegramBots) +[![Jitpack](https://jitpack.io/v/rubenlagus/TelegramBots.svg)](https://jitpack.io/#rubenlagus/TelegramBots) +[![JavaDoc](http://svgur.com/i/1Ex.svg)](https://addo37.github.io/AbilityBots/) +[![Telegram](http://trellobot.doomdns.org/telegrambadge.svg)](https://telegram.me/JavaBotsApi) +[![ghit.me](https://ghit.me/badge.svg?repo=rubenlagus/TelegramBots)](https://ghit.me/repo/rubenlagus/TelegramBots) + +
+ +Usage +----- + +**Maven** + +```xml + + org.telegram + telegrambots-spring-boot-starter + 3.6 + +``` + +**Gradle** + +```gradle + compile "org.telegram:telegrambots-spring-boot-starter:3.6" +``` + +Motivation +---------- +If you are spring boot user it`s better to be in touch with spring starters. This module allows to register bots in spring context automatically and +also use them as standard spring beans. + +How to use +---------- +Your main spring boot class should look like this: + +```java +@SpringBootApplication +//Add this annotation to enable automatic bots initializing +@EnableTelegramBots +public class YourApplicationMainClass { + + public static void main(String[] args) { + //Add this line to initialize bots context + ApiContextInitializer.init(); + + SpringApplication.run(MusicUploaderApplication.class, args); + } +} +``` + +After that your bot will look like: +```java + //Standart Spring component annotation + @Component + public class MusicBotImpl extends TelegramLongPollingBot { + //Bot body. + } +``` +Also you could just implement LongPollingBot or WebHookBot interfaces. All this bots will be registered in context and connected to Telegram api. \ No newline at end of file diff --git a/telegrambots-spring-boot-starter/pom.xml b/telegrambots-spring-boot-starter/pom.xml new file mode 100644 index 00000000..301ee6b1 --- /dev/null +++ b/telegrambots-spring-boot-starter/pom.xml @@ -0,0 +1,238 @@ + + + 4.0.0 + org.telegram + telegrambots-spring-boot-starter + 3.6 + jar + + Telegram Bots Spring Boot Starter + https://github.com/rubenlagus/TelegramBots + Easy to use library to create Telegram Bots + + + https://github.com/rubenlagus/TelegramBots/issues + GitHub Issues + + + + https://github.com/rubenlagus/TelegramBots + scm:git:git://github.com/rubenlagus/TelegramBots.git + scm:git:git@github.com:rubenlagus/TelegramBots.git + + + + https://travis-ci.org/rubenlagus/TelegramBots + Travis + + + + + homich1991@gmail.com + Roman Meerson + https://github.com/homich1991 + homich1991 + + + + + + MIT License + http://www.opensource.org/licenses/mit-license.php + repo + + + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + UTF-8 + UTF-8 + 3.6 + 1.5.10.RELEASE + + + + + + org.telegram + telegrambots + ${bots.version} + + + org.springframework.boot + spring-boot + ${spring-boot.version} + + + org.springframework.boot + spring-boot-autoconfigure + ${spring-boot.version} + + + + + ${project.basedir}/target + ${project.build.directory}/classes + ${project.artifactId}-${project.version} + ${project.build.directory}/test-classes + ${project.basedir}/src/main/java + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.3 + true + + ossrh + https://oss.sonatype.org/ + true + + + + maven-clean-plugin + 3.0.0 + + + clean-project + clean + + clean + + + + + + maven-assembly-plugin + 2.6 + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.0 + + + + jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + + jar + + + -Xdoclint:none + + + + + + org.jacoco + jacoco-maven-plugin + 0.7.7.201606060606 + + + + prepare-agent + + + + report + test + + report + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + 1.4.1 + + + enforce-versions + + enforce + + + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 2.4 + + + copy + package + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + UTF-8 + + + + + + \ No newline at end of file diff --git a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/EnableTelegramBots.java b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/EnableTelegramBots.java new file mode 100644 index 00000000..68c4acf9 --- /dev/null +++ b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/EnableTelegramBots.java @@ -0,0 +1,10 @@ +package org.telegram.telegrambots.starter; + +import org.springframework.context.annotation.Import; + +/** + * Imports configuration #TelegramBotStarterConfiguration in spring context. + */ +@Import(TelegramBotStarterConfiguration.class) +public @interface EnableTelegramBots { +} diff --git a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java new file mode 100644 index 00000000..ec851dc0 --- /dev/null +++ b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java @@ -0,0 +1,55 @@ +package org.telegram.telegrambots.starter; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.telegram.telegrambots.TelegramBotsApi; +import org.telegram.telegrambots.exceptions.TelegramApiException; +import org.telegram.telegrambots.generics.LongPollingBot; +import org.telegram.telegrambots.generics.WebhookBot; + +import java.util.List; + +/** + * Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi. + * #TelegramBotsApi added to spring context as well + */ +@Configuration +public class TelegramBotStarterConfiguration implements CommandLineRunner { + + + private final List longPollingBots; + private final List webHookBots; + + @Autowired + private TelegramBotsApi telegramBotsApi; + + public TelegramBotStarterConfiguration(List longPollingBots, + List webHookBots) { + this.longPollingBots = longPollingBots; + this.webHookBots = webHookBots; + } + + @Override + public void run(String... args) { + try { + for (LongPollingBot bot : longPollingBots) { + telegramBotsApi.registerBot(bot); + } + for (WebhookBot bot : webHookBots) { + telegramBotsApi.registerBot(bot); + } + } catch (TelegramApiException e) { + e.printStackTrace(); + } + } + + + @Bean + @ConditionalOnMissingBean(TelegramBotsApi.class) + public TelegramBotsApi telegramBotsApi() { + return new TelegramBotsApi(); + } +}