chat username + timeout=10s
This commit is contained in:
parent
0a8bf5c4fa
commit
0470cfe09e
@ -20,6 +20,7 @@ public class TelegramBot extends TelegramLongPollingBot {
|
|||||||
public TelegramBot(final String token, final Consumer<Update> onUpdate) throws IOException, TelegramApiException {
|
public TelegramBot(final String token, final Consumer<Update> onUpdate) throws IOException, TelegramApiException {
|
||||||
super(token);
|
super(token);
|
||||||
this.onUpdate = onUpdate;
|
this.onUpdate = onUpdate;
|
||||||
|
getOptions().setGetUpdatesTimeout(10);
|
||||||
log.info("Starting telegram bot...");
|
log.info("Starting telegram bot...");
|
||||||
final TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class);
|
final TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class);
|
||||||
session = (DefaultBotSession) api.registerBot(this);
|
session = (DefaultBotSession) api.registerBot(this);
|
||||||
|
|||||||
@ -112,13 +112,13 @@ public class TelegramService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleMessage(final Message message) throws AccessDenied {
|
private void handleMessage(final Message tlgMessage) throws AccessDenied {
|
||||||
chatService.setEnabled(message.getChatId(), !message.getText().equals("/stop"));
|
chatService.setEnabled(tlgMessage.getChatId(), !tlgMessage.getText().equals("/stop"), tlgMessage.getFrom().getUserName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleCallback(final CallbackQuery callback) throws AccessDenied {
|
private void handleCallback(final CallbackQuery callback) throws AccessDenied {
|
||||||
final MaybeInaccessibleMessage tlgMessage = callback.getMessage();
|
final MaybeInaccessibleMessage tlgMessage = callback.getMessage();
|
||||||
chatService.setEnabled(tlgMessage.getChatId(), true);
|
chatService.setEnabled(tlgMessage.getChatId(), true, callback.getFrom().getUserName());
|
||||||
try {
|
try {
|
||||||
final InlineDto dto = objectMapper.readValue(callback.getData(), InlineDto.class);
|
final InlineDto dto = objectMapper.readValue(callback.getData(), InlineDto.class);
|
||||||
switch (dto.getCommand()) {
|
switch (dto.getCommand()) {
|
||||||
|
|||||||
@ -2,10 +2,7 @@ package de.ph87.kleinanzeigen.telegram.chat;
|
|||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import lombok.Getter;
|
import lombok.*;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.Setter;
|
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Getter
|
@Getter
|
||||||
@ -19,9 +16,14 @@ public class Chat {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean enabled;
|
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.id = id;
|
||||||
this.enabled = enabled;
|
this.enabled = enabled;
|
||||||
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,7 +23,7 @@ public class ChatService {
|
|||||||
return chatRepository.findAllByEnabledTrue().stream().filter(chat -> config.isOnWhitelist(chat.getId())).map(this::toDto).toList();
|
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)) {
|
if (!config.isOnWhitelist(id)) {
|
||||||
throw new AccessDenied();
|
throw new AccessDenied();
|
||||||
}
|
}
|
||||||
@ -31,6 +31,7 @@ public class ChatService {
|
|||||||
.findById(id)
|
.findById(id)
|
||||||
.stream()
|
.stream()
|
||||||
.peek(chat -> {
|
.peek(chat -> {
|
||||||
|
chat.setName(name);
|
||||||
if (chat.isEnabled() != enabled) {
|
if (chat.isEnabled() != enabled) {
|
||||||
chat.setEnabled(enabled);
|
chat.setEnabled(enabled);
|
||||||
log.info("Chat {}: {}", enabled ? "ENABLED" : "DISABLED", chat);
|
log.info("Chat {}: {}", enabled ? "ENABLED" : "DISABLED", chat);
|
||||||
@ -38,7 +39,7 @@ public class ChatService {
|
|||||||
})
|
})
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElseGet(() -> {
|
.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);
|
log.info("Chat created: {}", chat);
|
||||||
return chat;
|
return chat;
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user