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

feat: Ensure Telegram API Error Exception details are not being masked #1453

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<module>telegrambots-longpolling</module>
<module>telegrambots-webhook</module>
<module>telegrambots-client</module>
<module>telegrambots-client-jetty-adapter</module>
<module>telegrambots-springboot-longpolling-starter</module>
<module>telegrambots-springboot-webhook-starter</module>
<module>telegrambots-extensions</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,21 +454,20 @@ private long getUserIdSendError(String username, MessageContext ctx) {
}
}


private Optional<Message> send(String message, MessageContext ctx, String... args) {
return bot.silent.send(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId());
return bot.silent.send(getLocalizedMessage(message, ctx.user().getLanguageCode(), (Object[]) args), ctx.chatId());
}

private Optional<Message> sendMd(String message, MessageContext ctx, String... args) {
return bot.silent.sendMd(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId());
return bot.silent.sendMd(getLocalizedMessage(message, ctx.user().getLanguageCode(), (Object[]) args), ctx.chatId());
}

private Optional<Message> send(String message, Update upd) {
Long chatId = upd.getMessage().getChatId();
return bot.silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId);
}

protected File downloadFileWithId(String fileId) throws TelegramApiException {
private File downloadFileWithId(String fileId) throws TelegramApiException {
return bot.telegramClient.downloadFile(bot.telegramClient.execute(GetFile.builder().fileId(fileId).build()));
}
}
99 changes: 99 additions & 0 deletions telegrambots-client-jetty-adapter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<version>7.11.0</version>
</parent>

<name>Telegram Bots Client Jetty HttpClient adapter</name>
<url>https://github.com/rubenlagus/TelegramBots</url>
<description>Use Jetty HttpClient instead of OkHttp to perform API calls</description>

<artifactId>telegrambots-client-jetty-adapter</artifactId>
<packaging>jar</packaging>

<properties>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<jetty.version>12.0.12</jetty.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-meta</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package org.telegram.telegrambots.client;

import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto;
import org.telegram.telegrambots.meta.api.methods.send.SendAnimation;
import org.telegram.telegrambots.meta.api.methods.send.SendAudio;
import org.telegram.telegrambots.meta.api.methods.send.SendDocument;
import org.telegram.telegrambots.meta.api.methods.send.SendMediaGroup;
import org.telegram.telegrambots.meta.api.methods.send.SendPaidMedia;
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
import org.telegram.telegrambots.meta.api.methods.send.SendSticker;
import org.telegram.telegrambots.meta.api.methods.send.SendVideo;
import org.telegram.telegrambots.meta.api.methods.send.SendVideoNote;
import org.telegram.telegrambots.meta.api.methods.send.SendVoice;
import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet;
import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet;
import org.telegram.telegrambots.meta.api.methods.stickers.ReplaceStickerInSet;
import org.telegram.telegrambots.meta.api.methods.stickers.SetStickerSetThumbnail;
import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageMedia;
import org.telegram.telegrambots.meta.api.objects.File;
import org.telegram.telegrambots.meta.api.objects.message.Message;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;

