mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-02-16 10:21:57 +01:00
Merge branch 'master' of github.com:leandrotoledo/python-telegram-bot into boteventhandler
This commit is contained in:
commit
04050ca883
6 changed files with 46 additions and 28 deletions
|
@ -7,9 +7,11 @@ python:
|
||||||
- "pypy"
|
- "pypy"
|
||||||
- "pypy3"
|
- "pypy3"
|
||||||
install:
|
install:
|
||||||
- pip install coveralls
|
- pip install pylint flake8 coveralls
|
||||||
- pip install -r requirements.txt
|
- pip install -r requirements.txt
|
||||||
script:
|
script:
|
||||||
nosetests --with-coverage --cover-package telegram/
|
- nosetests --with-coverage --cover-package telegram/
|
||||||
|
- flake8 telegram
|
||||||
|
- 'if [[ $TRAVIS_PYTHON_VERSION != 2.6 ]]; then pylint -E telegram; fi'
|
||||||
after_success:
|
after_success:
|
||||||
coveralls
|
coveralls
|
||||||
|
|
|
@ -1,3 +1,9 @@
|
||||||
|
**2015-11-11**
|
||||||
|
|
||||||
|
*Released 2.9.2*
|
||||||
|
|
||||||
|
- Error handling on request timeouts has been improved
|
||||||
|
|
||||||
**2015-11-10**
|
**2015-11-10**
|
||||||
|
|
||||||
*Released 2.9.1*
|
*Released 2.9.1*
|
||||||
|
|
|
@ -60,7 +60,7 @@ author = u'Leandro Toledo'
|
||||||
# The short X.Y version.
|
# The short X.Y version.
|
||||||
version = '2.9'
|
version = '2.9'
|
||||||
# The full version, including alpha/beta/rc tags.
|
# The full version, including alpha/beta/rc tags.
|
||||||
release = '2.9.1'
|
release = '2.9.2'
|
||||||
|
|
||||||
# The language for content autogenerated by Sphinx. Refer to documentation
|
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||||
# for a list of supported languages.
|
# for a list of supported languages.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
# Simple Bot to reply Telegram messages
|
# Simple Bot to reply to Telegram messages
|
||||||
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
|
# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com>
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
@ -19,13 +20,16 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import telegram
|
import telegram
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
try:
|
||||||
LAST_UPDATE_ID = None
|
from urllib.error import URLError
|
||||||
|
except ImportError:
|
||||||
|
from urllib2 import URLError # python 2
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global LAST_UPDATE_ID
|
update_id = None
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||||
|
@ -33,33 +37,35 @@ def main():
|
||||||
# Telegram Bot Authorization Token
|
# Telegram Bot Authorization Token
|
||||||
bot = telegram.Bot('TOKEN')
|
bot = telegram.Bot('TOKEN')
|
||||||
|
|
||||||
# This will be our global variable to keep the latest update_id when requesting
|
|
||||||
# for updates. It starts with the latest update_id if available.
|
|
||||||
try:
|
|
||||||
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id
|
|
||||||
except IndexError:
|
|
||||||
LAST_UPDATE_ID = None
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
echo(bot)
|
try:
|
||||||
|
update_id = echo(bot, update_id)
|
||||||
|
except telegram.TelegramError as e:
|
||||||
|
# These are network problems with Telegram.
|
||||||
|
if e.message in ("Bad Gateway", "Timed out"):
|
||||||
|
sleep(1)
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
|
except URLError as e:
|
||||||
|
# These are network problems on our end.
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
|
|
||||||
def echo(bot):
|
def echo(bot, update_id):
|
||||||
global LAST_UPDATE_ID
|
|
||||||
|
|
||||||
# Request updates after the last updated_id
|
# Request updates after the last update_id
|
||||||
for update in bot.getUpdates(offset=LAST_UPDATE_ID, timeout=10):
|
for update in bot.getUpdates(offset=update_id, timeout=10):
|
||||||
# chat_id is required to reply any message
|
# chat_id is required to reply to any message
|
||||||
chat_id = update.message.chat_id
|
chat_id = update.message.chat_id
|
||||||
reply_text = update.message.text
|
update_id = update.update_id + 1
|
||||||
|
message = update.message.text
|
||||||
|
|
||||||
if reply_text:
|
if message:
|
||||||
# Reply the message
|
# Reply to the message
|
||||||
bot.sendMessage(chat_id=chat_id,
|
bot.sendMessage(chat_id=chat_id,
|
||||||
text=reply_text)
|
text=message)
|
||||||
|
|
||||||
# Updates global offset to get the new updates
|
return update_id
|
||||||
LAST_UPDATE_ID = update.update_id + 1
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -26,7 +26,7 @@ def requirements():
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='python-telegram-bot',
|
name='python-telegram-bot',
|
||||||
version='2.9.1',
|
version='2.9.2',
|
||||||
author='Leandro Toledo',
|
author='Leandro Toledo',
|
||||||
author_email='leandrotoledodesouza@gmail.com',
|
author_email='leandrotoledodesouza@gmail.com',
|
||||||
license='LGPLv3',
|
license='LGPLv3',
|
||||||
|
|
|
@ -110,7 +110,11 @@ def post(url,
|
||||||
if error.getcode() == 502:
|
if error.getcode() == 502:
|
||||||
raise TelegramError('Bad Gateway')
|
raise TelegramError('Bad Gateway')
|
||||||
|
|
||||||
message = _parse(error.read())
|
try:
|
||||||
|
message = _parse(error.read())
|
||||||
|
except ValueError:
|
||||||
|
message = 'Unknown HTTPError'
|
||||||
|
|
||||||
raise TelegramError(message)
|
raise TelegramError(message)
|
||||||
except (SSLError, socket.timeout) as error:
|
except (SSLError, socket.timeout) as error:
|
||||||
if "operation timed out" in str(error):
|
if "operation timed out" in str(error):
|
||||||
|
|
Loading…
Add table
Reference in a new issue