From c89b96bf9716d0bcf603d180fe7640b0651a9b3f Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Mon, 25 Sep 2023 11:19:24 +0300 Subject: [PATCH 1/2] Simplified LongPollBot#startPolling, created getter and setters --- .../java/api/longpoll/bots/LongPollBot.java | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/main/java/api/longpoll/bots/LongPollBot.java b/src/main/java/api/longpoll/bots/LongPollBot.java index d4ff5f55..9c075083 100644 --- a/src/main/java/api/longpoll/bots/LongPollBot.java +++ b/src/main/java/api/longpoll/bots/LongPollBot.java @@ -56,20 +56,16 @@ public abstract class LongPollBot extends VkBot { * @throws VkApiException if errors occur. */ public void startPolling() throws VkApiException { - initialize(); while (polling) { try { - if (isSessionExpired()) { - initialize(); - } GetUpdates.ResponseBody updates = getUpdates().execute(); - ts = updates.getTs(); + setTs(updates.getTs()); handle(updates.getEvents()); } catch (VkResponseException e) { if (!e.getMessage().contains("failed")) { throw e; } - initialize(); + initializedAt = null; } } } @@ -95,10 +91,10 @@ public void initialize() throws VkApiException { .getResponse() .getAsJsonObject(); - server = longPollServer.get("server").getAsString(); - key = longPollServer.get("key").getAsString(); - if (ts == null) { - ts = longPollServer.get("ts").getAsInt(); + setServer(longPollServer.get("server").getAsString()); + setKey(longPollServer.get("key").getAsString()); + if (getTs() == null) { + setTs(longPollServer.get("ts").getAsInt()); } } @@ -125,9 +121,36 @@ private boolean isSessionExpired() { * * @return {@link GetUpdates} instance. */ - private GetUpdates getUpdates() { - return new GetUpdates(server) - .setKey(key) - .setTs(ts); + public GetUpdates getUpdates() throws VkApiException { + if (initializedAt == null || isSessionExpired()) { + initialize(); + } + return new GetUpdates(getServer()) + .setKey(getKey()) + .setTs(getTs()); + } + + public String getServer() { + return server; + } + + public void setServer(String server) { + this.server = server; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public Integer getTs() { + return ts; + } + + public void setTs(Integer ts) { + this.ts = ts; } } From f6fab7112af800ee3f625acdf588d0d59d2f5150 Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Mon, 25 Sep 2023 11:20:02 +0300 Subject: [PATCH 2/2] v4.1.8 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 83b51bc4..caef34a0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.yvasyliev java-vk-bots-longpoll-api jar - 4.1.7 + 4.1.8 Java VK Bots Long Poll API A Java library to create VK bots using Bots Long Poll API https://github.com/yvasyliev/java-vk-bots-long-poll-api