Skip to content

Commit efb7db7

Browse files
authored
Switch replies to rich text (#513)
1 parent f07ad20 commit efb7db7

6 files changed

Lines changed: 198 additions & 80 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test {
6969
useJUnitPlatform()
7070

7171
testLogging {
72-
events('passed', 'failed', 'skipped')
72+
events('started', 'passed', 'failed', 'skipped')
7373
}
7474
}
7575

src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_ALREADY_VALID;
99
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_READY;
1010
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_TOO_LARGE;
11-
import static com.pengrad.telegrambot.model.request.ParseMode.MarkdownV2;
11+
import static com.github.stickerifier.stickerify.telegram.Answer.PROCESSING;
1212
import static java.util.HashSet.newHashSet;
1313
import static java.util.concurrent.Executors.newThreadPerTaskExecutor;
1414

@@ -23,14 +23,15 @@
2323
import com.pengrad.telegrambot.TelegramBot;
2424
import com.pengrad.telegrambot.TelegramException;
2525
import com.pengrad.telegrambot.UpdatesListener;
26-
import com.pengrad.telegrambot.model.LinkPreviewOptions;
2726
import com.pengrad.telegrambot.model.Update;
2827
import com.pengrad.telegrambot.model.request.ReplyParameters;
28+
import com.pengrad.telegrambot.model.request.richmessages.InputRichMessage;
2929
import com.pengrad.telegrambot.request.BaseRequest;
3030
import com.pengrad.telegrambot.request.GetFile;
3131
import com.pengrad.telegrambot.request.GetUpdates;
3232
import com.pengrad.telegrambot.request.SendDocument;
33-
import com.pengrad.telegrambot.request.SendMessage;
33+
import com.pengrad.telegrambot.request.richmessages.SendRichMessage;
34+
import com.pengrad.telegrambot.request.richmessages.SendRichMessageDraft;
3435
import com.pengrad.telegrambot.response.BaseResponse;
3536
import org.slf4j.event.Level;
3637

@@ -54,6 +55,7 @@ public record Stickerify(TelegramBot bot, Executor executor) implements UpdatesL
5455
private static final StructuredLogger LOGGER = new StructuredLogger(Stickerify.class);
5556
private static final String BOT_TOKEN = System.getenv("STICKERIFY_TOKEN");
5657
private static final ThreadFactory VIRTUAL_THREAD_FACTORY = Thread.ofVirtual().name("Virtual-", 0).factory();
58+
private static final InputRichMessage PROCESSING_MESSAGE = new InputRichMessage().html(PROCESSING.getText());
5759

5860
/**
5961
* Instantiate the bot processing requests with virtual threads.
@@ -127,8 +129,11 @@ private void answerFile(TelegramRequest request, TelegramFile file) {
127129

128130
private void answerFile(TelegramRequest request, String fileId) {
129131
Set<Path> pathsToDelete = newHashSet(2);
132+
var processingMessage = new SendRichMessageDraft(request.getChatId(), 1, PROCESSING_MESSAGE);
130133

131134
try {
135+
execute(processingMessage);
136+
132137
var originalFile = retrieveFile(fileId);
133138
pathsToDelete.add(originalFile.toPath());
134139

@@ -141,11 +146,10 @@ private void answerFile(TelegramRequest request, String fileId) {
141146

142147
var answerWithFile = new SendDocument(request.getChatId(), outputFile)
143148
.replyParameters(new ReplyParameters(request.getMessageId()))
144-
.disableContentTypeDetection(true)
145-
.caption(FILE_READY.getText())
146-
.parseMode(MarkdownV2);
149+
.disableContentTypeDetection(true);
147150

148151
execute(answerWithFile);
152+
answerText(FILE_READY, request);
149153
}
150154
} catch (InterruptedException e) {
151155
Thread.currentThread().interrupt();
@@ -209,12 +213,8 @@ private void answerText(TelegramRequest request) {
209213
}
210214

211215
private void answerText(Answer answer, TelegramRequest request) {
212-
var previewOptions = new LinkPreviewOptions().isDisabled(answer.isDisableLinkPreview());
213-
214-
var answerWithText = new SendMessage(request.getChatId(), answer.getText())
215-
.replyParameters(new ReplyParameters(request.getMessageId()))
216-
.parseMode(MarkdownV2)
217-
.linkPreviewOptions(previewOptions);
216+
var message = new InputRichMessage().markdown(answer.getText());
217+
var answerWithText = new SendRichMessage(request.getChatId(), message).disableNotification(true);
218218

219219
try {
220220
execute(answerWithText);

src/main/java/com/github/stickerifier/stickerify/telegram/Answer.java

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,65 @@
66
public enum Answer {
77

88
HELP("""
9-
Send me the media you want to convert and I will take care of the rest\\.
9+
Send me the media you want to convert and I will take care of the rest.
1010
1111
Based on what you send, I will answer the following:
12-
\\- the converted media, if you sent a supported file \\(images, gifs, standard and video stickers are supported\\)
13-
\\- no file, if you sent a media already suiting Telegram's requirements
14-
\\- an error message, if you sent either an unsupported or a corrupted file
15-
\\- an informative message for any message without a file
12+
- the converted media, if you sent a supported file (use the **/supported** command to read the full list)
13+
- no file, if you sent a media already suiting Telegram's requirements
14+
- an error message, if you sent either an unsupported or a corrupted file
15+
- an informative message for any message without a file
1616
17-
Once the file is ready, head to [Stickers](https://t.me/Stickers) to create a new sticker\\.
17+
Once the file is ready, head to [Stickers bot](https://t.me/Stickers) to create a new sticker.
1818
"""),
1919
FILE_READY("""
20-
Your sticker file is ready\\!
21-
Head to [Stickers](https://t.me/Stickers) to create a new sticker\\.
20+
Your sticker file is ready!
21+
22+
Head to [Stickers bot](https://t.me/Stickers) to create a new sticker.
2223
"""),
2324
FILE_ALREADY_VALID("""
24-
The media you sent was already suitable to be a Telegram sticker\\.
25-
Send it to [Stickers](https://t.me/Stickers) to add it as a new sticker\\.
25+
The media you sent was already suitable to be a Telegram sticker.
26+
Send it to [Stickers bot](https://t.me/Stickers) to add it as a new sticker.
2627
"""),
2728
FILE_TOO_LARGE("""
28-
The file can't be converted because Telegram bots can't handle files larger than 20 MB at the moment: please send a smaller one\\.
29+
The file can't be converted because Telegram bots can't handle files larger than 20 MB at the moment: please send a smaller one.
2930
"""),
3031
ABOUT("""
31-
This bot is open source, check it out on [Github](https://github.com/Stickerifier/Stickerify)\\.
32+
This bot is open source, check it out on [GitHub](https://github.com/Stickerifier/Stickerify).
3233
33-
Looking for sticker packs? Try [MeminiCustom](https://t.me/addstickers/MeminiCustom) and [VideoMemini](https://t.me/addstickers/VideoMemini)\\!
34+
Looking for sticker packs? Try [MeminiCustom](https://t.me/addstickers/MeminiCustom) and [VideoMemini](https://t.me/addstickers/VideoMemini)!
3435
"""),
3536
ERROR("""
36-
The file conversion was unsuccessful: only images, gifs, standard and video stickers are supported\\.
37+
The file conversion was unsuccessful: use the **/supported** command to read the full list of supported files.
3738
38-
If you think it should have worked, please report the issue on [Github](https://github.com/Stickerifier/Stickerify/issues/new/choose)\\.
39-
""", true),
39+
If you think it should have worked, please report the issue on [GitHub](https://github.com/Stickerifier/Stickerify/issues/new/choose).
40+
"""),
4041
PRIVACY_POLICY("""
41-
You can view our privacy policy by visiting [this link](https://stickerifier.github.io/Stickerify/PRIVACY_POLICY.html)\\.
42+
You can view our privacy policy by visiting [this link](https://stickerifier.github.io/Stickerify/PRIVACY_POLICY.html).
43+
44+
If you have any questions or concerns, feel free to reach out to us at [stickerifier@gmail.com](mailto:stickerifier@gmail.com).
45+
"""),
46+
PROCESSING("""
47+
<tg-thinking>Processing file...</tg-thinking>
48+
"""),
49+
SUPPORTED_FORMATS("""
50+
| Type | Supported formats |
51+
|:---------|:------------------------------------------------|
52+
| images | png, jpg, static webp, tiff, ico, svg, psd |
53+
| videos | gif, mov, avi, mp4, webm, m4v, mkv, live photos |
54+
| stickers | static, video, animated |
4255
43-
If you have any questions or concerns, feel free to reach out to us\\.
56+
---
57+
58+
If you want to see a format added, please let us know by creating an issue on [GitHub](https://github.com/Stickerifier/Stickerify/issues/new).
4459
""");
4560

4661
private final String text;
47-
private final boolean disableLinkPreview;
4862

4963
Answer(String text) {
50-
this(text, false);
51-
}
52-
53-
Answer(String text, boolean disableLinkPreview) {
5464
this.text = text;
55-
this.disableLinkPreview = disableLinkPreview;
5665
}
5766

5867
public String getText() {
5968
return text;
6069
}
61-
62-
public boolean isDisableLinkPreview() {
63-
return disableLinkPreview;
64-
}
6570
}

src/main/java/com/github/stickerifier/stickerify/telegram/model/TelegramRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.github.stickerifier.stickerify.telegram.Answer.ABOUT;
44
import static com.github.stickerifier.stickerify.telegram.Answer.HELP;
55
import static com.github.stickerifier.stickerify.telegram.Answer.PRIVACY_POLICY;
6+
import static com.github.stickerifier.stickerify.telegram.Answer.SUPPORTED_FORMATS;
67
import static java.util.Comparator.comparing;
78

89
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -31,6 +32,7 @@ public record TelegramRequest(Message message) {
3132
private static final String START_COMMAND = "/start";
3233
private static final String HELP_COMMAND = "/help";
3334
private static final String PRIVACY_COMMAND = "/privacy";
35+
private static final String SUPPORTED_COMMAND = "/supported";
3436

3537
public @Nullable TelegramFile getFile() {
3638
return getMessageMedia()
@@ -82,6 +84,7 @@ public Answer getAnswerMessage() {
8284
return switch (message.text()) {
8385
case HELP_COMMAND, START_COMMAND -> HELP;
8486
case PRIVACY_COMMAND -> PRIVACY_POLICY;
87+
case SUPPORTED_COMMAND -> SUPPORTED_FORMATS;
8588
case null, default -> ABOUT;
8689
};
8790
}

src/test/java/com/github/stickerifier/stickerify/bot/MockResponses.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public final class MockResponses {
99

10-
static final MockResponse EMPTY_UPDATES = new MockResponse.Builder().body("""
10+
static final MockResponse SUCCESS_RESPONSE = new MockResponse.Builder().body("""
1111
{
1212
ok: true
1313
}
@@ -76,6 +76,27 @@ public final class MockResponses {
7676
}
7777
""").build();
7878

79+
static final MockResponse SUPPORTED_MESSAGE = new MockResponse.Builder().body("""
80+
{
81+
ok: true,
82+
result: [
83+
{
84+
update_id: 1,
85+
message: {
86+
message_id: 1,
87+
from: {
88+
id: 123456
89+
},
90+
chat: {
91+
id: 1
92+
},
93+
text: "/supported"
94+
}
95+
}
96+
]
97+
}
98+
""").build();
99+
79100
static final MockResponse FILE_NOT_SUPPORTED = new MockResponse.Builder().body("""
80101
{
81102
ok: true,

0 commit comments

Comments
 (0)