1515import com .pengrad .telegrambot .model .PhotoSize ;
1616import com .pengrad .telegrambot .model .Update ;
1717import com .pengrad .telegrambot .request .GetUpdates ;
18+ import org .apache .logging .log4j .LogManager ;
19+ import org .apache .logging .log4j .Logger ;
1820
1921public abstract class BaseBotHandler {
2022
23+ protected static final Logger LOGGER = LogManager .getLogger (BaseBotHandler .class );
24+
2125 private final Comparator <PhotoSize > photoSizeComparator = Comparator
2226 .comparingInt (ps -> ps .width () * ps .height ());
2327
@@ -33,23 +37,26 @@ public BaseBotHandler(String botToken) {
3337
3438 public void run () {
3539 int oldLastUpdateId = readLastUpdateId ();
40+ LOGGER .debug ("Start updates listener from {}" , oldLastUpdateId );
3641 bot .setUpdatesListener (updates -> {
3742 final var filteredUpdates = updates .stream ()
3843 .filter (u -> u .updateId () > oldLastUpdateId )
3944 .collect (Collectors .toList ());
4045 handleUpdates (filteredUpdates );
41- int newLastUpdateId = getLastUpdateIdFromUpdatesList (updates , oldLastUpdateId );
42- writeLastUpdateId ( newLastUpdateId + 1 );
46+ int nextLastUpdateId = geNextUpdateId (updates , oldLastUpdateId );
47+ writeNextUpdateId ( nextLastUpdateId );
4348 return UpdatesListener .CONFIRMED_UPDATES_ALL ;
4449 });
4550 }
4651
4752 public void runOnce () {
4853 int oldLastUpdateId = readLastUpdateId ();
54+ LOGGER .debug ("Get updates from {}" , oldLastUpdateId );
4955 final var updates = bot .execute (new GetUpdates ().offset (oldLastUpdateId )).updates ();
56+ LOGGER .debug ("Handle {} updates" , updates .size ());
5057 handleUpdates (updates );
51- int newLastUpdateId = getLastUpdateIdFromUpdatesList (updates , oldLastUpdateId );
52- writeLastUpdateId (newLastUpdateId + 1 );
58+ int newLastUpdateId = geNextUpdateId (updates , oldLastUpdateId );
59+ writeNextUpdateId (newLastUpdateId );
5360 }
5461
5562 protected abstract void handleUpdates (List <Update > updates );
@@ -74,23 +81,33 @@ protected PhotoSize getBiggestPhoto(PhotoSize[] photoSizes) {
7481 .orElse (photoSizes [0 ]);
7582 }
7683
77- private int getLastUpdateIdFromUpdatesList (List <Update > updates , int previousUpdateId ) {
78- return updates .stream ()
84+ private int geNextUpdateId (List <Update > updates , int previousUpdateId ) {
85+ final int lastUpdateId = updates .stream ()
7986 .mapToInt (Update ::updateId )
8087 .max ()
8188 .orElse (previousUpdateId );
89+ final int nextUpdateId ;
90+ if (lastUpdateId != previousUpdateId ) {
91+ nextUpdateId = lastUpdateId + 1 ;
92+ } else {
93+ nextUpdateId = lastUpdateId ;
94+ }
95+ return nextUpdateId ;
8296 }
8397
8498 private int readLastUpdateId () {
8599 try {
86100 return Integer .parseInt (Files .readString (uniqueIdPath ));
87101 } catch (IOException ioe ) {
102+ LOGGER .error ("readLastUpdateId" , ioe );
88103 return 0 ;
89104 }
90105 }
91106
92- private void writeLastUpdateId (int updateId ) {
107+ private void writeNextUpdateId (int updateId ) {
93108 try {
94109 Files .writeString (uniqueIdPath , Integer .toString (updateId ));
95- } catch (IOException ignore ) {}
110+ } catch (IOException ioe ) {
111+ LOGGER .error ("writeLastUpdateId" , ioe );
112+ }
96113 }}
0 commit comments