Skip to content

Commit

Permalink
Merge pull request #178 from yvasyliev/dev
Browse files Browse the repository at this point in the history
LongPollBot#getUpdates now public
  • Loading branch information
yvasyliev authored Sep 25, 2023
2 parents 3429b7a + f6fab71 commit 2465744
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.github.yvasyliev</groupId>
<artifactId>java-vk-bots-longpoll-api</artifactId>
<packaging>jar</packaging>
<version>4.1.7</version>
<version>4.1.8</version>
<name>Java VK Bots Long Poll API</name>
<description>A Java library to create VK bots using Bots Long Poll API</description>
<url>https://github.com/yvasyliev/java-vk-bots-long-poll-api</url>
Expand Down
51 changes: 37 additions & 14 deletions src/main/java/api/longpoll/bots/LongPollBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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());
}
}

Expand All @@ -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;
}
}

0 comments on commit 2465744

Please sign in to comment.