Skip to content

Commit 72988dc

Browse files
authored
Fix outdated API from preventing all other updates and messages (#51)
1 parent 685ced6 commit 72988dc

File tree

8 files changed

+89
-28
lines changed

8 files changed

+89
-28
lines changed

jtelegrambotapi-core/src/main/java/com/jtelegram/api/TelegramBotRegistry.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
import com.jtelegram.api.chat.ChatMemberStatus;
99
import com.jtelegram.api.chat.ChatType;
1010
import com.jtelegram.api.chat.id.ChatId;
11+
import com.jtelegram.api.ex.TelegramException;
1112
import com.jtelegram.api.inline.keyboard.InlineKeyboardRow;
1213
import com.jtelegram.api.inline.result.framework.InlineResultType;
13-
import com.jtelegram.api.message.entity.MessageEntity;
14-
import com.jtelegram.api.update.Update;
15-
import com.jtelegram.api.update.UpdateProvider;
16-
import com.jtelegram.api.update.UpdateType;
17-
import com.jtelegram.api.util.LowercaseEnumAdapter;
1814
import com.jtelegram.api.message.Message;
19-
import com.jtelegram.api.ex.TelegramException;
15+
import com.jtelegram.api.message.entity.MessageEntity;
2016
import com.jtelegram.api.message.input.file.InputFile;
2117
import com.jtelegram.api.message.input.media.InputMediaType;
2218
import com.jtelegram.api.message.keyboard.ReplyKeyboardRow;
2319
import com.jtelegram.api.message.sticker.MaskPoint;
2420
import com.jtelegram.api.requests.GetMe;
21+
import com.jtelegram.api.update.Update;
22+
import com.jtelegram.api.update.UpdateProvider;
23+
import com.jtelegram.api.update.UpdateType;
24+
import com.jtelegram.api.util.LowercaseEnumAdapter;
2525
import lombok.Builder;
2626
import lombok.Getter;
2727
import okhttp3.OkHttpClient;
@@ -30,6 +30,7 @@
3030
import java.util.HashSet;
3131
import java.util.Set;
3232
import java.util.function.BiConsumer;
33+
import java.util.function.Consumer;
3334

3435
@Getter
3536
public class TelegramBotRegistry {
@@ -52,6 +53,7 @@ public class TelegramBotRegistry {
5253
.registerTypeHierarchyAdapter(InputFile.class, new InputFile.Serializer())
5354
.registerTypeHierarchyAdapter(ChatId.class, new ChatId.Serializer())
5455
.create();
56+
private static Consumer<TelegramException> minorGsonErrorHandler = (a) -> {};
5557
private final UpdateProvider updateProvider;
5658
private String apiUrl = "https://api.telegram.org/bot";
5759
private String fileApiUrl = "https://api.telegram.org/file/bot";
@@ -79,6 +81,14 @@ private TelegramBotRegistry(UpdateProvider updateProvider, String apiUrl, OkHttp
7981
}
8082
}
8183

84+
public static void setMinorGsonErrorHandler(Consumer<TelegramException> minorGsonErrorHandler) {
85+
TelegramBotRegistry.minorGsonErrorHandler = minorGsonErrorHandler;
86+
}
87+
88+
public static Consumer<TelegramException> getMinorGsonErrorHandler() {
89+
return minorGsonErrorHandler;
90+
}
91+
8292
public void setHttpClient(OkHttpClient client) {
8393
this.client = client;
8494
bots.forEach((bot) -> bot.getRequestQueue().setClient(client));

jtelegrambotapi-core/src/main/java/com/jtelegram/api/message/Message.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jtelegram.api.message;
22

33
import com.google.gson.*;
4+
import com.jtelegram.api.TelegramBotRegistry;
45
import com.jtelegram.api.chat.Chat;
56
import com.jtelegram.api.ex.InvalidResponseException;
67
import com.jtelegram.api.requests.message.DeleteMessage;
@@ -81,10 +82,13 @@ public Message deserialize(JsonElement jsonElement, Type type, JsonDeserializati
8182
}
8283
}
8384

84-
throw new InvalidResponseException (
85-
"Unfamiliar Message object, update the bot API?",
86-
object.toString()
85+
TelegramBotRegistry.getMinorGsonErrorHandler().accept (
86+
new InvalidResponseException (
87+
"Unfamiliar Message object, update the bot API?",
88+
object.toString()
89+
)
8790
);
91+
return null;
8892
}
8993
}
9094
}

jtelegrambotapi-core/src/main/java/com/jtelegram/api/message/entity/MessageEntity.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jtelegram.api.message.entity;
22

