Skip to content

Commit

Permalink
Merge pull request #140 from yvasyliev/137-question-не-присылает-сооб…
Browse files Browse the repository at this point in the history
…щения-через-большой-промежуток-времени

Fixed #137
  • Loading branch information
yvasyliev authored Nov 8, 2022
2 parents 879dfee + b100e48 commit 1d4b0f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 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>3.5.3</version>
<version>3.5.4-beta-1</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
48 changes: 46 additions & 2 deletions src/main/java/api/longpoll/bots/LongPollBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
* Abstract bot to handle VK events.
*/
Expand All @@ -17,6 +20,13 @@ public abstract class LongPollBot extends VkBot {
*/
private static final Logger LOGGER = LoggerFactory.getLogger(LongPollBot.class);

/**
* Default session time.
*
* @see LongPollBot#sessionDuration
*/
private static final long DEFAULT_SESSION_DURATION = 9;

/**
* Group ID.
*/
Expand All @@ -37,6 +47,17 @@ public abstract class LongPollBot extends VkBot {
*/
private boolean polling = true;

/**
* Last time when {@link LongPollBot#initialize()} was called.
*/
private LocalDateTime initializedAt;

/**
* Long Poll session duration (in hours).
* When time is expired, {@link LongPollBot#initialize()} method is called.
*/
private long sessionDuration = DEFAULT_SESSION_DURATION;

/**
* Begins listening to VK updates.
*
Expand All @@ -46,10 +67,13 @@ public void startPolling() throws VkApiException {
initialize();
while (polling) {
try {
if (isSessionExpired()) {
initialize();
}
GetUpdates.ResponseBody updates = getUpdates.execute();
getUpdates.setTs(updates.getTs());
handle(updates.getEvents());
} catch (VkApiHttpException | VkApiResponseException e) {
} catch (VkApiHttpException | VkApiResponseException e) {
LOGGER.warn("Failed to get events from VK Long Poll Server.", e);
if (e instanceof VkApiResponseException && !e.getMessage().contains("failed")) {
throw e;
Expand All @@ -71,7 +95,9 @@ public void stopPolling() {
*
* @throws VkApiException if errors occur.
*/
private void initialize() throws VkApiException {
public void initialize() throws VkApiException {
initializedAt = LocalDateTime.now();

if (groupId == null) {
groupId = vk.other.execute()
.setCode("return API.groups.getById()@.id[0];")
Expand All @@ -89,4 +115,22 @@ private void initialize() throws VkApiException {
.setKey(longPollServer.getResponse().getKey())
.setTs(longPollServer.getResponse().getTs());
}

/**
* Sets session duration (in hours).
*
* @param sessionDuration session duration (in hours).
*/
public void setSessionDuration(long sessionDuration) {
this.sessionDuration = sessionDuration;
}

/**
* Checks whether Long Poll session is expired.
*
* @return {@code true} if Long Poll session is expired, {@code false} otherwise.
*/
private boolean isSessionExpired() {
return ChronoUnit.HOURS.between(initializedAt, LocalDateTime.now()) >= sessionDuration;
}
}

0 comments on commit 1d4b0f6

Please sign in to comment.