Merge pull request #162 from rahiel/master

legacy echobot: simpler exception handling
This commit is contained in:
Noam Meltzer 2016-02-10 10:44:09 +02:00
commit 9d77a3b503
2 changed files with 6 additions and 17 deletions

View file

@ -6,13 +6,9 @@
import logging
import telegram
from telegram.error import NetworkError, Unauthorized
from time import sleep
try:
from urllib.error import URLError
except ImportError:
from urllib2 import URLError # python 2
def main():
# Telegram Bot Authorization Token
@ -31,18 +27,11 @@ def main():
while True:
try:
update_id = echo(bot, update_id)
except telegram.TelegramError as e:
# These are network problems with Telegram.
if e.message in ("Bad Gateway", "Timed out"):
sleep(1)
elif e.message == "Unauthorized":
# The user has removed or blocked the bot.
update_id += 1
else:
raise e
except URLError as e:
# These are network problems on our end.
except NetworkError:
sleep(1)
except Unauthorized:
# The user has removed or blocked the bot.
update_id += 1
def echo(bot, update_id):

View file

@ -84,7 +84,7 @@ def _try_except_req(func):
if errcode in (401, 403):
raise Unauthorized()
if errcode == 502:
raise TelegramError('Bad Gateway')
raise NetworkError('Bad Gateway')
try:
message = _parse(error.read())