Added better logging output.

This commit is contained in:
Stan James 2018-06-28 10:45:17 -06:00
parent 4f49ecc3ef
commit a0e3922bed

43
bot.py
View File

@ -25,10 +25,8 @@ class TelegramMonitorBot:
def __init__(self): def __init__(self):
self.debug = os.environ.get('DEBUG') is not None self.debug = os.environ.get('DEBUG') is not None
# Users to notify of violoations # Channel to notify of violoations, e.g. '@channelname'
self.notify_user_ids = ( self.notify_chat = os.environ['NOTIFY_CHAT']
list(map(int, os.environ['NOTIFY_USER_IDS'].split(',')))
if "NOTIFY_USER_IDS" in os.environ else [])
# List of chat ids that bot should monitor # List of chat ids that bot should monitor
self.chat_ids = ( self.chat_ids = (
@ -75,15 +73,12 @@ class TelegramMonitorBot:
+ update.message.from_user.last_name) + update.message.from_user.last_name)
if self.name_ban_re and self.name_ban_re.search(full_name): if self.name_ban_re and self.name_ban_re.search(full_name):
# Logging # Logging
log_message = "Ban match full name: {}".format(full_name.encode('utf-8')) log_message = "❌ 🙅‍♂️ BAN MATCH FULL NAME: {}".format(full_name.encode('utf-8'))
if self.debug: if self.debug:
update.message.reply_text(log_message) update.message.reply_text(log_message)
print(log_message) print(log_message)
for notify_user_id in self.notify_user_ids: # Notify channel
print (notify_user_id,"gets notified") bot.sendMessage(chat_id=self.notify_chat, text=log_message)
bot.send_message(
chat_id=notify_user_id,
text=log_message)
# Ban the user # Ban the user
self.ban_user(update) self.ban_user(update)
# Log in database # Log in database
@ -97,14 +92,12 @@ class TelegramMonitorBot:
if self.name_ban_re and self.name_ban_re.search(update.message.from_user.username or ''): if self.name_ban_re and self.name_ban_re.search(update.message.from_user.username or ''):
# Logging # Logging
log_message = "Ban match username: {}".format(update.message.from_user.username.encode('utf-8')) log_message = "❌ 🙅‍♂️ BAN MATCH USERNAME: {}".format(update.message.from_user.username.encode('utf-8'))
if self.debug: if self.debug:
update.message.reply_text(log_message) update.message.reply_text(log_message)
print(log_message) print(log_message)
for notify_user_id in self.notify_user_ids: # Notify channel
bot.send_message( bot.sendMessage(chat_id=self.notify_chat, text=log_message)
chat_id=notify_user_id,
text=log_message)
# Ban the user # Ban the user
self.ban_user(update) self.ban_user(update)
# Log in database # Log in database
@ -127,14 +120,12 @@ class TelegramMonitorBot:
if self.message_ban_re and self.message_ban_re.search(message): if self.message_ban_re and self.message_ban_re.search(message):
# Logging # Logging
log_message = "Ban message match: {}".format(update.message.text.encode('utf-8')) log_message = "❌ 🙅‍♂️ BAN MATCH: {}".format(update.message.text.encode('utf-8'))
if self.debug: if self.debug:
update.message.reply_text(log_message) update.message.reply_text(log_message)
print(log_message) print(log_message)
for notify_user_id in self.notify_user_ids: # Notify channel
bot.send_message( bot.sendMessage(chat_id=self.notify_chat, text=log_message)
chat_id=notify_user_id,
text=log_message)
# Any message that causes a ban gets deleted # Any message that causes a ban gets deleted
update.message.delete() update.message.delete()
# Ban the user # Ban the user
@ -150,14 +141,12 @@ class TelegramMonitorBot:
elif self.message_hide_re and self.message_hide_re.search(message): elif self.message_hide_re and self.message_hide_re.search(message):
# Logging # Logging
log_message = "Hide match: {}".format(update.message.text.encode('utf-8')) log_message = "❌ 🙈 HIDE MATCH: {}".format(update.message.text.encode('utf-8'))
if self.debug: if self.debug:
update.message.reply_text(log_message) update.message.reply_text(log_message)
print(log_message) print(log_message)
for notify_user_id in self.notify_user_ids: # Notify channel
bot.send_message( bot.sendMessage(chat_id=self.notify_chat, text=log_message)
chat_id=notify_user_id,
text=log_message)
# Delete the message # Delete the message
update.message.delete() update.message.delete()
# Log in database # Log in database
@ -206,13 +195,13 @@ class TelegramMonitorBot:
update.message.text.encode('utf-8')) update.message.text.encode('utf-8'))
) )
if (self.debug or if (True or
update.message.from_user.id not in self.get_admin_ids(bot, update.message.chat_id)): update.message.from_user.id not in self.get_admin_ids(bot, update.message.chat_id)):
# Security checks # Security checks
self.security_check_username(bot, update) self.security_check_username(bot, update)
self.security_check_message(bot, update) self.security_check_message(bot, update)
else: else:
print("Skipping checks. User is admin: {}".format(user.id)) print("👮‍♂️ Skipping checks. User is admin: {}".format(user.id))
except Exception as e: except Exception as e:
print("Error: {}".format(e)) print("Error: {}".format(e))