-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTelegramRequest.java
More file actions
118 lines (100 loc) · 3.57 KB
/
TelegramRequest.java
File metadata and controls
118 lines (100 loc) · 3.57 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
114
115
116
117
118
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.github.stickerifier.stickerify.telegram.Answer;
import com.pengrad.telegrambot.model.Document;
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.jetbrains.annotations.NotNull;
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) {
public static final String NEW_USER = " (new user)";
private static final String START_COMMAND = "/start";
private static final String HELP_COMMAND = "/help";
private static final String PRIVACY_COMMAND = "/privacy";
public TelegramFile getFile() {
return getMessageMedia()
.map(media -> switch (media) {
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.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::size))
.orElse(TelegramFile.TOO_LARGE);
}
public long getChatId() {
return message.chat().id();
}
public Integer getMessageId() {
return message.messageId();
}
/**
* Creates a String describing the current request,
* writing <b>only</b> the user identifier and if the sender is a new user.
*
* @return the description of the request
*/
public String getDescription() {
var description = "request from user " + getUserId();
if (START_COMMAND.equals(message.text())) {
description += NEW_USER;
}
return description;
}
private Long getUserId() {
return message.from().id();
}
public Answer getAnswerMessage() {
return switch (message.text()) {
case HELP_COMMAND, START_COMMAND -> HELP;
case PRIVACY_COMMAND -> PRIVACY_POLICY;
case null, default -> ABOUT;
};
}
@NotNull
@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, String value) {
return value != null && !value.isEmpty()
? ", " + field + "=" + value
: "";
}
}