Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.3.1 #19

Merged
merged 5 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.9</version>
<version>6.0.7</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>6.7.0</version>
<version>6.5.0</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
Expand All @@ -96,7 +96,7 @@
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.16.1</version>
<version>1.15.4</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand All @@ -109,4 +109,4 @@
<version>2.20.0</version>
</dependency>
</dependencies>
</project>
</project>
40 changes: 31 additions & 9 deletions src/main/java/com/github/yvasyliev/telegram/chain/RepeatPhoto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -41,16 +42,25 @@ public void repeatRedditPost(JsonNode data, TelegramSenderBot telegramSenderBot,
} catch (TelegramApiRequestException e) {
var apiResponse = e.getApiResponse();

switch (apiResponse) {
case "Bad Request: wrong file identifier/HTTP URL specified":
try (var inputStream = new URL(photoUrl).openStream()) {
var filename = photoUrl.substring(photoUrl.lastIndexOf('/') + 1);
telegramSenderBot.sendPhoto(inputStream, filename, text, hasSpoiler, needModerate);
}
break;
if ("Bad Request: wrong file identifier/HTTP URL specified".equals(apiResponse)) {
try {
sendPhotoInputStream(
photoUrl,
(inputStream, filename) -> telegramSenderBot.sendPhoto(inputStream, filename, text, hasSpoiler, needModerate)
);
return;
} catch (TelegramApiRequestException ex) {
apiResponse = ex.getApiResponse();
}
}

default:
throw e;
if ("Bad Request: PHOTO_INVALID_DIMENSIONS".equals(apiResponse)) {
sendPhotoInputStream(
photoUrl,
(inputStream, filename) -> telegramSenderBot.sendDocument(inputStream, filename, text, needModerate)
);
} else {
throw e;
}
}
appData.setProperty("PREVIOUS_REDDIT_POST_CREATED", String.valueOf(data.get("created").intValue()));
Expand Down Expand Up @@ -90,4 +100,16 @@ private String extractPhotoUrl(JsonNode data) {

return null;
}

private void sendPhotoInputStream(String photoUrl, SenderFunction senderFunction) throws IOException, TelegramApiException {
try (var inputStream = new URL(photoUrl).openStream()) {
var filename = photoUrl.substring(photoUrl.lastIndexOf('/') + 1);
senderFunction.send(inputStream, filename);
}
}

@FunctionalInterface
private interface SenderFunction {
void send(InputStream inputStream, String filename) throws TelegramApiException;
}
}