Skip to content

Commit c3b3d51

Browse files
committed
Merge pull request python-telegram-bot#99 from rahiel/readme
improve echobot with cleaner code and basic exception handling
2 parents 1045150 + ff0d724 commit c3b3d51

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

examples/echobot.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
23
#
3-
# Simple Bot to reply Telegram messages
4+
# Simple Bot to reply to Telegram messages
45
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
56
#
67
# This program is free software: you can redistribute it and/or modify
@@ -19,47 +20,52 @@
1920

2021
import logging
2122
import telegram
23+
from time import sleep
2224

23-
24-
LAST_UPDATE_ID = None
25+
try:
26+
from urllib.error import URLError
27+
except ImportError:
28+
from urllib2 import URLError # python 2
2529

2630

2731
def main():
28-
global LAST_UPDATE_ID
32+
update_id = None
2933

3034
logging.basicConfig(
3135
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
3236

3337
# Telegram Bot Authorization Token
3438
bot = telegram.Bot('TOKEN')
3539

36-
# This will be our global variable to keep the latest update_id when requesting
37-
# for updates. It starts with the latest update_id if available.
38-
try:
39-
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id
40-
except IndexError:
41-
LAST_UPDATE_ID = None
42-
4340
while True:
44-
echo(bot)
45-
46-
47-
def echo(bot):
48-
global LAST_UPDATE_ID
49-
50-
# Request updates after the last updated_id
51-
for update in bot.getUpdates(offset=LAST_UPDATE_ID, timeout=10):
52-
# chat_id is required to reply any message
41+
try:
42+
update_id = echo(bot, update_id)
43+
except telegram.TelegramError as e:
44+
# These are network problems with Telegram.
45+
if e.message in ("Bad Gateway", "Timed out"):
46+
sleep(1)
47+
else:
48+
raise e
49+
except URLError as e:
50+
# These are network problems on our end.
51+
sleep(1)
52+
53+
54+
def echo(bot, update_id):
55+
56+
# Request updates after the last update_id
57+
for update in bot.getUpdates(offset=update_id, timeout=10):
58+
# chat_id is required to reply to any message
5359
chat_id = update.message.chat_id
54-
reply_text = update.message.text
60+
update_id = update.update_id + 1
61+
message = update.message.text
5562

56-
if reply_text:
57-
# Reply the message
63+
if message:
64+
# Reply to the message
5865
bot.sendMessage(chat_id=chat_id,
59-
text=reply_text)
66+
text=message)
6067

61-
# Updates global offset to get the new updates
62-
LAST_UPDATE_ID = update.update_id + 1
68+
return update_id
6369

6470

6571
if __name__ == '__main__':

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