Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e97827

Browse files
committed
echobot: simplify exception handling
1 parent d02e656 commit 3e97827

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

examples/legacy/echobot.py

+5-16
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66

77
import logging
88
import telegram
9+
from telegram.error import NetworkError, Unauthorized
910
from time import sleep
1011

11-
try:
12-
from urllib.error import URLError
13-
except ImportError:
14-
from urllib2 import URLError # python 2
15-
1612

1713
def main():
1814
# Telegram Bot Authorization Token
@@ -31,18 +27,11 @@ def main():
3127
while True:
3228
try:
3329
update_id = echo(bot, update_id)
34-
except telegram.TelegramError as e:
35-
# These are network problems with Telegram.
36-
if e.message in ("Bad Gateway", "Timed out"):
37-
sleep(1)
38-
elif e.message == "Unauthorized":
39-
# The user has removed or blocked the bot.
40-
update_id += 1
41-
else:
42-
raise e
43-
except URLError as e:
44-
# These are network problems on our end.
30+
except NetworkError:
4531
sleep(1)
32+
except Unauthorized:
33+
# The user has removed or blocked the bot.
34+
update_id += 1
4635

4736

4837
def echo(bot, update_id):

0 commit comments

Comments
 (0)