#3 session API draft

This commit is contained in:
2021-09-23 14:56:07 +03:00
parent 05af2f05a4
commit c62de38f17
13 changed files with 258 additions and 138 deletions

View File

@@ -1,58 +1,61 @@
package ru.penkrat.stbf.impl.rubenlagus;
import static lombok.AccessLevel.PROTECTED;
import java.util.Optional;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Contact;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import ru.penkrat.stbf.api.BotRequest;
import ru.penkrat.stbf.api.BotSession;
import java.util.Optional;
import static lombok.AccessLevel.PROTECTED;
@RequiredArgsConstructor
class BotRequestImpl implements BotRequest {
@Getter(PROTECTED)
private final Update update;
@Getter(PROTECTED)
private final Update update;
@Override
public Optional<String> getMessageText() {
if (update.hasMessage()) {
return Optional.ofNullable(update.getMessage().getText());
}
return Optional.empty();
}
@Getter
private final BotSession session;
@Override
public Optional<String> getPhoneNumber() {
return Optional.of(update)
.map(Update::getMessage)
.map(Message::getContact)
.map(Contact::getPhoneNumber);
}
@Override
public Optional<String> getMessageText() {
if (update.hasMessage()) {
return Optional.ofNullable(update.getMessage().getText());
}
return Optional.empty();
}
@Override
public Optional<String> getCallbackData() {
return Optional.of(update)
.map(Update::getCallbackQuery)
.map(CallbackQuery::getData);
}
@Override
public Optional<String> getPhoneNumber() {
return Optional.of(update)
.map(Update::getMessage)
.map(Message::getContact)
.map(Contact::getPhoneNumber);
}
@Override
public Optional<String> getCallbackMessageText() {
return Optional.of(update)
.map(Update::getCallbackQuery)
.map(CallbackQuery::getMessage)
.map(Message::getText);
}
@Override
public Optional<String> getCallbackData() {
return Optional.of(update)
.map(Update::getCallbackQuery)
.map(CallbackQuery::getData);
}
@Override
public Long getChatId() {
return update.getMessage().getChatId();
}
@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);
}
}

View File

@@ -1,54 +1,62 @@
package ru.penkrat.stbf.impl.rubenlagus;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
import lombok.extern.slf4j.Slf4j;
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 String botUsername;
private final String botToken;
private final BotCommandChain commandChain;
private final SessionProvider sessionProvider;
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) {
this(botUsername, botToken, botCommandChain, (id) -> {
throw new IllegalArgumentException("'SessionProvider' is not defined");
});
}
TelegramBotsApi telegramBotsApi;
try {
telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
telegramBotsApi.registerBot(this);
} catch (TelegramApiException e) {
log.error("Error", e);
}
}
public RubenlagusTelegramBot(String botUsername, String botToken, BotCommandChain botCommandChain, SessionProvider sessionProvider) {
this.botUsername = botUsername;
this.botToken = botToken;
this.commandChain = botCommandChain;
this.sessionProvider = sessionProvider;
@Override
public void onUpdateReceived(Update update) {
try {
commandChain.processCommand(
new BotRequestImpl(update),
new BotResponseImpl(update, this));
} catch (Exception e) {
log.error("Bot Error:", e);
}
}
TelegramBotsApi telegramBotsApi;
try {
telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
telegramBotsApi.registerBot(this);
} catch (TelegramApiException e) {
log.error("Error", e);
}
}
@Override
public String getBotUsername() {
return botUsername;
}
@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 getBotToken() {
return botToken;
}
@Override
public String getBotUsername() {
return botUsername;
}
@Override
public String getBotToken() {
return botToken;
}
}

View File

@@ -0,0 +1,21 @@
package ru.penkrat.stbf.impl.rubenlagus;
import lombok.experimental.UtilityClass;
import org.telegram.telegrambots.meta.api.objects.Update;
@UtilityClass
class Utils {
Long getChatId(Update update) {
if (update.getMessage() != null) {
return update.getMessage().getChatId();
}
if (update.getCallbackQuery() != null) {
update.getCallbackQuery().getMessage().getChatId();
}
throw new IllegalStateException("Can't extract chatId from 'Update'");
}
}