From 34da2552dd5a980ac8c8b2371c84710c1eacc7c6 Mon Sep 17 00:00:00 2001 From: pieter Date: Tue, 30 Jun 2015 05:49:35 +0200 Subject: [PATCH 1/2] 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. --- telebot/__init__.py | 32 +++++++------------------------- telebot/apihelper.py | 5 +++-- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 057f281..ac54d20 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -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)) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 17dfed7..0444c4c 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -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 From af02000534a38815abbfd68b3ff8c83356db3f83 Mon Sep 17 00:00:00 2001 From: pieter Date: Tue, 30 Jun 2015 05:54:04 +0200 Subject: [PATCH 2/2] PEP 8 & IntelliJ code clean-up. --- telebot/__init__.py | 2 +- telebot/apihelper.py | 4 +++- telebot/types.py | 15 +++++++-------- tests/test_types.py | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index ac54d20..ca6de4c 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -54,7 +54,7 @@ class TeleBot: def __notify_update(self, new_messages): for listener in self.update_listener: - t = threading.Thread(target=listener, args=(new_messages)) + t = threading.Thread(target=listener, args=new_messages) t.start() def polling(self, interval=3): diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 0444c4c..c381e8b 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -86,7 +86,8 @@ def send_location(token, chat_id, latitude, longitude, reply_to_message_id=None, req = requests.get(request_url, params=payload) return check_result(method_url, req) -def send_chat_action(token,chat_id,action): + +def send_chat_action(token, chat_id, action): api_url = telebot.API_URL method_url = r'sendChatAction' request_url = api_url + 'bot' + token + '/' + method_url @@ -94,6 +95,7 @@ def send_chat_action(token,chat_id,action): req = requests.get(request_url, params=payload) return check_result(method_url, req) + def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None): api_url = telebot.API_URL method_url = get_method_by_type(data_type) diff --git a/telebot/types.py b/telebot/types.py index 872732c..efba253 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -62,7 +62,7 @@ class Message: def de_json(cls, json_string): obj = json.loads(json_string) message_id = obj['message_id'] - fromUser = User.de_json(json.dumps(obj['from'])) + from_user = User.de_json(json.dumps(obj['from'])) chat = Message.parse_chat(obj['chat']) date = obj['date'] content_type = None @@ -88,7 +88,7 @@ class Message: if 'location' in obj: opts['location'] = Location.de_json(json.dumps(obj['location'])) content_type = 'location' - return Message(message_id, fromUser, date, chat, content_type, opts) + return Message(message_id, from_user, date, chat, content_type, opts) @classmethod def parse_chat(cls, chat): @@ -262,7 +262,7 @@ class UserProfilePhotos: class ReplyKeyboardMarkup: - def __init__(self, keyboard=[], resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3): + def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3): self.resize_keyboard = resize_keyboard self.one_time_keyboard = one_time_keyboard self.selective = selective @@ -307,15 +307,14 @@ class ReplyKeyboardMarkup: https://core.telegram.org/bots/api#replykeyboardmarkup :return: """ - json_dict = {} - json_dict['keyboard'] = self.keyboard - if self.one_time_keyboard != False: + json_dict = {'keyboard': self.keyboard} + if self.one_time_keyboard: json_dict['one_time_keyboard'] = True - if self.resize_keyboard != False: + if self.resize_keyboard: json_dict['resize_keyboard'] = True - if self.selective != False: + if self.selective: json_dict['selective'] = True return json.dumps(json_dict) diff --git a/tests/test_types.py b/tests/test_types.py index cb5887e..e548145 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -34,7 +34,7 @@ def test_json_GroupChat(): def test_json_Document(): json_string = r'{"file_name":"Text File","thumb":{},"file_id":"BQADBQADMwIAAsYifgZ_CEh0u682xwI","file_size":446}' doc = types.Document.de_json(json_string) - assert doc.thumb == None + assert doc.thumb is None assert doc.file_name == 'Text File'