mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Implemented the offset parameter.
Implemented the offset parameter for the getUpdates API call according to https://core.telegram.org/bots/api#getupdates In short: the offset is calculated by adding 1 to the id of the last received Update. Caching messages in a list is now redundant, so the cache is removed. Only the id of the last received Update is kept for future getUpdates calls.
This commit is contained in:
parent
53c76e75f7
commit
34da2552dd
@ -1,11 +1,12 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import apihelper
|
|
||||||
import json
|
import json
|
||||||
import types
|
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
import apihelper
|
||||||
|
import types
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Module : telebot
|
Module : telebot
|
||||||
"""
|
"""
|
||||||
@ -33,43 +34,24 @@ class TeleBot:
|
|||||||
|
|
||||||
def __init__(self, token):
|
def __init__(self, token):
|
||||||
self.token = token
|
self.token = token
|
||||||
self.update_entries = {}
|
|
||||||
self.update_listener = []
|
self.update_listener = []
|
||||||
self.chat_list = {}
|
|
||||||
self.update_id_list = []
|
|
||||||
self.max_message_size = 100000
|
|
||||||
self.polling_thread = None
|
self.polling_thread = None
|
||||||
self.__stop_polling = False
|
self.__stop_polling = False
|
||||||
self.interval = 3
|
self.interval = 3
|
||||||
|
|
||||||
def get_last_update_id(self):
|
self.last_update_id = 0
|
||||||
return self.update_id_list[-1] if len(self.update_id_list) > 0 else None
|
|
||||||
|
|
||||||
def get_update(self):
|
def get_update(self):
|
||||||
result = apihelper.get_updates(self.token)
|
result = apihelper.get_updates(self.token, offset=(self.last_update_id + 1))
|
||||||
if result['ok'] is not True:
|
|
||||||
raise Exception('getMe Error.' + json.dumps(result))
|
|
||||||
updates = result['result']
|
updates = result['result']
|
||||||
notify_updates = []
|
notify_updates = []
|
||||||
for update in updates:
|
for update in updates:
|
||||||
if update['update_id'] in self.update_entries:
|
if update['update_id'] > self.last_update_id:
|
||||||
continue
|
self.last_update_id = update['update_id']
|
||||||
msg = types.Message.de_json(json.dumps(update['message']))
|
msg = types.Message.de_json(json.dumps(update['message']))
|
||||||
self.__append_message_to_cache(update['update_id'], msg)
|
|
||||||
notify_updates.append(msg)
|
notify_updates.append(msg)
|
||||||
self.__notify_update(notify_updates)
|
self.__notify_update(notify_updates)
|
||||||
|
|
||||||
def __append_message_to_cache(self, update_id, message):
|
|
||||||
# over buffer size
|
|
||||||
if len(self.update_id_list) > self.max_message_size:
|
|
||||||
# remove oldest element.
|
|
||||||
upid = self.update_id_list.pop()
|
|
||||||
if upid in self.update_entries:
|
|
||||||
del self.update_entries[upid]
|
|
||||||
|
|
||||||
self.update_entries[update_id] = message
|
|
||||||
self.update_id_list.append(update_id)
|
|
||||||
|
|
||||||
def __notify_update(self, new_messages):
|
def __notify_update(self, new_messages):
|
||||||
for listener in self.update_listener:
|
for listener in self.update_listener:
|
||||||
t = threading.Thread(target=listener, args=(new_messages))
|
t = threading.Thread(target=listener, args=(new_messages))
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import telebot
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
import telebot
|
||||||
|
|
||||||
|
|
||||||
def get_me(token):
|
def get_me(token):
|
||||||
api_url = telebot.API_URL
|
api_url = telebot.API_URL
|
||||||
@ -124,7 +125,7 @@ def check_result(func_name, result):
|
|||||||
try:
|
try:
|
||||||
result_json = result.json()
|
result_json = result.json()
|
||||||
if not result_json['ok']:
|
if not result_json['ok']:
|
||||||
raise Exception('')
|
raise Exception(func_name, ' failed, result=' + result_json)
|
||||||
except:
|
except:
|
||||||
raise ApiError(func_name + r' error.', result)
|
raise ApiError(func_name + r' error.', result)
|
||||||
return result_json
|
return result_json
|
||||||
|
Loading…
Reference in New Issue
Block a user