@@ -20,9 +20,19 @@ public abstract class LongPollBot extends VkBot {
2020 private static final long DEFAULT_SESSION_DURATION = 9 ;
2121
2222 /**
23- * Gets VK updates .
23+ * Server URL .
2424 */
25- private GetUpdates getUpdates ;
25+ private String server ;
26+
27+ /**
28+ * Session key.
29+ */
30+ private String key ;
31+
32+ /**
33+ * Latest received event.
34+ */
35+ private int ts ;
2636
2737 /**
2838 * Whether infinite loop should be continued.
@@ -52,8 +62,8 @@ public void startPolling() throws VkApiException {
5262 if (isSessionExpired ()) {
5363 initialize ();
5464 }
55- GetUpdates .ResponseBody updates = getUpdates .execute ();
56- getUpdates . setTs ( updates .getTs () );
65+ GetUpdates .ResponseBody updates = getUpdates () .execute ();
66+ ts = updates .getTs ();
5767 handle (updates .getEvents ());
5868 } catch (VkResponseException e ) {
5969 if (!e .getMessage ().contains ("failed" )) {
@@ -85,9 +95,9 @@ public void initialize() throws VkApiException {
8595 .getResponse ()
8696 .getAsJsonObject ();
8797
88- getUpdates = new GetUpdates ( longPollServer .get ("server" ).getAsString ())
89- . setKey ( longPollServer .get ("key" ).getAsString ())
90- . setTs ( longPollServer .get ("ts" ).getAsInt () );
98+ server = longPollServer .get ("server" ).getAsString ();
99+ key = longPollServer .get ("key" ).getAsString ();
100+ ts = longPollServer .get ("ts" ).getAsInt ();
91101 }
92102
93103 /**
@@ -107,4 +117,15 @@ public void setSessionDuration(long sessionDuration) {
107117 private boolean isSessionExpired () {
108118 return ChronoUnit .HOURS .between (initializedAt , LocalDateTime .now ()) >= sessionDuration ;
109119 }
120+
121+ /**
122+ * Produces new {@link GetUpdates} instance based on {@link LongPollBot#server}, {@link LongPollBot#key} and {@link LongPollBot#ts}.
123+ *
124+ * @return {@link GetUpdates} instance.
125+ */
126+ private GetUpdates getUpdates () {
127+ return new GetUpdates (server )
128+ .setKey (key )
129+ .setTs (ts );
130+ }
110131}
0 commit comments