From 343b8ebc85493d50b40844169820df72de6ccdf6 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Sat, 27 Jun 2015 21:55:45 +0800 Subject: [PATCH] Add ApiError exception. --- telebot/__init__.py | 6 +++++- telebot/apihelper.py | 33 +++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 2c304a2..e8483c3 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -60,9 +60,10 @@ class TeleBot: # over buffer size if len(self.update_id_list) > self.max_message_size: # remove oldest element. - upid = self.update_id_list[0] + 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) @@ -177,3 +178,6 @@ class TeleBot: :return: """ return apihelper.send_data(self.token, chat_id, data, 'video', reply_to_message_id, reply_markup) + + + diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 65fa75e..6daef8a 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -9,7 +9,7 @@ def get_me(token): method_url = r'getMe' request_url = api_url + 'bot' + token + '/' + method_url req = requests.get(request_url) - return req.json() + return check_result(method_url, req) def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None): @@ -34,7 +34,7 @@ def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_m if reply_markup: payload['reply_markup'] = reply_markup req = requests.get(request_url, params=payload) - return req.json() + return check_result(method_url, req) def get_updates(token): @@ -42,7 +42,7 @@ def get_updates(token): method_url = r'getUpdates' request_url = api_url + 'bot' + token + '/' + method_url req = requests.get(request_url) - return req.json() + return check_result(method_url,req) def forward_message(token, chat_id, from_chat_id, message_id): @@ -51,7 +51,7 @@ def forward_message(token, chat_id, from_chat_id, message_id): request_url = api_url + 'bot' + token + '/' + method_url payload = {'chat_id': chat_id, 'from_chat_id': from_chat_id, 'message_id': message_id} req = requests.get(request_url, params=payload) - return req.json() + return check_result(method_url, req) def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None): @@ -67,7 +67,8 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re if reply_markup: payload['reply_markup'] = reply_markup req = requests.post(request_url, params=payload, files=files) - return req.json() + 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 @@ -80,7 +81,9 @@ def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_m if reply_markup: payload['reply_markup'] = reply_markup req = requests.post(request_url, params=payload, files=files) - return req.json() + req.status_code + return check_result(method_url, req) + def get_method_by_type(data_type): if data_type == 'audio': @@ -91,3 +94,21 @@ def get_method_by_type(data_type): return 'sendSticker' if data_type == 'video': return 'sendVideo' + + +def check_result(func_name, result): + if result.status_code != 200: + raise ApiError(func_name + r' error.', result) + try: + result_json = result.json() + if not result_json['ok']: + raise Exception('') + except: + raise ApiError(func_name + r' error.', result) + return result_json + + +class ApiError(Exception): + def __init__(self, message, result): + super(ApiError, self).__init__(message) + self.result = result