#3 change sessions impl

This commit is contained in:
2022-09-25 09:33:42 +03:00
parent 8453ba42f8
commit a06f9940ff
9 changed files with 279 additions and 189 deletions

View File

@@ -16,46 +16,48 @@ import static lombok.AccessLevel.PROTECTED;
@RequiredArgsConstructor
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.hasMessage()) {
return Optional.ofNullable(update.getMessage().getText());
}
return Optional.empty();
}
@Override
public Optional<String> getMessageText() {
if (update.hasMessage()) {
return Optional.ofNullable(update.getMessage().getText());
}
return Optional.empty();
}
@Override
public Optional<String> getPhoneNumber() {
return Optional.of(update)
.map(Update::getMessage)
.map(Message::getContact)
.map(Contact::getPhoneNumber);
}
@Override
public Optional<String> getPhoneNumber() {
return Optional.of(update)
.map(Update::getMessage)
.map(Message::getContact)
.map(Contact::getPhoneNumber);
}
@Override
public Optional<String> getCallbackData() {
return Optional.of(update)
.map(Update::getCallbackQuery)
.map(CallbackQuery::getData);
}
@Override
public Optional<String> getCallbackData() {
return Optional.of(update)
.map(Update::getCallbackQuery)
.map(CallbackQuery::getData);
}
@Override
public Optional<String> getCallbackMessageText() {
return Optional.of(update)
.map(Update::getCallbackQuery)
.map(CallbackQuery::getMessage)
.map(Message::getText);
}
@Override
public Optional<String> getCallbackMessageText() {
return Optional.of(update)
.map(Update::getCallbackQuery)
.map(CallbackQuery::getMessage)
.map(Message::getText);
}
@Override
public Long getChatId() {
return Utils.getChatId(update);
}
@Override
public Long getChatId() {
return Utils.getChatId(update);
}
@Override
public BotSession getSession() {
throw new UnsupportedOperationException("Session is not supported");
}
}

View File

@@ -7,56 +7,47 @@ import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import ru.penkrat.stbf.api.BotCommandChain;
import ru.penkrat.stbf.api.SessionProvider;
@Slf4j
public class RubenlagusTelegramBot extends TelegramLongPollingBot {
private final String botUsername;
private final String botToken;
private final BotCommandChain commandChain;
private final SessionProvider sessionProvider;
private final String botUsername;
private final String botToken;
private final BotCommandChain commandChain;
public RubenlagusTelegramBot(String botUsername, String botToken, BotCommandChain botCommandChain) {
this(botUsername, botToken, botCommandChain, (id) -> {
throw new IllegalArgumentException("'SessionProvider' is not defined");
});
}
public RubenlagusTelegramBot(String botUsername, String botToken, BotCommandChain botCommandChain) {
this.botUsername = botUsername;
this.botToken = botToken;
this.commandChain = botCommandChain;
public RubenlagusTelegramBot(String botUsername, String botToken, BotCommandChain botCommandChain, SessionProvider sessionProvider) {
this.botUsername = botUsername;
this.botToken = botToken;
this.commandChain = botCommandChain;
this.sessionProvider = sessionProvider;
TelegramBotsApi telegramBotsApi;
try {
telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
telegramBotsApi.registerBot(this);
} catch (TelegramApiException e) {
log.error("Error", e);
}
}
TelegramBotsApi telegramBotsApi;
try {
telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
telegramBotsApi.registerBot(this);
} catch (TelegramApiException e) {
log.error("Error", e);
}
}
@Override
public void onUpdateReceived(Update update) {
try {
commandChain.processCommand(
new BotRequestImpl(update),
new BotResponseImpl(update, this));
} catch (Exception e) {
log.error("Bot Error:", e);
}
}
@Override
public void onUpdateReceived(Update update) {
try {
commandChain.processCommand(
new BotRequestImpl(update, sessionProvider.get(Utils.getChatId(update))),
new BotResponseImpl(update, this));
} catch (Exception e) {
log.error("Bot Error:", e);
}
}
@Override
public String getBotUsername() {
return botUsername;
}
@Override
public String getBotUsername() {
return botUsername;
}
@Override
public String getBotToken() {
return botToken;
}
@Override
public String getBotToken() {
return botToken;
}
}