bot config via application.properties
This commit is contained in:
parent
d52865e54e
commit
6de379e2fe
@ -8,4 +8,6 @@ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
#-
|
||||
#spring.jpa.hibernate.ddl-auto=create
|
||||
#-
|
||||
de.ph87.kleinanzeigen.telegram.chat.whitelist=101138682,269710244
|
||||
de.ph87.kleinanzeigen.telegram.token=7279963040:AAEtff76gZxzl5m5U9eHpADMKRU6r8Omxcg
|
||||
de.ph87.kleinanzeigen.telegram.username=BotKleinanzeigenBot
|
||||
de.ph87.kleinanzeigen.telegram.whitelist=101138682,269710244
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package de.ph87.kleinanzeigen.telegram;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||
import org.telegram.telegrambots.meta.TelegramBotsApi;
|
||||
@ -17,8 +18,12 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
|
||||
private final Consumer<Update> onUpdate;
|
||||
|
||||
public TelegramBot(final String token, final Consumer<Update> onUpdate) throws IOException, TelegramApiException {
|
||||
@Getter
|
||||
private final String botUsername;
|
||||
|
||||
public TelegramBot(final String token, final String username, final Consumer<Update> onUpdate) throws IOException, TelegramApiException {
|
||||
super(token);
|
||||
this.botUsername = username;
|
||||
this.onUpdate = onUpdate;
|
||||
getOptions().setGetUpdatesTimeout(10);
|
||||
log.info("Starting telegram bot...");
|
||||
@ -27,11 +32,6 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
log.info("Telegram bot registered.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBotUsername() {
|
||||
return "BotKleinanzeigenBot";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdateReceived(final Update update) {
|
||||
onUpdate.accept(update);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package de.ph87.kleinanzeigen.telegram.chat;
|
||||
package de.ph87.kleinanzeigen.telegram;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -11,8 +11,12 @@ import java.util.List;
|
||||
@Slf4j
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "de.ph87.kleinanzeigen.telegram.chat")
|
||||
public class ChatConfig {
|
||||
@ConfigurationProperties(prefix = "de.ph87.kleinanzeigen.telegram")
|
||||
public class TelegramConfig {
|
||||
|
||||
private String token;
|
||||
|
||||
private String username;
|
||||
|
||||
private List<Long> whitelist = new ArrayList<>();
|
||||
|
||||
@ -3,7 +3,7 @@ package de.ph87.kleinanzeigen.telegram;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.ph87.kleinanzeigen.kleinanzeigen.offer.OfferDto;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.ChatDto.ChatDto;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.ChatDto;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.ChatService;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.message.MessageDeleted;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.message.MessageDto;
|
||||
@ -26,9 +26,7 @@ import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -41,13 +39,15 @@ public class TelegramService {
|
||||
|
||||
private static final String ICON_REMOVE = "❌";
|
||||
|
||||
private final TelegramConfig config;
|
||||
|
||||
private final ChatService chatService;
|
||||
|
||||
private byte[] NO_IMAGE = null;
|
||||
private final MessageService messageService;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final MessageService messageService;
|
||||
private byte[] NO_IMAGE = null;
|
||||
|
||||
private TelegramBot bot = null;
|
||||
|
||||
@ -59,7 +59,7 @@ public class TelegramService {
|
||||
NO_IMAGE = stream.toByteArray();
|
||||
|
||||
try {
|
||||
bot = new TelegramBot(readToken(), this::onUpdateReceived);
|
||||
bot = new TelegramBot(config.getToken(), config.getUsername(), this::onUpdateReceived);
|
||||
} catch (TelegramApiException | IOException e) {
|
||||
log.error("Failed to start TelegramBot: {}", e.toString());
|
||||
}
|
||||
@ -90,12 +90,6 @@ public class TelegramService {
|
||||
log.info("Telegram bot stopped");
|
||||
}
|
||||
|
||||
private static String readToken() throws IOException {
|
||||
try (final FileInputStream stream = new FileInputStream("./telegram.token")) {
|
||||
return new String(stream.readAllBytes(), StandardCharsets.UTF_8).trim();
|
||||
}
|
||||
}
|
||||
|
||||
private void onUpdateReceived(final Update update) {
|
||||
try {
|
||||
if (update.hasMessage() && update.getMessage().hasText()) {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package de.ph87.kleinanzeigen.telegram.chat;
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.*;
|
||||
@ -17,7 +18,7 @@ public class Chat {
|
||||
private boolean enabled;
|
||||
|
||||
@Setter
|
||||
@NonNull
|
||||
@Nullable
|
||||
private String name;
|
||||
|
||||
public Chat(final long id, final boolean enabled, @NonNull final String name) {
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package de.ph87.kleinanzeigen.telegram.chat.ChatDto;
|
||||
package de.ph87.kleinanzeigen.telegram.chat;
|
||||
|
||||
import de.ph87.kleinanzeigen.telegram.chat.Chat;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@ -10,9 +9,12 @@ public class ChatDto {
|
||||
|
||||
private final boolean enabled;
|
||||
|
||||
private final String name;
|
||||
|
||||
public ChatDto(final Chat chat) {
|
||||
this.id = chat.getId();
|
||||
this.enabled = chat.isEnabled();
|
||||
this.name = chat.getName();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
package de.ph87.kleinanzeigen.telegram.chat;
|
||||
|
||||
import de.ph87.kleinanzeigen.telegram.AccessDenied;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.ChatDto.ChatDto;
|
||||
import de.ph87.kleinanzeigen.telegram.TelegramConfig;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -17,7 +17,7 @@ public class ChatService {
|
||||
|
||||
private final ChatRepository chatRepository;
|
||||
|
||||
private final ChatConfig config;
|
||||
private final TelegramConfig config;
|
||||
|
||||
public List<ChatDto> findAllEnabled() {
|
||||
return chatRepository.findAllByEnabledTrue().stream().filter(chat -> config.isOnWhitelist(chat.getId())).map(this::toDto).toList();
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package de.ph87.kleinanzeigen.telegram.chat.message;
|
||||
|
||||
import de.ph87.kleinanzeigen.kleinanzeigen.offer.OfferDto;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.ChatDto.ChatDto;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.ChatDto;
|
||||
import jakarta.annotation.Nullable;
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
|
||||
@ -5,7 +5,7 @@ import de.ph87.kleinanzeigen.kleinanzeigen.offer.OfferDto;
|
||||
import de.ph87.kleinanzeigen.kleinanzeigen.offer.OfferRepository;
|
||||
import de.ph87.kleinanzeigen.kleinanzeigen.offer.OfferService;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.Chat;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.ChatDto.ChatDto;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.ChatDto;
|
||||
import de.ph87.kleinanzeigen.telegram.chat.ChatService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user