PM offending user and selected users on moderation action.

This commit is contained in:
Stan James 2018-02-10 22:24:36 -07:00
parent e812f33d9e
commit 0dc4a82c4c

91
bot.py
View File

@ -25,7 +25,13 @@ class TelegramMonitorBot:
def __init__(self):
self.debug = os.environ.get('DEBUG') is not None
self.safe_user_ids = map(int, os.environ['SAFE_USER_IDS'].split(','))
self.safe_user_ids = list(
map(int, os.environ['SAFE_USER_IDS'].split(','))
if "SAFE_USER_IDS" in os.environ else [])
self.notify_user_ids = list(
map(int, os.environ['NOTIFY_USER_IDS'].split(','))
if "NOTIFY_USER_IDS" in os.environ else [])
self.message_ban_patterns = os.environ['MESSAGE_BAN_PATTERNS']
self.message_ban_re = (re.compile(
@ -45,6 +51,9 @@ class TelegramMonitorBot:
re.IGNORECASE | re.VERBOSE)
if self.name_ban_patterns else None)
self.ban_feedback_message = "You have been banned from this group for posting prohibited content."
self.ban_name_feedback_message = "You have been banned from this group for having a misleading username."
self.hide_feedback_message = "Your last message was deleted for having prohibited content."
def ban_user(self, update):
""" Ban user """
@ -57,18 +66,55 @@ class TelegramMonitorBot:
full_name = (update.message.from_user.first_name + " "
+ update.message.from_user.last_name)
if self.name_ban_re and self.name_ban_re.search(full_name):
# Ban the user
# Logging
log_message = "Ban match full name: {}".format(full_name.encode('utf-8'))
if self.debug:
update.message.reply_text("DEBUG: Ban match full name: {}".format(full_name.encode('utf-8')))
print("Ban match full name: {}".format(full_name.encode('utf-8')))
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)
# Ban the user
self.ban_user(update)
# Log in database
s = session()
userBan = UserBan(
user_id=update.message.from_user.id,
reason=log_message)
s.add(userBan)
s.commit()
s.close()
# Send the user feedback
bot.send_message(
chat_id=update.message.from_user.id,
text=self.ban_name_feedback_message)
if self.name_ban_re and self.name_ban_re.search(update.message.from_user.username or ''):
# Ban the user
# Logging
log_message = "Ban match username: {}".format(update.message.from_user.username.encode('utf-8'))
if self.debug:
update.message.reply_text("DEBUG: Ban match username: {}".format(update.message.from_user.username.encode('utf-8')))
print("Ban match username: {}".format(update.message.from_user.username.encode('utf-8')))
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)
# Ban the user
self.ban_user(update)
# Log in database
s = session()
userBan = UserBan(
user_id=update.message.from_user.id,
reason=log_message)
s.add(userBan)
s.commit()
s.close()
# Send the user feedback
bot.send_message(
chat_id=update.message.from_user.id,
text=self.ban_name_feedback_message)
def security_check_message(self, bot, update):
@ -80,10 +126,15 @@ class TelegramMonitorBot:
# https://github.com/wanderingstan/Confusables
if self.message_ban_re and self.message_ban_re.search(message):
# Ban the user
# Logging
log_message = "Ban message match: {}".format(update.message.text.encode('utf-8'))
if self.debug:
update.message.reply_text("DEBUG: Ban message match: {}".format(update.message.text.encode('utf-8')))
print("Ban message match: {}".format(update.message.text.encode('utf-8')))
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)
# Any message that causes a ban gets deleted
update.message.delete()
# Ban the user
@ -96,12 +147,22 @@ class TelegramMonitorBot:
s.add(userBan)
s.commit()
s.close()
# Send the user feedback
bot.send_message(
chat_id=update.message.from_user.id,
text=self.ban_feedback_message)
elif self.message_hide_re and self.message_hide_re.search(message):
# Delete the message
# Logging
log_message = "Hide match: {}".format(update.message.text.encode('utf-8'))
if self.debug:
update.message.reply_text("DEBUG: Hide match: {}".format(update.message.text.encode('utf-8')))
print("Hide match: {}".format(update.message.text.encode('utf-8')))
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)
# Delete the message
update.message.delete()
# Log in database
s = session()
@ -111,6 +172,10 @@ class TelegramMonitorBot:
s.add(messageHide)
s.commit()
s.close()
# Send the user feedback
bot.send_message(
chat_id=update.message.from_user.id,
text=self.hide_feedback_message)
def logger(self, bot, update):