X Tutup
Skip to content

Commit 62b79df

Browse files
committed
handle "Unauthorized" exceptions in echobot
1 parent 70de2bd commit 62b79df

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

examples/echobot.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,29 @@
2929

3030

3131
def main():
32-
update_id = None
32+
# Telegram Bot Authorization Token
33+
bot = telegram.Bot('TOKEN')
34+
35+
# get the first pending update_id, this is so we can skip over it in case
36+
# we get an "Unauthorized" exception.
37+
try:
38+
update_id = bot.getUpdates()[0].update_id
39+
except IndexError:
40+
update_id = None
3341

3442
logging.basicConfig(
3543
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
3644

37-
# Telegram Bot Authorization Token
38-
bot = telegram.Bot('TOKEN')
39-
4045
while True:
4146
try:
4247
update_id = echo(bot, update_id)
4348
except telegram.TelegramError as e:
4449
# These are network problems with Telegram.
4550
if e.message in ("Bad Gateway", "Timed out"):
4651
sleep(1)
52+
elif e.message == "Unauthorized":
53+
# The user has removed or blocked the bot.
54+
update_id += 1
4755
else:
4856
raise e
4957
except URLError as e:

0 commit comments

Comments
 (0)
X Tutup