1
0
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:
pieter 2015-06-30 05:49:35 +02:00
parent 53c76e75f7
commit 34da2552dd
2 changed files with 10 additions and 27 deletions

View File

@ -1,11 +1,12 @@
# -*- coding: utf-8 -*-
import apihelper
import json
import types
import time
import threading
import apihelper
import types
"""
Module : telebot
"""
@ -33,43 +34,24 @@ class TeleBot:
def __init__(self, token):
self.token = token
self.update_entries = {}
self.update_listener = []
self.chat_list = {}
self.update_id_list = []
self.max_message_size = 100000
self.polling_thread = None
self.__stop_polling = False
self.interval = 3
def get_last_update_id(self):
return self.update_id_list[-1] if len(self.update_id_list) > 0 else None
self.last_update_id = 0
def get_update(self):
result = apihelper.get_updates(self.token)
if result['ok'] is not True:
raise Exception('getMe Error.' + json.dumps(result))
result = apihelper.get_updates(self.token, offset=(self.last_update_id + 1))
updates = result['result']
notify_updates = []
for update in updates:
if update['update_id'] in self.update_entries:
continue
if update['update_id'] > self.last_update_id:
self.last_update_id = update['update_id']
msg = types.Message.de_json(json.dumps(update['message']))
self.__append_message_to_cache(update['update_id'], msg)
notify_updates.append(msg)
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):
for listener in self.update_listener:
t = threading.Thread(target=listener, args=(new_messages))

View File

@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
import telebot
import requests
import telebot
def get_me(token):
api_url = telebot.API_URL
@ -124,7 +125,7 @@ def check_result(func_name, result):
try:
result_json = result.json()
if not result_json['ok']:
raise Exception('')
raise Exception(func_name, ' failed, result=' + result_json)
except:
raise ApiError(func_name + r' error.', result)
return result_json