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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy