Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions pydeckard/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@ def set_logger(self):
handlers=[console_handler],
force=True,
)
# Ajustamos el nivel del logger bot
# Ajustamos el nivel del logger pydeckard
self.logger.setLevel(config.LOG_LEVEL)
config.log(self.logger.info)

def trace(self, msg):
self.logger.info(msg)

async def command_status(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
self.trace('Received command: /status')
self.logger.info('Received command: /status')
python_version = sys.version.split(maxsplit=1)[0]
text = '\n'.join([
config.BOT_GREETING,
Expand All @@ -67,18 +64,18 @@ async def command_status(self, update: Update, context: ContextTypes.DEFAULT_TYP
text=text,
parse_mode=ParseMode.HTML,
)
self.trace(text)
self.logger.info(text)

async def command_start(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
self.trace('Received command: /start')
self.logger.info('Received command: /start')
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=config.BOT_GREETING,
parse_mode=ParseMode.HTML,
)

async def command_help(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
self.trace('Received command: /help')
self.logger.info('Received command: /help')
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=(
Expand All @@ -93,7 +90,7 @@ async def command_help(self, update: Update, context: ContextTypes.DEFAULT_TYPE)
)

async def command_zen(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
self.trace('Received command: /zen')
self.logger.info('Received command: /zen')
text = '\n'.join(config.THE_ZEN_OF_PYTHON)
await context.bot.send_message(
chat_id=update.effective_chat.id,
Expand All @@ -102,7 +99,7 @@ async def command_zen(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
)

async def command_config(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
self.trace('Received command: /config')
self.logger.info('Received command: /config')
buff = [
f'Probabilidad de responder: {config.VERBOSITY:.2f}',
'Disparadores:',
Expand All @@ -118,17 +115,17 @@ async def command_config(self, update: Update, context: ContextTypes.DEFAULT_TYP
)

async def welcome(self, update: Update, context):
self.trace('Received new user event')
self.logger.info('Received new user event')
new_member = update.message.new_chat_members[0]

self.trace(f'Waiting {config.WELCOME_DELAY} seconds until user completes captcha...')
self.logger.info('Waiting %s seconds until user completes captcha...', config.WELCOME_DELAY)
time.sleep(config.WELCOME_DELAY)
membership_info = await context.bot.get_chat_member(update.message.chat_id, new_member.id)
if membership_info['status'] == 'left':
self.trace(f'Skipping welcome message, user {new_member.name} is no longer in the chat')
self.logger.info('Skipping welcome message, user %s is no longer in the chat', new_member.name)
return

self.trace(f'Send welcome message for {new_member.name}')
self.logger.info('Send welcome message for %s', new_member.name)
msg = None

if new_member.is_bot:
Expand All @@ -154,11 +151,11 @@ async def reply(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
msg = update.message.text
reply_spec = utils.triggers_reply(msg) if msg else None
if reply_spec is not None:
self.trace(f'Sending reply: {reply_spec.reply}')
self.logger.info('Sending reply: %s', reply_spec.reply)
await update.message.reply_text(reply_spec.reply)

def run(self):
self.trace('Starting bot')
self.logger.info('Starting bot')
application = ApplicationBuilder().token(config.TELEGRAM_BOT_TOKEN).build()
start_handler = CommandHandler('start', self.command_start)
application.add_handler(start_handler)
Expand All @@ -183,7 +180,7 @@ def run(self):
self.reply,
)
application.add_handler(reply_handler)
self.trace('Bot is ready')
self.logger.info('Bot is ready')
application.run_polling(poll_interval=config.POLL_INTERVAL)


Expand Down
Loading