From a0e3922bed0db81d81fbb7423d4ddca1e535f832 Mon Sep 17 00:00:00 2001 From: Stan James Date: Thu, 28 Jun 2018 10:45:17 -0600 Subject: [PATCH 1/5] Added better logging output. --- bot.py | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/bot.py b/bot.py index e8b1dea..8dfa176 100644 --- a/bot.py +++ b/bot.py @@ -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)) From b2460d8e3a5f9a7ce1d3c21511f5915fef8d78e0 Mon Sep 17 00:00:00 2001 From: Stan James Date: Thu, 28 Jun 2018 11:22:23 -0600 Subject: [PATCH 2/5] better handling of debug setting --- bot.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index 8dfa176..5506b06 100644 --- a/bot.py +++ b/bot.py @@ -23,7 +23,9 @@ class TelegramMonitorBot: def __init__(self): - self.debug = os.environ.get('DEBUG') is not None + self.debug = ( + (os.environ.get('DEBUG') is not None) && + (os.environ.get('DEBUG').upper() != "false")) # Channel to notify of violoations, e.g. '@channelname' self.notify_chat = os.environ['NOTIFY_CHAT'] @@ -78,7 +80,7 @@ class TelegramMonitorBot: update.message.reply_text(log_message) print(log_message) # Notify channel - bot.sendMessage(chat_id=self.notify_chat, text=log_message) + bot.sendMessage(chat_id=self.notify_chat, text="🤖:" + log_message) # Ban the user self.ban_user(update) # Log in database From 7874dbfbfe09dec3fc49e4e0331de31755878d68 Mon Sep 17 00:00:00 2001 From: Stan James Date: Thu, 28 Jun 2018 11:23:34 -0600 Subject: [PATCH 3/5] syntax fix --- bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 5506b06..99014e5 100644 --- a/bot.py +++ b/bot.py @@ -24,7 +24,7 @@ class TelegramMonitorBot: def __init__(self): self.debug = ( - (os.environ.get('DEBUG') is not None) && + (os.environ.get('DEBUG') is not None) and (os.environ.get('DEBUG').upper() != "false")) # Channel to notify of violoations, e.g. '@channelname' From 68aa8b85b85088be19b92593c43959be85730731 Mon Sep 17 00:00:00 2001 From: Stan James Date: Thu, 28 Jun 2018 11:54:17 -0600 Subject: [PATCH 4/5] Move notification chat to end of function ...So if notification channel is not set up right, it doesn't stop blocking from happening. --- bot.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index 99014e5..6932eb3 100644 --- a/bot.py +++ b/bot.py @@ -79,8 +79,6 @@ class TelegramMonitorBot: if self.debug: update.message.reply_text(log_message) print(log_message) - # Notify channel - bot.sendMessage(chat_id=self.notify_chat, text="🤖:" + log_message) # Ban the user self.ban_user(update) # Log in database @@ -91,6 +89,8 @@ class TelegramMonitorBot: s.add(userBan) s.commit() s.close() + # Notify channel + bot.sendMessage(chat_id=self.notify_chat, text=log_message) if self.name_ban_re and self.name_ban_re.search(update.message.from_user.username or ''): # Logging @@ -98,8 +98,6 @@ class TelegramMonitorBot: if self.debug: update.message.reply_text(log_message) print(log_message) - # Notify channel - bot.sendMessage(chat_id=self.notify_chat, text=log_message) # Ban the user self.ban_user(update) # Log in database @@ -110,6 +108,8 @@ class TelegramMonitorBot: s.add(userBan) s.commit() s.close() + # Notify channel + bot.sendMessage(chat_id=self.notify_chat, text=log_message) def security_check_message(self, bot, update): @@ -126,8 +126,6 @@ class TelegramMonitorBot: if self.debug: update.message.reply_text(log_message) print(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 @@ -140,6 +138,8 @@ class TelegramMonitorBot: s.add(userBan) s.commit() s.close() + # Notify channel + bot.sendMessage(chat_id=self.notify_chat, text=log_message) elif self.message_hide_re and self.message_hide_re.search(message): # Logging @@ -147,8 +147,6 @@ class TelegramMonitorBot: if self.debug: update.message.reply_text(log_message) print(log_message) - # Notify channel - bot.sendMessage(chat_id=self.notify_chat, text=log_message) # Delete the message update.message.delete() # Log in database @@ -159,6 +157,8 @@ class TelegramMonitorBot: s.add(messageHide) s.commit() s.close() + # Notify channel + bot.sendMessage(chat_id=self.notify_chat, text=log_message) def logger(self, bot, update): From e1a29f06a97b75794a3af3a072d85d882e400042 Mon Sep 17 00:00:00 2001 From: Stan James Date: Tue, 16 Oct 2018 17:54:47 +0200 Subject: [PATCH 5/5] Changes for mainnet. Hide forwarded messages. --- .gitignore | 3 +++ bot.py | 31 ++++++++++++++++++++++++++++++- env_sample.sh | 7 ++++++- model.py | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 74d14ea..04473bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Never commit config data config.cnf +# Env vars for "real" installation +env.sh + # Byte-compiled / optimized / DLL files *.py[cod] diff --git a/bot.py b/bot.py index 6932eb3..c96a24e 100644 --- a/bot.py +++ b/bot.py @@ -27,8 +27,17 @@ class TelegramMonitorBot: (os.environ.get('DEBUG') is not None) and (os.environ.get('DEBUG').upper() != "false")) + if (self.debug): + print("🔵 DEBUG:", os.environ["DEBUG"]) + print("🔵 TELEGRAM_BOT_POSTGRES_URL:", os.environ["TELEGRAM_BOT_POSTGRES_URL"]) + print("🔵 TELEGRAM_BOT_TOKEN:", os.environ["TELEGRAM_BOT_TOKEN"]) + print("🔵 NOTIFY_CHAT:", os.environ['NOTIFY_CHAT'] if 'NOTIFY_CHAT' in os.environ else "") + print("🔵 MESSAGE_BAN_PATTERNS:\n", os.environ['MESSAGE_BAN_PATTERNS']) + print("🔵 MESSAGE_HIDE_PATTERNS:\n", os.environ['MESSAGE_HIDE_PATTERNS']) + print("🔵 NAME_BAN_PATTERNS:\n", os.environ['NAME_BAN_PATTERNS']) + # Channel to notify of violoations, e.g. '@channelname' - self.notify_chat = os.environ['NOTIFY_CHAT'] + self.notify_chat = os.environ['NOTIFY_CHAT'] if 'NOTIFY_CHAT' in os.environ else None # List of chat ids that bot should monitor self.chat_ids = ( @@ -120,6 +129,26 @@ class TelegramMonitorBot: # TODO: Replace lookalike unicode characters: # https://github.com/wanderingstan/Confusables + # Hide forwarded messages + if update.message.forward_date is not None: + # Logging + log_message = "❌ HIDE FORWARDED: {}".format(update.message.text.encode('utf-8')) + if self.debug: + update.message.reply_text(log_message) + print(log_message) + # Delete the message + 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() + # Notify channel + bot.sendMessage(chat_id=self.notify_chat, text=log_message) + if self.message_ban_re and self.message_ban_re.search(message): # Logging log_message = "❌ 🙅‍♂️ BAN MATCH: {}".format(update.message.text.encode('utf-8')) diff --git a/env_sample.sh b/env_sample.sh index 83917ed..3718794 100644 --- a/env_sample.sh +++ b/env_sample.sh @@ -1,6 +1,7 @@ # Example env vars for bot +# Copy this to `env.sh` and edit with your real vars -- it is ignored by git -export TELEGRAM_BOT_POSTGRES_URL="postgresql://postgres:postgres@localhost/origindb" +export TELEGRAM_BOT_POSTGRES_URL="postgresql://localhost/postgres" read -r -d '' MESSAGE_BAN_PATTERNS << 'EOF' # ETH @@ -25,3 +26,7 @@ export TELEGRAM_BOT_TOKEN="XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" export NAME_BAN_PATTERNS="admin$" export CHAT_IDS="-250531994" + +# Needed to make these env vars visible to python +export MESSAGE_BAN_PATTERNS=$MESSAGE_BAN_PATTERNS +export MESSAGE_HIDE_PATTERNS=$MESSAGE_HIDE_PATTERNS diff --git a/model.py b/model.py index 7bbed81..46f4909 100644 --- a/model.py +++ b/model.py @@ -3,6 +3,7 @@ from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declarative_base import os +# Localhost url: postgresql://localhost/postgres postgres_url = os.environ["TELEGRAM_BOT_POSTGRES_URL"]