mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-21 22:56:38 +01:00
Adding echobot example and fix IOError typo
This commit is contained in:
parent
32c8f10652
commit
582bb61e5b
2 changed files with 41 additions and 1 deletions
38
examples/echobot.py
Normal file
38
examples/echobot.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
'''Simple Bot to reply Telegram messages'''
|
||||||
|
|
||||||
|
import telegram
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Telegram Bot Authorization Token
|
||||||
|
bot = telegram.Bot('120405045:AAEAQ3EcfZ3NztkUbOkMOwCxXdDikEW1VZE')
|
||||||
|
|
||||||
|
# This will be our global variable to keep the latest update_id when requesting
|
||||||
|
# for updates. It starts with the latest update_id available.
|
||||||
|
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id
|
||||||
|
|
||||||
|
|
||||||
|
def echo():
|
||||||
|
global LAST_UPDATE_ID
|
||||||
|
|
||||||
|
# Request updates from last updated_id
|
||||||
|
for update in bot.getUpdates(offset=LAST_UPDATE_ID):
|
||||||
|
if LAST_UPDATE_ID < update.update_id:
|
||||||
|
# chat_id is required to reply any message
|
||||||
|
chat_id = update.message.chat_id
|
||||||
|
message = update.message.text
|
||||||
|
|
||||||
|
if (message):
|
||||||
|
# Reply the message
|
||||||
|
bot.sendMessage(chat_id=chat_id,
|
||||||
|
text=message)
|
||||||
|
|
||||||
|
# Updates global offset to get the new updates
|
||||||
|
LAST_UPDATE_ID = update.update_id
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
while True:
|
||||||
|
echo()
|
||||||
|
time.sleep(5)
|
|
@ -589,7 +589,9 @@ class Bot(object):
|
||||||
url,
|
url,
|
||||||
urllib.urlencode(data)
|
urllib.urlencode(data)
|
||||||
).read()
|
).read()
|
||||||
except urllib.IOError as e:
|
except IOError as e:
|
||||||
|
raise TelegramError(str(e))
|
||||||
|
except urllib2.HTTPError as e:
|
||||||
raise TelegramError(str(e))
|
raise TelegramError(str(e))
|
||||||
except urllib2.URLError as e:
|
except urllib2.URLError as e:
|
||||||
raise TelegramError(str(e))
|
raise TelegramError(str(e))
|
||||||
|
|
Loading…
Reference in a new issue