Skip to content

Commit 2465744

Browse files
authored
Merge pull request #178 from yvasyliev/dev
LongPollBot#getUpdates now public
2 parents 3429b7a + f6fab71 commit 2465744

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>com.github.yvasyliev</groupId>
77
<artifactId>java-vk-bots-longpoll-api</artifactId>
88
<packaging>jar</packaging>
9-
<version>4.1.7</version>
9+
<version>4.1.8</version>
1010
<name>Java VK Bots Long Poll API</name>
1111
<description>A Java library to create VK bots using Bots Long Poll API</description>
1212
<url>https://github.com/yvasyliev/java-vk-bots-long-poll-api</url>

src/main/java/api/longpoll/bots/LongPollBot.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,16 @@ public abstract class LongPollBot extends VkBot {
5656
* @throws VkApiException if errors occur.
5757
*/
5858
public void startPolling() throws VkApiException {
59-
initialize();
6059
while (polling) {
6160
try {
62-
if (isSessionExpired()) {
63-
initialize();
64-
}
6561
GetUpdates.ResponseBody updates = getUpdates().execute();
66-
ts = updates.getTs();
62+
setTs(updates.getTs());
6763
handle(updates.getEvents());
6864
} catch (VkResponseException e) {
6965
if (!e.getMessage().contains("failed")) {
7066
throw e;
7167
}
72-
initialize();
68+
initializedAt = null;
7369
}
7470
}
7571
}
@@ -95,10 +91,10 @@ public void initialize() throws VkApiException {
9591
.getResponse()
9692
.getAsJsonObject();
9793

98-
server = longPollServer.get("server").getAsString();
99-
key = longPollServer.get("key").getAsString();
100-
if (ts == null) {
101-
ts = longPollServer.get("ts").getAsInt();
94+
setServer(longPollServer.get("server").getAsString());
95+
setKey(longPollServer.get("key").getAsString());
96+
if (getTs() == null) {
97+
setTs(longPollServer.get("ts").getAsInt());
10298
}
10399
}
104100

@@ -125,9 +121,36 @@ private boolean isSessionExpired() {
125121
*
126122
* @return {@link GetUpdates} instance.
127123
*/
128-
private GetUpdates getUpdates() {
129-
return new GetUpdates(server)
130-
.setKey(key)
131-
.setTs(ts);
124+
public GetUpdates getUpdates() throws VkApiException {
125+
if (initializedAt == null || isSessionExpired()) {
126+
initialize();
127+
}
128+
return new GetUpdates(getServer())
129+
.setKey(getKey())
130+
.setTs(getTs());
131+
}
132+
133+
public String getServer() {
134+
return server;
135+
}
136+
137+
public void setServer(String server) {
138+
this.server = server;
139+
}
140+
141+
public String getKey() {
142+
return key;
143+
}
144+
145+
public void setKey(String key) {
146+
this.key = key;
147+
}
148+
149+
public Integer getTs() {
150+
return ts;
151+
}
152+
153+
public void setTs(Integer ts) {
154+
this.ts = ts;
132155
}
133156
}

0 commit comments

Comments
 (0)