33
import com.google.gson.*;
4+
import com.jtelegram.api.TelegramBotRegistry;
45
import com.jtelegram.api.ex.InvalidResponseException;
56
import lombok.EqualsAndHashCode;
67
import lombok.Getter;
@@ -37,10 +38,14 @@ public MessageEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializa
3738
MessageEntityType type = context.deserialize(object.get("type"), MessageEntityType.class);
3839

3940
if (type == null) {
40-
throw new InvalidResponseException (
41-
"Invalid Message Entity Type. Update the API?",
42-
object.toString()
41+
TelegramBotRegistry.getMinorGsonErrorHandler().accept (
42+
new InvalidResponseException (
43+
"Invalid Message Entity Type. Update the API?",
44+
object.toString()
45+
)
4346
);
47+
48+
return null;
4449
}
4550

4651
return context.deserialize(object, type.getImplementationClass());

jtelegrambotapi-core/src/main/java/com/jtelegram/api/requests/framework/AbstractTelegramRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public abstract class AbstractTelegramRequest implements TelegramRequest {
2525
protected void handleError(TelegramException ex) {
2626
if (errorHandler == null) {
2727
System.out.println("Uncaught exception for " + getClass().getSimpleName() + "...");
28+
ex.printStackTrace();
2829
return;
2930
}
3031

jtelegrambotapi-core/src/main/java/com/jtelegram/api/update/PollingUpdateRunnable.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.jtelegram.api.update;
22

33
import com.jtelegram.api.TelegramBot;
4+
import com.jtelegram.api.ex.NetworkException;
45
import com.jtelegram.api.requests.GetUpdates;
56
import com.jtelegram.api.requests.framework.TelegramRequest;
67
import lombok.RequiredArgsConstructor;
78
import okhttp3.Response;
89

910
import java.io.IOException;
11+
import java.net.SocketTimeoutException;
12+
import java.util.Objects;
1013
import java.util.stream.Stream;
1114

1215
@RequiredArgsConstructor
@@ -23,7 +26,16 @@ public void run() {
2326
.offset(offset)
2427
.timeout(owner.getTimeout())
2528
.callback(this::handleUpdates)
26-
.errorHandler(owner.getUpdateErrorHandler())
29+
.errorHandler((error) -> {
30+
if (error instanceof NetworkException) {
31+
// this exception is expected for polling
32+
if (((NetworkException) error).getUnderlyingException() instanceof SocketTimeoutException) {
33+
return;
34+
}
35+
}
36+
37+
owner.getUpdateErrorHandler().accept(error);
38+
})
2739
.build();
2840

2941
try {
@@ -44,10 +56,12 @@ public void run() {
4456

4557
public void handleUpdates(Update[] updates) {
4658
for (Update update : updates) {
47-
handleUpdate(bot, UpdateType.from(update.getClass()), update);
59+
if (update != null) {
60+
handleUpdate(bot, UpdateType.from(update.getClass()), update);
61+
}
4862
}
4963

50-
offset = Stream.of(updates).mapToInt(Update::getUpdateId).max().orElse(offset - 1) + 1;
64+
offset = Stream.of(updates).filter(Objects::nonNull).mapToInt(Update::getUpdateId).max().orElse(offset - 1) + 1;
5165
}
5266

5367
public static <T extends Update> void handleUpdate(TelegramBot bot, UpdateType<T> type, Update update) {

jtelegrambotapi-core/src/main/java/com/jtelegram/api/update/Update.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jtelegram.api.update;
22

33
import com.google.gson.*;
4+
import com.jtelegram.api.TelegramBotRegistry;
45
import com.jtelegram.api.ex.InvalidResponseException;
56
import com.jtelegram.api.inline.CallbackQuery;
67
import com.jtelegram.api.inline.InlineQuery;
@@ -98,10 +99,13 @@ public Update deserialize(JsonElement jsonElement, Type type, JsonDeserializatio
9899
}
99100
}
100101

101-
throw new InvalidResponseException (
102-
"Unfamiliar update object, update the bot API?",
103-
object.toString()
102+
TelegramBotRegistry.getMinorGsonErrorHandler().accept (
103+
new InvalidResponseException (
104+
"Unfamiliar update object, update the bot API?",
105+
object.toString()
106+
)
104107
);
108+
return null;
105109
}
106110
}
107111
}

jtelegrambotapi-core/src/main/java/com/jtelegram/api/update/UpdateType.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@
33
import com.google.gson.TypeAdapter;
44
import com.google.gson.stream.JsonReader;
55
import com.google.gson.stream.JsonWriter;
6-
import com.jtelegram.api.events.Event;
7-
import com.jtelegram.api.events.location.LocationUpdateEvent;
8-
import com.jtelegram.api.events.payment.ShippingQueryEvent;
9-
import com.jtelegram.api.message.CaptionableMessage;
10-
import com.jtelegram.api.message.Message;
11-
import com.jtelegram.api.message.impl.LocationMessage;
126
import com.jtelegram.api.TelegramBot;
7+
import com.jtelegram.api.events.Event;
138
import com.jtelegram.api.events.channel.ChannelPostEditEvent;
149
import com.jtelegram.api.events.channel.ChannelPostEvent;
1510
import com.jtelegram.api.events.inline.ChosenInlineResultEvent;
1611
import com.jtelegram.api.events.inline.InlineQueryEvent;
1712
import com.jtelegram.api.events.inline.keyboard.CallbackQueryEvent;
13+
import com.jtelegram.api.events.location.LocationUpdateEvent;
1814
import com.jtelegram.api.events.message.MessageEvent;
1915
import com.jtelegram.api.events.message.edit.CaptionEditEvent;
2016
import com.jtelegram.api.events.message.edit.TextMessageEditEvent;
2117
import com.jtelegram.api.events.payment.PreCheckoutQueryEvent;
18+
import com.jtelegram.api.events.payment.ShippingQueryEvent;
19+
import com.jtelegram.api.message.CaptionableMessage;
20+
import com.jtelegram.api.message.Message;
2221
import com.jtelegram.api.message.MessageType;
22+
import com.jtelegram.api.message.impl.LocationMessage;
2323
import com.jtelegram.api.message.impl.TextMessage;
2424
import lombok.AccessLevel;
2525
import lombok.AllArgsConstructor;
2626
import lombok.Getter;
27+
import lombok.ToString;
2728

2829
import java.io.IOException;
2930
import java.lang.reflect.Constructor;
3031
import java.lang.reflect.InvocationTargetException;
3132
import java.util.function.BiFunction;
32-
import lombok.ToString;
3333

3434
@Getter
3535
@ToString
@@ -38,13 +38,25 @@ public class UpdateType<T extends Update> {
3838
public static final UpdateType<Update.ChannelPostUpdate> CHANNEL_POST = new UpdateType<>(
3939
"CHANNEL_POST",
4040
Update.ChannelPostUpdate.class,
41-
(bot, update) -> new ChannelPostEvent(bot, update.getChannelPost())
41+
(bot, update) -> {
42+
if (update.getChannelPost() == null) {
43+
return null;
44+
}
45+
46+
return new ChannelPostEvent(bot, update.getChannelPost());
47+
}
4248
);
4349

4450
public static final UpdateType<Update.EditedChannelPostUpdate> EDITED_CHANNEL_POST = new UpdateType<>(
4551
"EDITED_CHANNEL_POST",
4652
Update.EditedChannelPostUpdate.class,
47-
(bot, update) -> new ChannelPostEditEvent(bot, update.getEditedChannelPost())
53+
(bot, update) -> {
54+
if (update.getEditedChannelPost() == null) {
55+
return null;
56+
}
57+
58+
return new ChannelPostEditEvent(bot, update.getEditedChannelPost());
59+
}
4860
);
4961

5062
public static final UpdateType<Update.InlineQueryUpdate> INLINE_QUERY = new UpdateType<>(
@@ -81,6 +93,10 @@ public class UpdateType<T extends Update> {
8193
"MESSAGE",
8294
Update.MessageUpdate.class,
8395
(bot, update) -> {
96+
if (update.getMessage() == null) {
97+
return null;
98+
}
99+
84100
MessageType type = MessageType.typeFrom(update.getMessage());
85101
Class<? extends MessageEvent> eventClass = type.getReceiveEventClass();
86102
Constructor<? extends MessageEvent> constructor;
@@ -112,6 +128,10 @@ public class UpdateType<T extends Update> {
112128
(bot, update) -> {
113129
Message updatedMessage = update.getEditedMessage();
114130

131+
if (updatedMessage == null) {
132+
return null;
133+
}
134+
115135
if (updatedMessage instanceof TextMessage) {
116136
return new TextMessageEditEvent(bot, (TextMessage) updatedMessage);
117137
} else if (updatedMessage instanceof LocationMessage) {

jtelegrambotapi-webhooks/src/main/java/com/jtelegram/api/webhooks/WebhookUpdateProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public WebhookUpdateProvider(HttpServerOptions serverOptions, File selfSignedCer
7878
request.bodyHandler((buffer) -> {
7979
try {
8080
Update update = TelegramBotRegistry.GSON.fromJson(buffer.toString(), Update.class);
81-
PollingUpdateRunnable.handleUpdate(bot, UpdateType.from(update.getClass()), update);
81+
82+
if (update != null) {
83+
PollingUpdateRunnable.handleUpdate(bot, UpdateType.from(update.getClass()), update);
84+
}
8285
} catch (TelegramException ex) {
8386
this.errorHandler.accept(ex);
8487
}

0 commit comments

Comments
 (0)