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

LongPollBot#getUpdates now public #178

Merged
merged 2 commits into from
Sep 25, 2023
Merged
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
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;
}
}
Loading