#1 initial sending media files support
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:
@@ -7,10 +7,10 @@ import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup;
|
||||
import com.pengrad.telegrambot.model.request.KeyboardButton;
|
||||
import com.pengrad.telegrambot.model.request.ParseMode;
|
||||
import com.pengrad.telegrambot.model.request.ReplyKeyboardMarkup;
|
||||
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.request.SendMessage;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.penkrat.stbf.api.BotResponse;
|
||||
@@ -28,10 +28,7 @@ public class BotResponseImpl implements BotResponse {
|
||||
public void send(Screen screen) {
|
||||
log.debug("Send message: \n============\n{}\n============", screen.getText().trim());
|
||||
|
||||
SendMessage sendMessage = new SendMessage(chatId(), screen.getText().trim())
|
||||
.parseMode(screen.getScreenProperties().isParseModeHtml() ? ParseMode.HTML : ParseMode.MarkdownV2)
|
||||
.disableWebPagePreview(screen.getScreenProperties().isDisableWebPagePreview())
|
||||
.disableNotification(screen.getScreenProperties().isDisableNotification());
|
||||
AbstractSendRequest<? extends AbstractSendRequest> sendMessage= SendMethodUtils.createFromScreen(chatId(), screen);
|
||||
|
||||
if (screen.getKeyboard() instanceof KeyboardImpl) {
|
||||
KeyboardImpl kk = (KeyboardImpl) screen.getKeyboard();
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package ru.penkrat.stbf.impl.pengrad;
|
||||
|
||||
import com.pengrad.telegrambot.model.request.ParseMode;
|
||||
import com.pengrad.telegrambot.request.AbstractSendRequest;
|
||||
import com.pengrad.telegrambot.request.SendMessage;
|
||||
import com.pengrad.telegrambot.request.SendPhoto;
|
||||
import com.pengrad.telegrambot.request.SendVideo;
|
||||
import lombok.NonNull;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import ru.penkrat.stbf.api.Media;
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@UtilityClass
|
||||
class SendMethodUtils {
|
||||
|
||||
public AbstractSendRequest<? extends AbstractSendRequest> createFromScreen(@NonNull Object chatId, @NonNull Screen screen) {
|
||||
if (isMedia(screen)) {
|
||||
final Media media = screen.getMedia();
|
||||
switch (media.getMediaType()) {
|
||||
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;
|
||||
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 new SendMessage(chatId, screen.getText().trim())
|
||||
.parseMode(screen.getScreenProperties().isParseModeHtml() ? ParseMode.HTML : ParseMode.MarkdownV2)
|
||||
.disableWebPagePreview(screen.getScreenProperties().isDisableWebPagePreview())
|
||||
.disableNotification(screen.getScreenProperties().isDisableNotification());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user