Skip to content
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test {
useJUnitPlatform()

testLogging {
events('passed', 'failed', 'skipped')
events('started', 'passed', 'failed', 'skipped')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_ALREADY_VALID;
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_READY;
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_TOO_LARGE;
import static com.pengrad.telegrambot.model.request.ParseMode.MarkdownV2;
import static com.github.stickerifier.stickerify.telegram.Answer.PROCESSING;
import static java.util.HashSet.newHashSet;
import static java.util.concurrent.Executors.newThreadPerTaskExecutor;

Expand All @@ -23,14 +23,15 @@
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.TelegramException;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.LinkPreviewOptions;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.ReplyParameters;
import com.pengrad.telegrambot.model.request.richmessages.InputRichMessage;
import com.pengrad.telegrambot.request.BaseRequest;
import com.pengrad.telegrambot.request.GetFile;
import com.pengrad.telegrambot.request.GetUpdates;
import com.pengrad.telegrambot.request.SendDocument;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.richmessages.SendRichMessage;
import com.pengrad.telegrambot.request.richmessages.SendRichMessageDraft;
import com.pengrad.telegrambot.response.BaseResponse;
import org.slf4j.event.Level;

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

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

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

try {
execute(processingMessage);

var originalFile = retrieveFile(fileId);
pathsToDelete.add(originalFile.toPath());

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

var answerWithFile = new SendDocument(request.getChatId(), outputFile)
.replyParameters(new ReplyParameters(request.getMessageId()))
.disableContentTypeDetection(true)
.caption(FILE_READY.getText())
.parseMode(MarkdownV2);
.disableContentTypeDetection(true);

execute(answerWithFile);
answerText(FILE_READY, request);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand Down Expand Up @@ -209,12 +213,8 @@ private void answerText(TelegramRequest request) {
}

private void answerText(Answer answer, TelegramRequest request) {
var previewOptions = new LinkPreviewOptions().isDisabled(answer.isDisableLinkPreview());

var answerWithText = new SendMessage(request.getChatId(), answer.getText())
.replyParameters(new ReplyParameters(request.getMessageId()))
.parseMode(MarkdownV2)
.linkPreviewOptions(previewOptions);
var message = new InputRichMessage().markdown(answer.getText());
var answerWithText = new SendRichMessage(request.getChatId(), message).disableNotification(true);

try {
execute(answerWithText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,65 @@
public enum Answer {

HELP("""
Send me the media you want to convert and I will take care of the rest\\.
Send me the media you want to convert and I will take care of the rest.

Based on what you send, I will answer the following:
\\- the converted media, if you sent a supported file \\(images, gifs, standard and video stickers are supported\\)
\\- no file, if you sent a media already suiting Telegram's requirements
\\- an error message, if you sent either an unsupported or a corrupted file
\\- an informative message for any message without a file
- the converted media, if you sent a supported file (use the **/supported** command to read the full list)
- no file, if you sent a media already suiting Telegram's requirements
- an error message, if you sent either an unsupported or a corrupted file
- an informative message for any message without a file

Once the file is ready, head to [Stickers](https://t.me/Stickers) to create a new sticker\\.
Once the file is ready, head to [Stickers bot](https://t.me/Stickers) to create a new sticker.
"""),
FILE_READY("""
Your sticker file is ready\\!
Head to [Stickers](https://t.me/Stickers) to create a new sticker\\.
Your sticker file is ready!

Head to [Stickers bot](https://t.me/Stickers) to create a new sticker.
"""),
FILE_ALREADY_VALID("""
The media you sent was already suitable to be a Telegram sticker\\.
Send it to [Stickers](https://t.me/Stickers) to add it as a new sticker\\.
The media you sent was already suitable to be a Telegram sticker.
Send it to [Stickers bot](https://t.me/Stickers) to add it as a new sticker.
"""),
FILE_TOO_LARGE("""
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\\.
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.
"""),
ABOUT("""
This bot is open source, check it out on [Github](https://github.com/Stickerifier/Stickerify)\\.
This bot is open source, check it out on [GitHub](https://github.com/Stickerifier/Stickerify).

Looking for sticker packs? Try [MeminiCustom](https://t.me/addstickers/MeminiCustom) and [VideoMemini](https://t.me/addstickers/VideoMemini)\\!
Looking for sticker packs? Try [MeminiCustom](https://t.me/addstickers/MeminiCustom) and [VideoMemini](https://t.me/addstickers/VideoMemini)!
"""),
ERROR("""
The file conversion was unsuccessful: only images, gifs, standard and video stickers are supported\\.
The file conversion was unsuccessful: use the **/supported** command to read the full list of supported files.

If you think it should have worked, please report the issue on [Github](https://github.com/Stickerifier/Stickerify/issues/new/choose)\\.
""", true),
If you think it should have worked, please report the issue on [GitHub](https://github.com/Stickerifier/Stickerify/issues/new/choose).
"""),
PRIVACY_POLICY("""
You can view our privacy policy by visiting [this link](https://stickerifier.github.io/Stickerify/PRIVACY_POLICY.html)\\.
You can view our privacy policy by visiting [this link](https://stickerifier.github.io/Stickerify/PRIVACY_POLICY.html).

If you have any questions or concerns, feel free to reach out to us at [stickerifier@gmail.com](mailto:stickerifier@gmail.com).
"""),
PROCESSING("""
<tg-thinking>Processing file...</tg-thinking>
"""),
SUPPORTED_FORMATS("""
| Type | Supported formats |
|:---------|:------------------------------------------------|
| images | png, jpg, static webp, tiff, ico, svg, psd |
| videos | gif, mov, avi, mp4, webm, m4v, mkv, live photos |
| stickers | static, video, animated |

If you have any questions or concerns, feel free to reach out to us\\.
---

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).
Comment thread
coderabbitai[bot] marked this conversation as resolved.
""");

private final String text;
private final boolean disableLinkPreview;

Answer(String text) {
this(text, false);
}

Answer(String text, boolean disableLinkPreview) {
this.text = text;
this.disableLinkPreview = disableLinkPreview;
}

public String getText() {
return text;
}

public boolean isDisableLinkPreview() {
return disableLinkPreview;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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 com.github.stickerifier.stickerify.telegram.Answer.SUPPORTED_FORMATS;
import static java.util.Comparator.comparing;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -31,6 +32,7 @@ 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";
private static final String SUPPORTED_COMMAND = "/supported";

public @Nullable TelegramFile getFile() {
return getMessageMedia()
Expand Down Expand Up @@ -82,6 +84,7 @@ public Answer getAnswerMessage() {
return switch (message.text()) {
case HELP_COMMAND, START_COMMAND -> HELP;
case PRIVACY_COMMAND -> PRIVACY_POLICY;
case SUPPORTED_COMMAND -> SUPPORTED_FORMATS;
case null, default -> ABOUT;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public final class MockResponses {

static final MockResponse EMPTY_UPDATES = new MockResponse.Builder().body("""
static final MockResponse SUCCESS_RESPONSE = new MockResponse.Builder().body("""
{
ok: true
}
Expand Down Expand Up @@ -76,6 +76,27 @@ public final class MockResponses {
}
""").build();

static final MockResponse SUPPORTED_MESSAGE = new MockResponse.Builder().body("""
{
ok: true,
result: [
{
update_id: 1,
message: {
message_id: 1,
from: {
id: 123456
},
chat: {
id: 1
},
text: "/supported"
}
}
]
}
""").build();

static final MockResponse FILE_NOT_SUPPORTED = new MockResponse.Builder().body("""
{
ok: true,
Expand Down
Loading
Loading