Skip to content

Commit

Permalink
Rework error answer logic (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
rob93c authored Jan 29, 2024
1 parent 4d16252 commit 70512d1
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 254 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Based on what you send, [Stickerify](https://t.me/StickerifyImageBot) will answe
* the converted media, if you sent a supported file (images, gifs, videos, and existing Telegram stickers are supported)
that needed conversion
* no file, if you sent a media already suiting Telegram's requirements
* an error message, if you sent an unsupported file
* an error message, if you sent either an unsupported or a corrupted file
* an informative message for any message without a file

```mermaid
Expand Down Expand Up @@ -67,15 +67,15 @@ After you successfully set up the project, you will have to go through the follo
1. Chat with [BotFather](https://t.me/BotFather) and ask it to create a new bot
2. Copy the token it provided you and either:
* set it as the value of a new environment variable named `STICKERIFY_TOKEN`
* use it as the value passed to the `super(botToken)` constructor inside `Stickerify`
* use it as the value passed to the `super(botToken)` constructor inside the class `Stickerify`
3. Install [FFmpeg](https://ffmpeg.org/download.html)
4. Run the `Main` class to start the bot, it will be now able to answer messages in Telegram

## How to launch the bot using Docker

1. Install [Docker](https://docs.docker.com/get-docker/)
2. Prepare the Docker image either:
* building it with the command:
* moving into the project folder and building the image with the command:
```shell
docker build -t rob93c/stickerify .
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.stickerifier.stickerify.bot;

import static com.github.stickerifier.stickerify.telegram.Answer.CORRUPTED;
import static com.github.stickerifier.stickerify.telegram.Answer.ERROR;
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_ALREADY_VALID;
import static com.github.stickerifier.stickerify.telegram.Answer.FILE_READY;
Expand Down Expand Up @@ -150,10 +151,11 @@ private File retrieveFile(String fileId) throws TelegramApiException {
private void processFailure(TelegramRequest request, TelegramApiException e) {
if (e.getMessage().endsWith("Bad Request: message to reply not found")) {
LOGGER.atInfo().log("Unable to reply to {} because the message sent has been deleted", request.getDescription());
} else if (e.getMessage().equals("The video could not be processed successfully")) {
LOGGER.atWarn().setCause(e).log("Unable to process the file {}", request.getFile().id());
} else if ("The video could not be processed successfully".equals(e.getMessage())) {
LOGGER.atWarn().setCause(e).log("Unable to reply to {}: the file is corrupted", request.getDescription());
answerText(CORRUPTED, request);
} else {
LOGGER.atWarn().setCause(e).log("Unable to reply to {} with processed file", request.getDescription());
LOGGER.atWarn().setCause(e).log("Unable to process the file {}", request.getFile().id());
answerText(ERROR, request);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ private static boolean isVideoCompliant(File file, MultimediaInfo mediaInfo) thr
return isSizeCompliant(videoSize.getWidth(), videoSize.getHeight())
&& videoInfo.getFrameRate() <= MAX_VIDEO_FRAMES
&& videoInfo.getDecoder().startsWith(VP9_CODEC)
&& mediaInfo.getDuration() > 0L
&& mediaInfo.getDuration() <= MAX_VIDEO_DURATION_MILLIS
&& mediaInfo.getAudio() == null
&& MATROSKA_FORMAT.equals(mediaInfo.getFormat())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum Answer {
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 an unsupported file
\\- an error message, if you sent either an unsupported or a corrupted file
\\- an informative message for any message without a file
"""),
FILE_READY("""
Expand All @@ -33,6 +33,11 @@ public enum Answer {
ERROR("""
The file conversion was unsuccessful: only images, gifs, standard and video stickers are supported\\.
If you think it should have worked, please report the issue on [Github](https://github.com/Stickerifier/Stickerify/issues/new/choose)\\.
""", true),
CORRUPTED("""
The conversion was unsuccessful: the video might be corrupted and it cannot be processed\\.
If you think it should have worked, please report the issue on [Github](https://github.com/Stickerifier/Stickerify/issues/new/choose)\\.
""", true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
package com.github.stickerifier.stickerify;

import static org.junit.jupiter.api.Assumptions.abort;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public final class ResourceHelper {

private final File directory;

public ResourceHelper(File directory) {
this.directory = directory;
}

public File createImage(int width, int height, String extension) {
var image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
var file = new File(directory, "%d x %d.%s".formatted(width, height, extension));

try {
ImageIO.write(image, extension, file);
} catch (IOException e) {
abort("Image could not be written to file [%s].".formatted(file.getName()));
} finally {
image.flush();
}

return file;
}

public File loadResource(String filename) {
var resource = getClass().getClassLoader().getResource(filename);
public static File loadResource(String filename) {
var resource = ResourceHelper.class.getClassLoader().getResource(filename);
assumeTrue(resource != null, "Test resource [%s] not found.".formatted(filename));

return new File(resource.getFile());
}

private ResourceHelper() {
throw new UnsupportedOperationException();
}
}
Loading

0 comments on commit 70512d1

Please sign in to comment.