-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTelegramRequest.java
More file actions
113 lines (95 loc) · 3.66 KB
/
TelegramRequest.java
File metadata and controls
113 lines (95 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.github.stickerifier.stickerify.telegram.model;
import static com.github.stickerifier.stickerify.telegram.Answer.ABOUT;
import static com.github.stickerifier.stickerify.telegram.Answer.HELP;
import static com.github.stickerifier.stickerify.telegram.Answer.PRIVACY_POLICY;
import static java.util.Comparator.comparing;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.stickerifier.stickerify.telegram.Answer;
import com.pengrad.telegrambot.model.Document;
import com.pengrad.telegrambot.model.LivePhoto;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.PhotoSize;
import com.pengrad.telegrambot.model.Sticker;
import com.pengrad.telegrambot.model.Video;
import com.pengrad.telegrambot.model.VideoNote;
import org.jspecify.annotations.Nullable;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
/**
* Data class wrapping a {@link Message} instance to represent a Telegram request.
*
* @param message the message to wrap
*/
public record TelegramRequest(Message message) {
private static final String START_COMMAND = "/start";
private static final String HELP_COMMAND = "/help";
private static final String PRIVACY_COMMAND = "/privacy";
public @Nullable TelegramFile getFile() {
return getMessageMedia()
.map(media -> switch (media) {
case LivePhoto livePhoto -> new TelegramFile(livePhoto.fileId(), livePhoto.fileSize());
case PhotoSize[] photos when photos.length > 0 -> getBestPhoto(photos);
case Document document -> new TelegramFile(document.fileId(), document.fileSize());
case Sticker sticker -> new TelegramFile(sticker.fileId(), sticker.fileSize());
case Video video -> new TelegramFile(video.fileId(), video.fileSize());
case VideoNote videoNote -> new TelegramFile(videoNote.fileId(), videoNote.fileSize());
default -> TelegramFile.NOT_SUPPORTED;
})
.orElse(null);
}
private Optional<?> getMessageMedia() {
return Stream.of(message.livePhoto(), message.photo(), message.document(), message.sticker(),
message.video(), message.videoNote(),
message.audio(), message.voice())
.filter(Objects::nonNull)
.findFirst();
}
private TelegramFile getBestPhoto(PhotoSize[] photos) {
return Arrays.stream(photos)
.map(photo -> new TelegramFile(photo.fileId(), photo.fileSize()))
.filter(TelegramFile::canBeDownloaded)
.max(comparing(TelegramFile::sizeValue))
.orElse(TelegramFile.TOO_LARGE);
}
public long getChatId() {
return message.chat().id();
}
public Integer getMessageId() {
return message.messageId();
}
private Long getUserId() {
return message.from().id();
}
private boolean isNewUser() {
return START_COMMAND.equals(message.text());
}
public Answer getAnswerMessage() {
return switch (message.text()) {
case HELP_COMMAND, START_COMMAND -> HELP;
case PRIVACY_COMMAND -> PRIVACY_POLICY;
case null, default -> ABOUT;
};
}
public RequestDetails toRequestDetails() {
return new RequestDetails(getUserId(), isNewUser());
}
@Override
public String toString() {
var file = Optional.ofNullable(getFile()).map(TelegramFile::id).orElse(null);
var text = Optional.ofNullable(message.text()).orElse(message.caption());
return "request ["
+ "chat=" + getChatId()
+ ", from=" + getUserId()
+ writeIfNotEmpty("file", file)
+ writeIfNotEmpty("text", text)
+ "]";
}
private static String writeIfNotEmpty(String field, @Nullable String value) {
return value != null && !value.isEmpty()
? ", " + field + "=" + value
: "";
}
public record RequestDetails(@JsonProperty("user_id") Long userId, @JsonProperty("new_user") boolean isNewUser) {}
}