mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-22 07:06:26 +01:00
echobot: simplify exception handling
This commit is contained in:
parent
d02e656700
commit
3e978277b3
1 changed files with 5 additions and 16 deletions
|
@ -6,13 +6,9 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import telegram
|
import telegram
|
||||||
|
from telegram.error import NetworkError, Unauthorized
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
try:
|
|
||||||
from urllib.error import URLError
|
|
||||||
except ImportError:
|
|
||||||
from urllib2 import URLError # python 2
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Telegram Bot Authorization Token
|
# Telegram Bot Authorization Token
|
||||||
|
@ -31,18 +27,11 @@ def main():
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
update_id = echo(bot, update_id)
|
update_id = echo(bot, update_id)
|
||||||
except telegram.TelegramError as e:
|
except NetworkError:
|
||||||
# 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.
|
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
except Unauthorized:
|
||||||
|
# The user has removed or blocked the bot.
|
||||||
|
update_id += 1
|
||||||
|
|
||||||
|
|
||||||
def echo(bot, update_id):
|
def echo(bot, update_id):
|
||||||
|
|
Loading…
Reference in a new issue