chat username + timeout=10s

This commit is contained in:
Patrick Haßel 2024-06-17 14:45:31 +02:00
parent 0a8bf5c4fa
commit 0470cfe09e
4 changed files with 14 additions and 10 deletions

View File

@ -20,6 +20,7 @@ public class TelegramBot extends TelegramLongPollingBot {
public TelegramBot(final String token, final Consumer<Update> onUpdate) throws IOException, TelegramApiException {
super(token);
this.onUpdate = onUpdate;
getOptions().setGetUpdatesTimeout(10);
log.info("Starting telegram bot...");
final TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class);
session = (DefaultBotSession) api.registerBot(this);

View File

@ -112,13 +112,13 @@ public class TelegramService {
}
}
private void handleMessage(final Message message) throws AccessDenied {
chatService.setEnabled(message.getChatId(), !message.getText().equals("/stop"));
private void handleMessage(final Message tlgMessage) throws AccessDenied {
chatService.setEnabled(tlgMessage.getChatId(), !tlgMessage.getText().equals("/stop"), tlgMessage.getFrom().getUserName());
}
private void handleCallback(final CallbackQuery callback) throws AccessDenied {
final MaybeInaccessibleMessage tlgMessage = callback.getMessage();
chatService.setEnabled(tlgMessage.getChatId(), true);
chatService.setEnabled(tlgMessage.getChatId(), true, callback.getFrom().getUserName());
try {
final InlineDto dto = objectMapper.readValue(callback.getData(), InlineDto.class);
switch (dto.getCommand()) {

View File

@ -2,10 +2,7 @@ package de.ph87.kleinanzeigen.telegram.chat;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.*;
@Entity
@Getter
@ -19,9 +16,14 @@ public class Chat {
@Setter
private boolean enabled;
public Chat(final long id, final boolean enabled) {
@Setter
@NonNull
private String name;
public Chat(final long id, final boolean enabled, @NonNull final String name) {
this.id = id;
this.enabled = enabled;
this.name = name;
}
}

View File

@ -23,7 +23,7 @@ public class ChatService {
return chatRepository.findAllByEnabledTrue().stream().filter(chat -> config.isOnWhitelist(chat.getId())).map(this::toDto).toList();
}
public void setEnabled(final long id, final boolean enabled) throws AccessDenied {
public void setEnabled(final long id, final boolean enabled, final String name) throws AccessDenied {
if (!config.isOnWhitelist(id)) {
throw new AccessDenied();
}
@ -31,6 +31,7 @@ public class ChatService {
.findById(id)
.stream()
.peek(chat -> {
chat.setName(name);
if (chat.isEnabled() != enabled) {
chat.setEnabled(enabled);
log.info("Chat {}: {}", enabled ? "ENABLED" : "DISABLED", chat);
@ -38,7 +39,7 @@ public class ChatService {
})
.findFirst()
.orElseGet(() -> {
final Chat chat = chatRepository.save(new Chat(id, enabled));
final Chat chat = chatRepository.save(new Chat(id, enabled, name));
log.info("Chat created: {}", chat);
return chat;
});