55import logging
66import sys
77import time
8+ from logging .handlers import RotatingFileHandler
89
910import telegram
1011from telegram import Update
@@ -20,6 +21,7 @@ class DeckardBot():
2021 def __init__ (self ):
2122 self .get_options ()
2223 self .set_logger ()
24+ self .verbose = False
2325 self .started_at = DateTime .now ()
2426
2527 def get_options (self ):
@@ -34,19 +36,28 @@ def get_options(self):
3436
3537 def set_logger (self ):
3638 self .logger = logging .getLogger ('bot' )
39+
40+ file_handler = RotatingFileHandler ('bot.log' , maxBytes = 1_000_000 , backupCount = 5 )
41+ console_handler = logging .NullHandler ()
42+ if self .verbose :
43+ console_handler = logging .StreamHandler ()
44+
3745 logging .basicConfig (
38- level = config .LOG_LEVEL ,
39- format = '%(asctime)s [%(name)s] %(levelname)s: %(message)s' ,
40- )
46+ level = logging .WARNING , # Pone el nivel de todos los logger a WARNING
47+ format = '%(asctime)s [%(name)s] %(levelname)s: %(message)s' ,
48+ handlers = [file_handler ,console_handler ],
49+ force = True
50+ )
51+
52+ # Ajustamos el nivel del logger bot
53+ self .logger .setLevel (config .LOG_LEVEL )
4154 config .log (self .logger .info )
4255
4356 def trace (self , msg ):
44- self .logger .info ('bot asked to execute /status commamd' )
45- if self .verbose :
46- print (msg )
57+ self .logger .info (msg )
4758
4859 async def command_status (self , update : Update , context : ContextTypes .DEFAULT_TYPE ):
49- self .trace ('bot asked to execute /status commamd ' )
60+ self .trace ('Received command: /status' )
5061 python_version = sys .version .split (maxsplit = 1 )[0 ]
5162 text = '\n ' .join ([
5263 config .BOT_GREETING ,
@@ -61,15 +72,15 @@ async def command_status(self, update: Update, context: ContextTypes.DEFAULT_TYP
6172 self .trace (text )
6273
6374 async def command_start (self , update : Update , context : ContextTypes .DEFAULT_TYPE ):
64- self .trace ('Received command /start' )
75+ self .trace ('Received command: /start' )
6576 await context .bot .send_message (
6677 chat_id = update .effective_chat .id ,
6778 text = config .BOT_GREETING ,
6879 parse_mode = ParseMode .HTML ,
6980 )
7081
7182 async def command_help (self , update : Update , context : ContextTypes .DEFAULT_TYPE ):
72- self .trace ('Received command /help' )
83+ self .trace ('Received command: /help' )
7384 await context .bot .send_message (
7485 chat_id = update .effective_chat .id ,
7586 text = (
@@ -83,7 +94,7 @@ async def command_help(self, update: Update, context: ContextTypes.DEFAULT_TYPE)
8394 )
8495
8596 async def command_zen (self , update : Update , context : ContextTypes .DEFAULT_TYPE ):
86- self .trace ('Received command /zen' )
97+ self .trace ('Received command: /zen' )
8798 text = '\n ' .join (config .THE_ZEN_OF_PYTHON )
8899 await context .bot .send_message (
89100 chat_id = update .effective_chat .id ,
@@ -102,7 +113,7 @@ async def welcome(self, update: Update, context):
102113 self .trace (f'Skipping welcome message, user { new_member .name } is no longer in the chat' )
103114 return
104115
105- self .trace (f'send welcome message for { new_member .name } ' )
116+ self .trace (f'Send welcome message for { new_member .name } ' )
106117 msg = None
107118
108119 if new_member .is_bot :
@@ -126,15 +137,15 @@ async def reply(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
126137 msg = update .message .text
127138 reply_spec = utils .triggers_reply (msg ) if msg else None
128139 if reply_spec is not None :
129- self .trace (f'bot sends reply { reply_spec .reply } ' )
140+ self .trace (f'Sending reply: { reply_spec .reply } ' )
130141 await update .message .reply_text (reply_spec .reply )
131142 context .bot .send_message (
132143 chat_id = update .message .chat_id ,
133144 text = reply_spec .reply
134145 )
135146
136147 def run (self ):
137- self .trace ('Starting bot... ' )
148+ self .trace ('Starting bot' )
138149 application = ApplicationBuilder ().token (config .TELEGRAM_BOT_TOKEN ).build ()
139150 start_handler = CommandHandler ('start' , self .command_start )
140151 application .add_handler (start_handler )
0 commit comments