Ban and hide now getting logged in db
This commit is contained in:
parent
1430126c44
commit
ca7826d88e
56
bot.py
56
bot.py
@ -13,7 +13,7 @@ from __future__ import print_function
|
|||||||
import sys
|
import sys
|
||||||
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
||||||
import os
|
import os
|
||||||
from model import User, Message, session
|
from model import User, Message, MessageHide, UserBan, session
|
||||||
from time import strftime
|
from time import strftime
|
||||||
import re
|
import re
|
||||||
import unidecode
|
import unidecode
|
||||||
@ -27,19 +27,27 @@ class TelegramMonitorBot:
|
|||||||
self.safe_user_ids = map(int, os.environ['SAFE_USER_IDS'].split(','))
|
self.safe_user_ids = map(int, os.environ['SAFE_USER_IDS'].split(','))
|
||||||
|
|
||||||
self.message_ban_patterns = os.environ['MESSAGE_BAN_PATTERNS']
|
self.message_ban_patterns = os.environ['MESSAGE_BAN_PATTERNS']
|
||||||
self.message_ban_re = re.compile(self.message_ban_patterns, re.IGNORECASE | re.VERBOSE)
|
self.message_ban_re = re.compile(
|
||||||
|
self.message_ban_patterns,
|
||||||
|
re.IGNORECASE | re.VERBOSE)
|
||||||
|
if self.message_ban_patterns else None
|
||||||
|
|
||||||
self.message_hide_patterns = os.environ['MESSAGE_HIDE_PATTERNS']
|
self.message_hide_patterns = os.environ['MESSAGE_HIDE_PATTERNS']
|
||||||
self.message_hide_re = re.compile(self.message_hide_patterns, re.IGNORECASE | re.VERBOSE)
|
self.message_hide_re = re.compile(
|
||||||
|
self.message_hide_patterns,
|
||||||
|
re.IGNORECASE | re.VERBOSE)
|
||||||
|
if self.message_hide_patterns else None
|
||||||
|
|
||||||
self.name_ban_patterns = os.environ['NAME_BAN_PATTERNS']
|
self.name_ban_patterns = os.environ['NAME_BAN_PATTERNS']
|
||||||
self.name_ban_re = re.compile(self.name_ban_patterns, re.IGNORECASE | re.VERBOSE)
|
self.name_ban_re = re.compile(
|
||||||
|
self.name_ban_patterns,
|
||||||
|
re.IGNORECASE | re.VERBOSE)
|
||||||
|
if self.name_ban_patterns else None
|
||||||
|
|
||||||
|
|
||||||
def ban_user(self, update):
|
def ban_user(self, update):
|
||||||
""" Ban user """
|
""" Ban user """
|
||||||
kick_success = update.message.chat.kick_member(update.message.from_user.id)
|
kick_success = update.message.chat.kick_member(update.message.from_user.id)
|
||||||
# print ("Not banning in testing")
|
|
||||||
|
|
||||||
|
|
||||||
def security_check_username(self, bot, update):
|
def security_check_username(self, bot, update):
|
||||||
@ -47,40 +55,60 @@ class TelegramMonitorBot:
|
|||||||
|
|
||||||
full_name = (update.message.from_user.first_name + " "
|
full_name = (update.message.from_user.first_name + " "
|
||||||
+ update.message.from_user.last_name)
|
+ update.message.from_user.last_name)
|
||||||
if self.name_ban_re.search(full_name):
|
if self.name_ban_re and self.name_ban_re.search(full_name):
|
||||||
# Ban the user
|
# Ban the user
|
||||||
if self.debug:
|
if self.debug:
|
||||||
update.message.reply_text("DEBUG: Ban match full name: {}".format(full_name.encode('utf-8')))
|
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')))
|
print("Ban match full name: {}".format(full_name.encode('utf-8')))
|
||||||
self.ban_user(update)
|
self.ban_user(update)
|
||||||
|
|
||||||
if 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 ''):
|
||||||
# Ban the user
|
# Ban the user
|
||||||
if self.debug:
|
if self.debug:
|
||||||
update.message.reply_text("DEBUG: Ban match username: {}".format(update.message.from_user.username.encode('utf-8')))
|
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')))
|
print("Ban match username: {}".format(update.message.from_user.username.encode('utf-8')))
|
||||||
self.ban_user(update)
|
self.ban_user(update)
|
||||||
|
|
||||||
|
|
||||||
def security_check_message(self, bot, update):
|
def security_check_message(self, bot, update):
|
||||||
""" Test message for security violations """
|
""" Test message for security violations """
|
||||||
|
|
||||||
# Remove accents from letters (é->e, ñ->n, etc...)
|
# Remove accents from letters (é->e, ñ->n, etc...)
|
||||||
message = unidecode.unidecode(update.message.text)
|
message = unidecode.unidecode(update.message.text)
|
||||||
if self.message_hide_re.search(message):
|
# TODO: Replace lookalike unicode characters
|
||||||
|
|
||||||
|
if self.message_hide_re and self.message_hide_re.search(message):
|
||||||
# Delete the message
|
# Delete the message
|
||||||
if self.debug:
|
if self.debug:
|
||||||
update.message.reply_text("DEBUG: Hide match: {}".format(update.message.text.encode('utf-8')))
|
update.message.reply_text("DEBUG: Hide match: {}".format(update.message.text.encode('utf-8')))
|
||||||
print("Hide match: {}".format(update.message.text.encode('utf-8')))
|
print("Hide match: {}".format(update.message.text.encode('utf-8')))
|
||||||
update.message.delete()
|
update.message.delete()
|
||||||
|
# Log in database
|
||||||
|
s = session()
|
||||||
|
messageHide = MessageHide(
|
||||||
|
user_id=update.message.from_user.id,
|
||||||
|
message=update.message.text)
|
||||||
|
s.add(messageHide)
|
||||||
|
s.commit()
|
||||||
|
s.close()
|
||||||
|
|
||||||
if self.message_ban_re.search(message):
|
if self.message_ban_re and self.message_ban_re.search(message):
|
||||||
# Ban the user
|
# Ban the user
|
||||||
if self.debug:
|
if self.debug:
|
||||||
update.message.reply_text("DEBUG: Ban message match: {}".format(update.message.text.encode('utf-8')))
|
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')))
|
print("Ban message match: {}".format(update.message.text.encode('utf-8')))
|
||||||
|
# Ban the user
|
||||||
self.ban_user(update)
|
self.ban_user(update)
|
||||||
# Any message that causes a ban gets deleted
|
# Any message that causes a ban gets deleted
|
||||||
update.message.delete()
|
update.message.delete()
|
||||||
|
# Log in database
|
||||||
|
s = session()
|
||||||
|
userBan = UserBan(
|
||||||
|
user_id=update.message.from_user.id,
|
||||||
|
reason=update.message.text)
|
||||||
|
s.add(userBan)
|
||||||
|
s.commit()
|
||||||
|
s.close()
|
||||||
|
|
||||||
|
|
||||||
def logger(self, bot, update):
|
def logger(self, bot, update):
|
||||||
@ -91,7 +119,11 @@ class TelegramMonitorBot:
|
|||||||
if self.id_exists(user.id):
|
if self.id_exists(user.id):
|
||||||
self.log_message(user.id, update.message.text)
|
self.log_message(user.id, update.message.text)
|
||||||
else:
|
else:
|
||||||
add_user_success = self.add_user(user.id, user.first_name, user.last_name, user.username)
|
add_user_success = self.add_user(
|
||||||
|
user.id,
|
||||||
|
user.first_name,
|
||||||
|
user.last_name,
|
||||||
|
user.username)
|
||||||
|
|
||||||
if add_user_success:
|
if add_user_success:
|
||||||
self.log_message(user.id, update.message.text)
|
self.log_message(user.id, update.message.text)
|
||||||
@ -130,14 +162,12 @@ class TelegramMonitorBot:
|
|||||||
|
|
||||||
|
|
||||||
def log_message(self, user_id, user_message):
|
def log_message(self, user_id, user_message):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
s = session()
|
s = session()
|
||||||
msg1 = Message(user_id=user_id,message=user_message)
|
msg1 = Message(user_id=user_id, message=user_message)
|
||||||
s.add(msg1)
|
s.add(msg1)
|
||||||
s.commit()
|
s.commit()
|
||||||
s.close()
|
s.close()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
23
model.py
23
model.py
@ -26,17 +26,34 @@ class Message(Base):
|
|||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
user_id = Column(Integer, ForeignKey('telegram_users.id'), nullable=False)
|
user_id = Column(Integer, ForeignKey('telegram_users.id'), nullable=False)
|
||||||
message = Column(String)
|
message = Column(String)
|
||||||
# Use default=func.now() to set the default hiring time
|
time = Column(DateTime, default=func.now())
|
||||||
# of an Employee to be the current time when an
|
|
||||||
# Employee record was created
|
|
||||||
|
class MessageHide(Base):
|
||||||
|
__tablename__ = 'telegram_message_hides'
|
||||||
|
id = Column(Integer, primary_key=True)
|
||||||
|
user_id = Column(Integer, ForeignKey('telegram_users.id'), nullable=False)
|
||||||
|
message = Column(String)
|
||||||
|
time = Column(DateTime, default=func.now())
|
||||||
|
|
||||||
|
|
||||||
|
class UserBan(Base):
|
||||||
|
__tablename__ = 'telegram_user_bans'
|
||||||
|
id = Column(Integer, primary_key=True)
|
||||||
|
user_id = Column(Integer, ForeignKey('telegram_users.id'), nullable=False)
|
||||||
|
reason = Column(String)
|
||||||
time = Column(DateTime, default=func.now())
|
time = Column(DateTime, default=func.now())
|
||||||
|
|
||||||
|
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
engine = create_engine(postgres_url)
|
engine = create_engine(postgres_url)
|
||||||
|
|
||||||
|
print (engine)
|
||||||
|
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
session = sessionmaker()
|
session = sessionmaker()
|
||||||
session.configure(bind=engine)
|
session.configure(bind=engine)
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
|
|
||||||
|
|
||||||
print ("Created database model")
|
print ("Created database model")
|
||||||
|
Loading…
Reference in New Issue
Block a user