Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
Update to telegram version 4.2 and newest spring boot version
Browse files Browse the repository at this point in the history
  • Loading branch information
xabgesagtx committed May 3, 2019
1 parent 3bcb567 commit c69d9ba
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 52 deletions.
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<version>2.1.4.RELEASE</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -51,6 +51,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<glassfish.version>2.25.1</glassfish.version>
</properties>

<dependencies>
Expand All @@ -62,7 +63,7 @@
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>4.1</version>
<version>4.2</version>
</dependency>

<dependency>
Expand All @@ -81,6 +82,17 @@
<scope>provided</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${glassfish.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.github.xabgesagtx.bots;

import lombok.Getter;
import lombok.Setter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Import;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.BotSession;
Expand All @@ -27,61 +24,40 @@
* Auto configuration for telegram bots to automatically start bots that are configured as beans.
*/
@Configuration
@ConfigurationProperties("telegram")
@Slf4j
@ConditionalOnClass(TelegramBotsApi.class)
@RequiredArgsConstructor
@Import(TelegramProperties.class)
public class TelegramBotAutoConfiguration {

private List<BotSession> sessions = new ArrayList<>();

@Autowired(required=false)
private List<TelegramLongPollingBot> pollingBots = new ArrayList<>();

@Autowired(required=false)
private List<TelegramWebhookBot> webHookBots = new ArrayList<>();

@Getter
@Setter
private String externalUrl = null;
private final List<TelegramLongPollingBot> pollingBots;
private final List<TelegramWebhookBot> webHookBots;
private final TelegramProperties properties;

@Getter
@Setter
private String internalUrl = null;

@Getter
@Setter
private String keyStore = null;

@Getter
@Setter
private String keyStorePassword;

@Getter
@Setter
private String pathToCertificate;

static {
ApiContextInitializer.init();
}

@PostConstruct
public void start() throws TelegramApiRequestException {
log.info("Starting auto config for telegram bots");
TelegramBotsApi api = getApi();
TelegramBotsApi api = telegramBotsApi();
pollingBots.forEach(bot -> {
try {
log.info("Registering polling bot: {}", bot.getBotUsername());
sessions.add(api.registerBot(bot));
} catch (TelegramApiException e) {
log.error("Failed to register bot {} due to error {}", bot.getBotUsername(), e.getMessage());
log.error("Failed to register bot {} due to error", bot.getBotUsername(), e);
}
});
webHookBots.forEach(bot -> {
try {
log.info("Registering web hook bot: {}", bot.getBotUsername());
api.registerBot(bot);
} catch (TelegramApiException e) {
log.error("Failed to register bot {} due to error {}", bot.getBotUsername(), e.getMessage());
log.error("Failed to register bot {} due to error", bot.getBotUsername(), e);
}
});
}
Expand All @@ -90,25 +66,21 @@ public void start() throws TelegramApiRequestException {
* Get API object depending on configured properties.
* If no properties are configured, the API object won't support webhooks.
* @return api object
* @throws TelegramApiRequestException
* @throws TelegramApiRequestException in case the creation of the API object fails
*/
@Bean
@ConditionalOnMissingBean
public TelegramBotsApi getApi() throws TelegramApiRequestException {
public TelegramBotsApi telegramBotsApi() throws TelegramApiRequestException {
TelegramBotsApi result;
if (!StringUtils.isEmpty(externalUrl) && !StringUtils.isEmpty(internalUrl)) {
if (!StringUtils.isEmpty(keyStore) && !StringUtils.isEmpty(keyStorePassword)) {
if (!StringUtils.isEmpty(pathToCertificate)) {
log.info("Initializing API with webhook support and configured keystore and path to certificate");
result = new TelegramBotsApi(keyStore, keyStorePassword, externalUrl, internalUrl, pathToCertificate);
} else {
log.info("Initializing API with webhook support and configured keystore");
result = new TelegramBotsApi(keyStore, keyStorePassword, externalUrl, internalUrl);
}
} else {
log.info("Initializing API with webhook support");
result = new TelegramBotsApi(externalUrl, internalUrl);
}
if (properties.hasKeyStoreWithPath()) {
log.info("Initializing API with webhook support and configured keystore and path to certificate");
result = new TelegramBotsApi(properties.getKeyStore(), properties.getKeyStorePassword(), properties.getExternalUrl(), properties.getInternalUrl(), properties.getPathToCertificate());
} else if (properties.hasKeyStore()) {
log.info("Initializing API with webhook support and configured keystore");
result = new TelegramBotsApi(properties.getKeyStore(), properties.getKeyStorePassword(), properties.getExternalUrl(), properties.getInternalUrl());
} else if (properties.hasUrls()) {
log.info("Initializing API with webhook support");
result = new TelegramBotsApi(properties.getExternalUrl(), properties.getInternalUrl());
} else {
log.info("Initializing API without webhook support");
result = new TelegramBotsApi();
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/github/xabgesagtx/bots/TelegramProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.xabgesagtx.bots;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@ConfigurationProperties("telegram")
@Data
class TelegramProperties {
private String externalUrl;
private String internalUrl;
private String keyStore;
private String keyStorePassword;
private String pathToCertificate;

boolean hasKeyStoreWithPath() {
return hasUrls() && hasKeyStore() && !StringUtils.isEmpty(pathToCertificate);
}

boolean hasKeyStore() {
return !StringUtils.isEmpty(keyStore) && !StringUtils.isEmpty(keyStorePassword);
}

boolean hasUrls() {
return !StringUtils.isEmpty(externalUrl) && !StringUtils.isEmpty(internalUrl);
}

}

0 comments on commit c69d9ba

Please sign in to comment.