From 9b332e4a3bc67894ae9c4a55687ab76a096dc1dd Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 26 Mar 2017 13:26:01 +0200 Subject: [PATCH] fix error logs --- telegram.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/telegram.py b/telegram.py index fb9db66d..ad778043 100644 --- a/telegram.py +++ b/telegram.py @@ -139,12 +139,13 @@ class Bot: # Send the request to the Telegram API token = self.token async with session.request("GET", f"https://api.telegram.org/bot{token}/{endpoint}", params=params) as response: - # Check for errors in the request - if response.status != 200: - error = await response.text() - raise TelegramAPIError(f"Request returned {response.status} {response.reason}\n{error}") # Parse the json data as soon it's ready data = await response.json() + # Check for errors in the request + if "description" in data: + error = data["description"] + if response.status != 200: + raise TelegramAPIError(f"Request returned {response.status} {response.reason}\n{}") # Check for errors in the response if not data["ok"]: error = data["description"] @@ -230,7 +231,11 @@ class Chat: # TODO: This could give problems if a class inherits Bot if not isinstance(bot, Bot): raise TypeError("bot is not an instance of Bot.") - await bot.api_request("sendMessage", text=text, chat_id=self.chat_id, **params) + # Catch TelegramAPI exceptions + try: + await bot.api_request("sendMessage", text=text, chat_id=self.chat_id, **params) + except TelegramAPIError as e: + print(f"[Telegram] sendMessage failed: {e.text}") class User: