diff --git a/telegram/bot.py b/telegram/bot.py index 792ae14b6..afbdf341f 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -139,7 +139,7 @@ class Bot(TelegramObject): url, data = func(self, *args, **kwargs) if not data.get('chat_id'): - raise TelegramError('Invalid chat_id.') + raise TelegramError('Invalid chat_id') if kwargs.get('reply_to_message_id'): reply_to_message_id = kwargs.get('reply_to_message_id') @@ -389,7 +389,7 @@ class Bot(TelegramObject): if filename: data['filename'] = filename - + return url, data @log @@ -422,7 +422,7 @@ class Bot(TelegramObject): data = {'chat_id': chat_id, 'sticker': sticker} - + return url, data @log diff --git a/telegram/error.py b/telegram/error.py index fdbc00319..f8b7e21f2 100644 --- a/telegram/error.py +++ b/telegram/error.py @@ -18,6 +18,8 @@ """This module contains a object that represents a Telegram Error""" +import re + class TelegramError(Exception): """This object represents a Telegram Error.""" @@ -29,7 +31,11 @@ class TelegramError(Exception): """ super(TelegramError, self).__init__() - self.message = message.split(':')[-1].strip().capitalize() + api_error = re.match(r'^Error: (?P.*)', message) + if api_error: + self.message = api_error.group('message').capitalize() + else: + self.message = message def __str__(self): return '%s' % (self.message) diff --git a/telegram/inputfile.py b/telegram/inputfile.py index f86406c48..0946f9724 100644 --- a/telegram/inputfile.py +++ b/telegram/inputfile.py @@ -164,7 +164,7 @@ class InputFile(object): if image: return 'image/%s' % image - raise TelegramError({'message': 'Could not parse file content'}) + raise TelegramError('Could not parse file content') @staticmethod def is_inputfile(data): @@ -177,7 +177,7 @@ class InputFile(object): bool """ if data: - file_types = ['audio', 'document', 'photo', 'sticker', 'video', + file_types = ['audio', 'document', 'photo', 'sticker', 'video', 'voice', 'certificate'] file_type = [i for i in list(data.keys()) if i in file_types] diff --git a/telegram/utils/request.py b/telegram/utils/request.py index be0593cbd..54aabac96 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# pylint: disable=no-name-in-module +# pylint: disable=no-name-in-module,unused-import # # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015 Leandro Toledo de Souza @@ -44,13 +44,10 @@ def _parse(json_data): Returns: A JSON parsed as Python dict with results. """ - try: - data = json.loads(json_data.decode()) + data = json.loads(json_data.decode()) - if not data.get('ok') and data.get('description'): - return data['description'] - except ValueError: - raise TelegramError({'message': 'JSON decoding'}) + if not data.get('ok') and data.get('description'): + return data['description'] return data['result'] @@ -84,20 +81,21 @@ def post(url, try: if InputFile.is_inputfile(data): data = InputFile(data) - request = Request(url, data=data.to_form(), - headers=data.headers) + request = Request(url, + data=data.to_form(), + headers=data.headers) else: data = json.dumps(data) - request = Request(url, data=data.encode(), - headers={'Content-Type': 'application/json'}) + request = Request(url, + data=data.encode(), + headers={'Content-Type': 'application/json'}) result = urlopen(request).read() except HTTPError as error: + if error.getcode() == 403: + raise TelegramError('Unauthorized') + message = _parse(error.read()) raise TelegramError(message) - except URLError as error: - raise TelegramError(str(error)) - except IOError as error: - raise TelegramError(str(error)) return _parse(result)