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

Commit

Permalink
Update to spring boot 2.6.2 and telegram bots 5.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
xabgesagtx committed Jan 3, 2022
1 parent 6d5580a commit debea73
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 51 deletions.
4 changes: 2 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.5.7</version>
<version>2.6.2</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -52,7 +52,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>
<glassfish.version>2.32</glassfish.version>
<telegrambots.version>5.4.0.1</telegrambots.version>
<telegrambots.version>5.6.0</telegrambots.version>
<maven-checkstyle-plugin.version>3.1.1</maven-checkstyle-plugin.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook.SetWebhookBuilder;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;

/**
* Auto configuration for telegram bots to automatically start bots that are configured as beans.
Expand All @@ -26,40 +17,10 @@
@Slf4j
@ConditionalOnClass(TelegramBotsApi.class)
@RequiredArgsConstructor
@Import({TelegramProperties.class, TelegramApiFactory.class, SetWebhookBuilderFactory.class})
@Import({TelegramProperties.class, TelegramApiFactory.class, SetWebhookBuilderFactory.class, TelegramBotStarter.class})
public class TelegramBotAutoConfiguration {

private final List<BotSession> sessions = new ArrayList<>();
private final List<LongPollingBot> pollingBots;
private final List<TelegramWebhookBot> webHookBots;
private final TelegramApiFactory apiFactory;
private final SetWebhookBuilderFactory setWebhookFactory;

@PostConstruct
public void start() throws TelegramApiException {
log.info("Starting auto config for telegram bots");
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);
}
});
webHookBots.forEach(bot -> {
try {
log.info("Registering web hook bot: {}", bot.getBotUsername());
SetWebhookBuilder webhookBuilder = setWebhookFactory.create();
if (bot instanceof CustomizableTelegramWebhookBot) {
((CustomizableTelegramWebhookBot) bot).customizeWebHook(webhookBuilder);
}
api.registerBot(bot, webhookBuilder.build());
} catch (TelegramApiException e) {
log.error("Failed to register bot {} due to error", bot.getBotUsername(), e);
}
});
}

/**
* Get API object depending on configured properties.
Expand All @@ -74,13 +35,4 @@ public TelegramBotsApi telegramBotsApi() throws TelegramApiException {
return apiFactory.create();
}

@PreDestroy
public void stop() {
sessions.forEach(session -> {
if (session != null) {
session.stop();
}
});
}

}
63 changes: 63 additions & 0 deletions src/main/java/com/github/xabgesagtx/bots/TelegramBotStarter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.xabgesagtx.bots;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class TelegramBotStarter {

private final List<BotSession> sessions = new ArrayList<>();
private final List<LongPollingBot> pollingBots;
private final List<TelegramWebhookBot> webHookBots;
private final TelegramBotsApi api;
private final SetWebhookBuilderFactory setWebhookFactory;

@PostConstruct
public void start() {
log.info("Starting auto config for telegram bots");
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);
}
});
webHookBots.forEach(bot -> {
try {
log.info("Registering web hook bot: {}", bot.getBotUsername());
SetWebhook.SetWebhookBuilder webhookBuilder = setWebhookFactory.create();
if (bot instanceof CustomizableTelegramWebhookBot) {
((CustomizableTelegramWebhookBot) bot).customizeWebHook(webhookBuilder);
}
api.registerBot(bot, webhookBuilder.build());
} catch (TelegramApiException e) {
log.error("Failed to register bot {} due to error", bot.getBotUsername(), e);
}
});
}

@PreDestroy
public void stop() {
sessions.forEach(session -> {
if (session != null) {
session.stop();
}
});
}

}

0 comments on commit debea73

Please sign in to comment.