diff options
Diffstat (limited to 'bot.py')
-rw-r--r-- | bot.py | 20 |
1 files changed, 18 insertions, 2 deletions
@@ -34,6 +34,7 @@ class Bot: conn = getDbConn() cur = conn.cursor() for data in data['result']: + updateId = data['update_id'] msg = data['message'] msgId = msg['message_id'] msgDate = msg['date'] @@ -41,7 +42,8 @@ class Bot: text = msg['text'] res = cur.execute(f"SELECT id, is_read FROM incoming WHERE id = {msgId}") if not res.fetchall(): - cur.execute(f"INSERT INTO incoming VALUES ({msgId}, {msgDate}, '{msgFrom}', '{text}', False)") + cur.execute( + f"INSERT INTO incoming VALUES ({msgId}, {msgDate}, '{msgFrom}', '{text}', False, {updateId})") self.hasNewMessages = True conn.commit() conn.close() @@ -62,7 +64,7 @@ class Bot: self.isProcessingPolling = True try: - url = f'https://api.telegram.org/bot{self.token}/getUpdates' + url = f'https://api.telegram.org/bot{self.token}/getUpdates?offset=-10' response = requests.get(url) if response.status_code == 200: self._dbUpdate(response.json()) @@ -91,6 +93,20 @@ class Bot: finally: return result + def cleanTelegramApiQueue(self): + try: + urlGetMaxId = f'https://api.telegram.org/bot{self.token}/getUpdates?offset=-1' + response = requests.get(urlGetMaxId) + data = response.json() + if data['result']: + maxId = 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) + except requests.exceptions.ConnectionError as e: + logger.error("Connection error during polling update") + if __name__ == '__main__': bot = Bot() |