import java.io.InputStream;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
* Abstract client implementation which delegates all synchronous methods to the async methods. Preferable for implementations
*/
public abstract class AbstractTelegramClient implements TelegramClient {
@Override
public <T extends Serializable, Method extends BotApiMethod<T>> T execute(Method method) throws TelegramApiException {
try {
return executeAsync(method).get();
} catch (Exception e) {
throw mapException(e, method.getMethod());
}
}

@Override
public Message execute(SendDocument sendDocument) throws TelegramApiException {
try {
return executeAsync(sendDocument).get();
} catch (Exception e) {
throw mapException(e, sendDocument.getMethod());
}
}

@Override
public Message execute(SendPhoto sendPhoto) throws TelegramApiException {
try {
return executeAsync(sendPhoto).get();
} catch (Exception e) {
throw mapException(e, sendPhoto.getMethod());
}
}

@Override
public Boolean execute(SetWebhook setWebhook) throws TelegramApiException {
try {
return executeAsync(setWebhook).get();
} catch (Exception e) {
throw mapException(e, setWebhook.getMethod());
}
}

@Override
public Message execute(SendVideo sendVideo) throws TelegramApiException {
try {
return executeAsync(sendVideo).get();
} catch (Exception e) {
throw mapException(e, sendVideo.getMethod());
}
}

@Override
public Message execute(SendVideoNote sendVideoNote) throws TelegramApiException {
try {
return executeAsync(sendVideoNote).get();
} catch (Exception e) {
throw mapException(e, sendVideoNote.getMethod());
}
}

@Override
public Message execute(SendSticker sendSticker) throws TelegramApiException {
try {
return executeAsync(sendSticker).get();
} catch (Exception e) {
throw mapException(e, sendSticker.getMethod());
}
}

@Override
public Message execute(SendAudio sendAudio) throws TelegramApiException {
try {
return executeAsync(sendAudio).get();
} catch (Exception e) {
throw mapException(e, sendAudio.getMethod());
}
}

@Override
public Message execute(SendVoice sendVoice) throws TelegramApiException {
try {
return executeAsync(sendVoice).get();
} catch (Exception e) {
throw mapException(e, sendVoice.getMethod());
}
}

@Override
public List<Message> execute(SendMediaGroup sendMediaGroup) throws TelegramApiException {
try {
return executeAsync(sendMediaGroup).get();
} catch (Exception e) {
throw mapException(e, sendMediaGroup.getMethod());
}
}

@Override
public List<Message> execute(SendPaidMedia sendPaidMedia) throws TelegramApiException {
try {
return executeAsync(sendPaidMedia).get();
} catch (Exception e) {
throw mapException(e, sendPaidMedia.getMethod());
}
}

@Override
public Boolean execute(SetChatPhoto setChatPhoto) throws TelegramApiException {
try {
return executeAsync(setChatPhoto).get();
} catch (Exception e) {
throw mapException(e, setChatPhoto.getMethod());
}
}

@Override
public Boolean execute(AddStickerToSet addStickerToSet) throws TelegramApiException {
try {
return executeAsync(addStickerToSet).get();
} catch (Exception e) {
throw mapException(e, addStickerToSet.getMethod());
}
}

@Override
public Boolean execute(ReplaceStickerInSet replaceStickerInSet) throws TelegramApiException {
try {
return executeAsync(replaceStickerInSet).get();
} catch (Exception e) {
throw mapException(e, replaceStickerInSet.getMethod());
}
}

@Override
public Boolean execute(SetStickerSetThumbnail setStickerSetThumbnail) throws TelegramApiException {
try {
return executeAsync(setStickerSetThumbnail).get();
} catch (Exception e) {
throw mapException(e, setStickerSetThumbnail.getMethod());
}
}

@Override
public Boolean execute(CreateNewStickerSet createNewStickerSet) throws TelegramApiException {
try {
return executeAsync(createNewStickerSet).get();
} catch (Exception e) {
throw mapException(e, createNewStickerSet.getMethod());
}
}

@Override
public File execute(UploadStickerFile uploadStickerFile) throws TelegramApiException {
try {
return executeAsync(uploadStickerFile).get();
} catch (Exception e) {
throw mapException(e, uploadStickerFile.getMethod());
}
}

@Override
public Serializable execute(EditMessageMedia editMessageMedia) throws TelegramApiException {
try {
return executeAsync(editMessageMedia).get();
} catch (Exception e) {
throw mapException(e, editMessageMedia.getMethod());
}
}

@Override
public Message execute(SendAnimation sendAnimation) throws TelegramApiException {
try {
return executeAsync(sendAnimation).get();
} catch (Exception e) {
throw mapException(e, sendAnimation.getMethod());
}
}

@Override
public java.io.File downloadFile(File file) throws TelegramApiException {
try {
return downloadFileAsync(file).get();
} catch (Exception e) {
throw mapException(e, " download file ");
}
}

@Override
public InputStream downloadFileAsStream(File file) throws TelegramApiException {
try {
return downloadFileAsStreamAsync(file).get();
} catch (Exception e) {
throw mapException(e, " download file ");
}
}

private TelegramApiException mapException(Exception e, String method) {
if (e instanceof ExecutionException) {
if (e.getCause() instanceof TelegramApiException) {
return (TelegramApiException) e.getCause();
} else {
return new TelegramApiException("Unable to execute " + method + " method", e.getCause());
}
} else {
return new TelegramApiException("Unable to execute " + method + " method", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.telegram.telegrambots.client;

/**
* Special type of Consumer that may throw a checked exception defined by parameter E
*/
@FunctionalInterface
public interface ThrowingConsumer<T, E extends Exception> {
void accept(T t) throws E;
}
Loading