Merge pull request #22 from wjt/logging

Don't call logging.basicConfig() in library code
This commit is contained in:
Leandro Toledo 2015-08-08 18:59:38 -03:00
commit a91c158825
4 changed files with 31 additions and 16 deletions

View file

@ -2,21 +2,36 @@
'''Simple Bot to reply Telegram messages''' '''Simple Bot to reply Telegram messages'''
import logging
import telegram import telegram
import time import time
# Telegram Bot Authorization Token
bot = telegram.Bot('TOKEN')
# This will be our global variable to keep the latest update_id when requesting LAST_UPDATE_ID = None
# 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
def echo(): def main():
global LAST_UPDATE_ID
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Telegram Bot Authorization 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:
echo(bot)
time.sleep(3)
def echo(bot):
global LAST_UPDATE_ID global LAST_UPDATE_ID
# Request updates from last updated_id # Request updates from last updated_id
@ -36,6 +51,4 @@ def echo():
if __name__ == '__main__': if __name__ == '__main__':
while True: main()
echo()
time.sleep(3)

View file

@ -5,14 +5,16 @@
__author__ = 'leandrotoledodesouza@gmail.com' __author__ = 'leandrotoledodesouza@gmail.com'
import logging
import telegram import telegram
import urllib import urllib
def main(): def main():
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
bot = telegram.Bot('TOKEN') # Telegram Bot Authorization Token bot = telegram.Bot('TOKEN') # Telegram Bot Authorization Token
global LAST_UPDATE_ID
LAST_UPDATE_ID = bot.getUpdates()[-1].update_id # Get lastest update LAST_UPDATE_ID = bot.getUpdates()[-1].update_id # Get lastest update
while True: while True:

View file

@ -18,9 +18,6 @@ import logging
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError, from telegram import (User, Message, Update, UserProfilePhotos, TelegramError,
ReplyMarkup, InputFile, TelegramObject) ReplyMarkup, InputFile, TelegramObject)
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
class Bot(TelegramObject): class Bot(TelegramObject):

View file

@ -1,6 +1,9 @@
import logging
import unittest import unittest
from tests.test_bot import BotTest from tests.test_bot import BotTest
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
testsuite = unittest.TestLoader().loadTestsFromTestCase(BotTest) testsuite = unittest.TestLoader().loadTestsFromTestCase(BotTest)
unittest.TextTestRunner(verbosity=1).run(testsuite) unittest.TextTestRunner(verbosity=1).run(testsuite)