#3 change sessions impl
This commit is contained in:
@@ -16,49 +16,51 @@ import static lombok.AccessLevel.PROTECTED;
|
||||
@RequiredArgsConstructor
|
||||
public class BotRequestImpl implements BotRequest {
|
||||
|
||||
@Getter(PROTECTED)
|
||||
private final Update update;
|
||||
@Getter(PROTECTED)
|
||||
private final Update update;
|
||||
|
||||
@Getter
|
||||
private final BotSession session;
|
||||
@Override
|
||||
public Optional<String> getMessageText() {
|
||||
if (update.message() != null) {
|
||||
return Optional.ofNullable(update.message().text());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getMessageText() {
|
||||
if (update.message() != null) {
|
||||
return Optional.ofNullable(update.message().text());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
@Override
|
||||
public Optional<String> getPhoneNumber() {
|
||||
return Optional.of(update)
|
||||
.map(Update::message)
|
||||
.map(Message::contact)
|
||||
.map(Contact::phoneNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getPhoneNumber() {
|
||||
return Optional.of(update)
|
||||
.map(Update::message)
|
||||
.map(Message::contact)
|
||||
.map(Contact::phoneNumber);
|
||||
}
|
||||
@Override
|
||||
public Optional<String> getCallbackData() {
|
||||
return Optional.of(update)
|
||||
.map(Update::callbackQuery)
|
||||
.map(CallbackQuery::data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getCallbackData() {
|
||||
return Optional.of(update)
|
||||
.map(Update::callbackQuery)
|
||||
.map(CallbackQuery::data);
|
||||
}
|
||||
@Override
|
||||
public Optional<String> getCallbackMessageText() {
|
||||
return Optional.of(update)
|
||||
.map(Update::callbackQuery)
|
||||
.map(CallbackQuery::message)
|
||||
.map(Message::text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getCallbackMessageText() {
|
||||
return Optional.of(update)
|
||||
.map(Update::callbackQuery)
|
||||
.map(CallbackQuery::message)
|
||||
.map(Message::text);
|
||||
}
|
||||
@Override
|
||||
public Long getChatId() {
|
||||
return Optional.of(update)
|
||||
.map(Update::callbackQuery)
|
||||
.map(CallbackQuery::message)
|
||||
.orElseGet(() -> update.message()).chat().id();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getChatId() {
|
||||
return Optional.of(update)
|
||||
.map(Update::callbackQuery)
|
||||
.map(CallbackQuery::message)
|
||||
.orElseGet(() -> update.message()).chat().id();
|
||||
}
|
||||
@Override
|
||||
public BotSession getSession() {
|
||||
throw new UnsupportedOperationException("Session is not supported");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package ru.penkrat.stbf.impl.pengrad;
|
||||
|
||||
import com.google.gson.internal.reflect.ReflectionAccessor;
|
||||
import com.google.gson.internal.reflect.ReflectionHelper;
|
||||
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
|
||||
import com.pengrad.telegrambot.model.request.KeyboardButton;
|
||||
import lombok.Value;
|
||||
@@ -58,7 +58,7 @@ class KeyboardImpl implements Keyboard {
|
||||
return text.computeIfAbsent(btn, o -> {
|
||||
try {
|
||||
Field text = btn.getClass().getDeclaredField("text");
|
||||
ReflectionAccessor.getInstance().makeAccessible(text);
|
||||
ReflectionHelper.makeAccessible(text);
|
||||
return String.valueOf(text.get(btn));
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
return "*error*";
|
||||
|
||||
@@ -6,46 +6,39 @@ import com.pengrad.telegrambot.model.CallbackQuery;
|
||||
import com.pengrad.telegrambot.model.Update;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.penkrat.stbf.api.CommandChain;
|
||||
import ru.penkrat.stbf.api.SessionProvider;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
|
||||
|
||||
public PengradTelegramBot(String botToken, CommandChain commandChain) {
|
||||
this(botToken, commandChain, (id) -> {
|
||||
throw new IllegalArgumentException("'SessionProvider' is not defined");
|
||||
});
|
||||
}
|
||||
public PengradTelegramBot(String botToken, CommandChain commandChain) {
|
||||
super(botToken);
|
||||
this.setUpdatesListener(updates -> {
|
||||
for (Update update : updates) {
|
||||
try {
|
||||
final Long chatId = Optional.of(update)
|
||||
.map(Update::callbackQuery)
|
||||
.map(CallbackQuery::message)
|
||||
.orElseGet(() -> update.message()).chat().id();
|
||||
|
||||
public PengradTelegramBot(String botToken, CommandChain commandChain, SessionProvider sessionProvider) {
|
||||
super(botToken);
|
||||
this.setUpdatesListener(updates -> {
|
||||
for (Update update : updates) {
|
||||
try {
|
||||
final Long chatId = Optional.of(update)
|
||||
.map(Update::callbackQuery)
|
||||
.map(CallbackQuery::message)
|
||||
.orElseGet(() -> update.message()).chat().id();
|
||||
log.debug("New message in chat {}", chatId);
|
||||
|
||||
log.debug("New message in chat {}", chatId);
|
||||
commandChain.processCommand(
|
||||
new BotRequestImpl(update),
|
||||
new BotResponseImpl(update, this));
|
||||
} catch (Exception e) {
|
||||
log.error("Bot Error:", e);
|
||||
}
|
||||
}
|
||||
return UpdatesListener.CONFIRMED_UPDATES_ALL;
|
||||
});
|
||||
}
|
||||
|
||||
commandChain.processCommand(
|
||||
new BotRequestImpl(update, sessionProvider.get(chatId)),
|
||||
new BotResponseImpl(update, this));
|
||||
} catch (Exception e) {
|
||||
log.error("Bot Error:", e);
|
||||
}
|
||||
}
|
||||
return UpdatesListener.CONFIRMED_UPDATES_ALL;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
removeGetUpdatesListener();
|
||||
log.debug("Bot closed.");
|
||||
}
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
removeGetUpdatesListener();
|
||||
log.debug("Bot closed.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user