#1 temporary store fileId in RAM
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -11,6 +11,7 @@ import com.pengrad.telegrambot.request.AbstractSendRequest;
|
||||
import com.pengrad.telegrambot.request.DeleteMessage;
|
||||
import com.pengrad.telegrambot.request.EditMessageText;
|
||||
import com.pengrad.telegrambot.request.SendDocument;
|
||||
import com.pengrad.telegrambot.response.SendResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.penkrat.stbf.api.BotResponse;
|
||||
@@ -28,7 +29,7 @@ public class BotResponseImpl implements BotResponse {
|
||||
public void send(Screen screen) {
|
||||
log.debug("Send message: \n============\n{}\n============", screen.getText().trim());
|
||||
|
||||
AbstractSendRequest<? extends AbstractSendRequest> sendMessage= SendMethodUtils.createFromScreen(chatId(), screen);
|
||||
AbstractSendRequest<? extends AbstractSendRequest> sendMessage = SendMethodUtils.createFromScreen(chatId(), screen);
|
||||
|
||||
if (screen.getKeyboard() instanceof KeyboardImpl) {
|
||||
KeyboardImpl kk = (KeyboardImpl) screen.getKeyboard();
|
||||
@@ -48,7 +49,8 @@ public class BotResponseImpl implements BotResponse {
|
||||
}
|
||||
}
|
||||
|
||||
telegramBot.execute(sendMessage);
|
||||
final SendResponse sendResponse = telegramBot.execute(sendMessage);
|
||||
SendMethodUtils.processResponse(screen, sendResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
package ru.penkrat.stbf.impl.pengrad;
|
||||
|
||||
import com.pengrad.telegrambot.model.Message;
|
||||
import com.pengrad.telegrambot.model.request.ParseMode;
|
||||
import com.pengrad.telegrambot.request.AbstractSendRequest;
|
||||
import com.pengrad.telegrambot.request.SendAnimation;
|
||||
import com.pengrad.telegrambot.request.SendAudio;
|
||||
import com.pengrad.telegrambot.request.SendMessage;
|
||||
import com.pengrad.telegrambot.request.SendPhoto;
|
||||
import com.pengrad.telegrambot.request.SendVideo;
|
||||
import com.pengrad.telegrambot.request.SendVoice;
|
||||
import com.pengrad.telegrambot.response.SendResponse;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import ru.penkrat.stbf.api.Media;
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@UtilityClass
|
||||
class SendMethodUtils {
|
||||
@@ -19,23 +27,51 @@ class SendMethodUtils {
|
||||
if (isMedia(screen)) {
|
||||
final Media media = screen.getMedia();
|
||||
switch (media.getMediaType()) {
|
||||
case ANIMATION:
|
||||
return Setter.to(createSendAnimation(chatId, media))
|
||||
.setNotNUll(SendAnimation::caption, screen.getText())
|
||||
.setNotNUll(SendAnimation::width, media.getWidth())
|
||||
.setNotNUll(SendAnimation::height, media.getHeight())
|
||||
.setNotNUll(SendAnimation::duration, media.getDuration())
|
||||
.set(SendAnimation::parseMode, screen.getScreenProperties().isParseModeHtml()
|
||||
? ParseMode.HTML
|
||||
: ParseMode.MarkdownV2)
|
||||
.get();
|
||||
case AUDIO:
|
||||
return Setter.to(createSendAudio(chatId, media))
|
||||
.setNotNUll(SendAudio::caption, screen.getText())
|
||||
.setNotNUll(SendAudio::duration, media.getDuration())
|
||||
.set(SendAudio::parseMode, screen.getScreenProperties().isParseModeHtml()
|
||||
? ParseMode.HTML
|
||||
: ParseMode.MarkdownV2)
|
||||
.get();
|
||||
case PHOTO:
|
||||
final SendPhoto sendPhoto = new SendPhoto(chatId, media.getUrl());
|
||||
apply(sendPhoto, sendPhoto::caption, screen.getText());
|
||||
sendPhoto.parseMode(screen.getScreenProperties().isParseModeHtml()
|
||||
? ParseMode.HTML
|
||||
: ParseMode.MarkdownV2);
|
||||
return sendPhoto;
|
||||
return Setter.to(createSendPhoto(chatId, media))
|
||||
.setNotNUll(SendPhoto::caption, screen.getText())
|
||||
.set(SendPhoto::parseMode, screen.getScreenProperties().isParseModeHtml()
|
||||
? ParseMode.HTML
|
||||
: ParseMode.MarkdownV2)
|
||||
.get();
|
||||
case VIDEO:
|
||||
final SendVideo sendVideo = new SendVideo(chatId, media.getUrl());
|
||||
apply(sendVideo, sendVideo::caption, screen.getText());
|
||||
apply(sendVideo, sendVideo::width, media.getWidth());
|
||||
apply(sendVideo, sendVideo::height, media.getHeight());
|
||||
apply(sendVideo, sendVideo::duration, media.getDuration());
|
||||
sendVideo.parseMode(screen.getScreenProperties().isParseModeHtml()
|
||||
? ParseMode.HTML
|
||||
: ParseMode.MarkdownV2);
|
||||
return sendVideo;
|
||||
return Setter.to(createSendVideo(chatId, media))
|
||||
.setNotNUll(SendVideo::caption, screen.getText())
|
||||
.setNotNUll(SendVideo::width, media.getWidth())
|
||||
.setNotNUll(SendVideo::height, media.getHeight())
|
||||
.setNotNUll(SendVideo::duration, media.getDuration())
|
||||
.set(SendVideo::parseMode, screen.getScreenProperties().isParseModeHtml()
|
||||
? ParseMode.HTML
|
||||
: ParseMode.MarkdownV2)
|
||||
.get();
|
||||
case VOICE:
|
||||
return Setter.to(createSendVoice(chatId, media))
|
||||
.setNotNUll(SendVoice::caption, screen.getText())
|
||||
.setNotNUll(SendVoice::duration, media.getDuration())
|
||||
.set(SendVoice::parseMode, screen.getScreenProperties().isParseModeHtml()
|
||||
? ParseMode.HTML
|
||||
: ParseMode.MarkdownV2)
|
||||
.get();
|
||||
default:
|
||||
throw new IllegalStateException("Unsupported media type " + media.getMediaType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,14 +81,106 @@ class SendMethodUtils {
|
||||
.disableNotification(screen.getScreenProperties().isDisableNotification());
|
||||
}
|
||||
|
||||
|
||||
public static void processResponse(Screen screen, SendResponse sendResponse) {
|
||||
if (isMedia(screen)) {
|
||||
final Consumer<String> fileIdConsumer = screen.getMedia().getFileIdConsumer();
|
||||
if (fileIdConsumer != null) {
|
||||
final Message message = sendResponse.message();
|
||||
if (message.animation() != null)
|
||||
fileIdConsumer.accept(message.animation().fileId());
|
||||
else if (message.audio() != null)
|
||||
fileIdConsumer.accept(message.audio().fileId());
|
||||
else if (message.photo() != null && message.photo().length > 0)
|
||||
fileIdConsumer.accept(message.photo()[0].fileId());
|
||||
else if (message.video() != null)
|
||||
fileIdConsumer.accept(message.video().fileId());
|
||||
else if (message.voice() != null)
|
||||
fileIdConsumer.accept(message.voice().fileId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private SendVoice createSendVoice(@NotNull Object chatId, Media media) {
|
||||
if (media.getData() != null) {
|
||||
return new SendVoice(chatId, media.getData().get());
|
||||
}
|
||||
if (media.getFileId() != null) {
|
||||
return new SendVoice(chatId, media.getFileId());
|
||||
}
|
||||
return new SendVoice(chatId, media.getUrl());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private SendVideo createSendVideo(@NotNull Object chatId, Media media) {
|
||||
if (media.getData() != null) {
|
||||
return new SendVideo(chatId, media.getData().get());
|
||||
}
|
||||
if (media.getFileId() != null) {
|
||||
return new SendVideo(chatId, media.getFileId());
|
||||
}
|
||||
return new SendVideo(chatId, media.getUrl());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private SendAudio createSendAudio(@NotNull Object chatId, Media media) {
|
||||
if (media.getData() != null) {
|
||||
return new SendAudio(chatId, media.getData().get());
|
||||
}
|
||||
if (media.getFileId() != null) {
|
||||
return new SendAudio(chatId, media.getFileId());
|
||||
}
|
||||
return new SendAudio(chatId, media.getUrl());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private SendAnimation createSendAnimation(@NotNull Object chatId, Media media) {
|
||||
if (media.getData() != null) {
|
||||
return new SendAnimation(chatId, media.getData().get());
|
||||
}
|
||||
if (media.getFileId() != null) {
|
||||
return new SendAnimation(chatId, media.getFileId());
|
||||
}
|
||||
return new SendAnimation(chatId, media.getUrl());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private SendPhoto createSendPhoto(@NotNull Object chatId, Media media) {
|
||||
if (media.getData() != null) {
|
||||
return new SendPhoto(chatId, media.getData().get());
|
||||
}
|
||||
if (media.getFileId() != null) {
|
||||
return new SendPhoto(chatId, media.getFileId());
|
||||
}
|
||||
return new SendPhoto(chatId, media.getUrl());
|
||||
}
|
||||
|
||||
private boolean isMedia(Screen screen) {
|
||||
return screen.getMedia() != null;
|
||||
}
|
||||
|
||||
private <T, V> T apply(T target, Function<V, T> setter, V value) {
|
||||
if (value != null) {
|
||||
setter.apply(value);
|
||||
|
||||
@RequiredArgsConstructor(staticName = "to")
|
||||
private static class Setter<T> {
|
||||
|
||||
private final T target;
|
||||
|
||||
<V> Setter<T> setNotNUll(BiFunction<T, V, T> setter, V value) {
|
||||
if (value != null) {
|
||||
setter.apply(target, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
return target;
|
||||
|
||||
<V> Setter<T> set(BiFunction<T, V, T> setter, V value) {
|
||||
setter.apply(target, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
T get() {
|
||||
return target;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user