From 899d75d5165b7156dfece893523a7131232a195a Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Sun, 28 Jul 2024 15:00:48 +0700 Subject: upd status, clean db, clean telegram queue --- bot.py | 14 +++++++------- main.py | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bot.py b/bot.py index 7a22a35..c985ff4 100644 --- a/bot.py +++ b/bot.py @@ -64,10 +64,12 @@ class Bot: self.isProcessingPolling = True try: - url = f'https://api.telegram.org/bot{self.token}/getUpdates?offset=-10' + url = f'https://api.telegram.org/bot{self.token}/getUpdates' response = requests.get(url) if response.status_code == 200: self._dbUpdate(response.json()) + # Clean queue after reading because apparently it has maximum size and can block newer messages + self.cleanTelegramApiQueue() except requests.exceptions.ConnectionError as e: logger.error("Connection error during polling update") self._checkUnreadDb() @@ -95,20 +97,18 @@ class Bot: def cleanTelegramApiQueue(self): try: - urlGetMaxId = f'https://api.telegram.org/bot{self.token}/getUpdates?offset=-1' - response = requests.get(urlGetMaxId) + baseGetUpdateUrl = f'https://api.telegram.org/bot{self.token}/getUpdates' + response = requests.get(f'{baseGetUpdateUrl}?offset=-1') data = response.json() if data['result']: - maxId = data['result'][0]['update_id'] + lastUpdateId = data['result'][0]['update_id'] # dapet trik dari stackoverflow # https://stackoverflow.com/questions/61976560/how-to-delete-queue-updates-in-telegram-api - urlRemoveApiQueue = f'https://api.telegram.org/bot{self.token}/getUpdates?offset={maxId + 1}' - requests.get(urlRemoveApiQueue) + requests.get(f'{baseGetUpdateUrl}?offset={lastUpdateId + 1}') except requests.exceptions.ConnectionError as e: logger.error("Connection error during polling update") if __name__ == '__main__': bot = Bot() - # bot.sendMessage('*harusnya ini bold* dan ini ngga') bot.getNewMessages() diff --git a/main.py b/main.py index 17d7bbc..6753a42 100644 --- a/main.py +++ b/main.py @@ -45,7 +45,7 @@ def sendLog(msg): bot.sendMessage(f"Selesai menampilkan log. Line terakhir: {maxLines}", log=False) -def processMessages(messages): +def handleCommand(messages): for msg in messages: msg = msg.lower() if msg == 'update': @@ -105,7 +105,7 @@ if __name__ == '__main__': initDb() while True: if not bot.isProcessingPolling and bot.hasNewMessages: - processMessages(bot.getNewMessages()) + handleCommand(bot.getNewMessages()) else: bot.pollUpdate() run_pending() -- cgit v1.2.3-70-g09d2