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):
self.debug = os.environ.get('DEBUG') is not None
# Users to notify of violoations
self.notify_user_ids = (
list(map(int, os.environ['NOTIFY_USER_IDS'].split(',')))
if "NOTIFY_USER_IDS" in os.environ else [])
# Channel to notify of violoations, e.g. '@channelname'
self.notify_chat = os.environ['NOTIFY_CHAT']
# List of chat ids that bot should monitor
self.chat_ids = (
@ -75,15 +73,12 @@ class TelegramMonitorBot:
+ update.message.from_user.last_name)
if self.name_ban_re and self.name_ban_re.search(full_name):
# 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:
update.message.reply_text(log_message)
print(log_message)
for notify_user_id in self.notify_user_ids:
print (notify_user_id,"gets notified")
bot.send_message(
chat_id=notify_user_id,
text=log_message)
# Notify channel
bot.sendMessage(chat_id=self.notify_chat, text=log_message)
# Ban the user
self.ban_user(update)
# 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 ''):
# 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:
update.message.reply_text(log_message)
print(log_message)
for notify_user_id in self.notify_user_ids:
bot.send_message(
chat_id=notify_user_id,
text=log_message)
# Notify channel
bot.sendMessage(chat_id=self.notify_chat, text=log_message)
# Ban the user
self.ban_user(update)
# Log in database
@ -127,14 +120,12 @@ class TelegramMonitorBot:
if self.message_ban_re and self.message_ban_re.search(message):
# 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:
update.message.reply_text(log_message)
print(log_message)
for notify_user_id in self.notify_user_ids:
bot.send_message(
chat_id=notify_user_id,
text=log_message)
# Notify channel
bot.sendMessage(chat_id=self.notify_chat, text=log_message)
# Any message that causes a ban gets deleted
update.message.delete()
# Ban the user
@ -150,14 +141,12 @@ class TelegramMonitorBot:
elif self.message_hide_re and self.message_hide_re.search(message):
# 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:
update.message.reply_text(log_message)
print(log_message)
for notify_user_id in self.notify_user_ids:
bot.send_message(
chat_id=notify_user_id,
text=log_message)
# Notify channel
bot.sendMessage(chat_id=self.notify_chat, text=log_message)
# Delete the message
update.message.delete()
# Log in database
@ -206,13 +195,13 @@ class TelegramMonitorBot:
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)):
# Security checks
self.security_check_username(bot, update)
self.security_check_message(bot, update)
else:
print("Skipping checks. User is admin: {}".format(user.id))
print("👮‍♂️ Skipping checks. User is admin: {}".format(user.id))
except Exception as e:
print("Error: {}".format(e))