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 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; } }