telegram-moderator/bot.py

306 lines
10 KiB
Python
Raw Normal View History

2018-01-28 18:29:58 -05:00
# -*- coding: utf-8 -*-
"""Group Chat Logger
This bot is a modified version of the echo2 bot found here:
2018-01-28 18:29:58 -05:00
https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot2.py
This bot logs all messages sent in a Telegram Group to a database.
"""
2018-01-30 18:01:31 -05:00
from __future__ import print_function
import sys
2018-01-28 18:29:58 -05:00
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import os
2018-02-09 20:13:09 -05:00
from model import User, Message, MessageHide, UserBan, session
2018-01-30 18:01:31 -05:00
from time import strftime
2018-02-01 16:54:27 -05:00
import re
2018-02-01 18:04:18 -05:00
import unidecode
2018-02-22 18:38:02 -05:00
from mwt import MWT
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
class TelegramMonitorBot:
2018-02-01 16:54:27 -05:00
2018-02-09 20:24:09 -05:00
2018-02-01 21:35:56 -05:00
def __init__(self):
2018-02-01 23:29:58 -05:00
self.debug = os.environ.get('DEBUG') is not None
# Users to notify of violoations
2018-02-22 19:48:59 -05:00
self.notify_user_ids = (
list(map(int, os.environ['NOTIFY_USER_IDS'].split(',')))
if "NOTIFY_USER_IDS" in os.environ else [])
2018-02-01 23:02:19 -05:00
# List of chat ids that bot should monitor
2018-02-22 19:48:59 -05:00
self.chat_ids = (
list(map(int, os.environ['CHAT_IDS'].split(',')))
if "CHAT_IDS" in os.environ else [])
# Regex for message patterns that cause user ban
2018-02-01 21:35:56 -05:00
self.message_ban_patterns = os.environ['MESSAGE_BAN_PATTERNS']
2018-02-09 20:24:09 -05:00
self.message_ban_re = (re.compile(
2018-02-09 20:13:09 -05:00
self.message_ban_patterns,
re.IGNORECASE | re.VERBOSE)
2018-02-09 20:24:09 -05:00
if self.message_ban_patterns else None)
2018-02-01 16:54:27 -05:00
# Regex for message patterns that cause message to be hidden
2018-02-01 21:35:56 -05:00
self.message_hide_patterns = os.environ['MESSAGE_HIDE_PATTERNS']
2018-02-09 20:24:09 -05:00
self.message_hide_re = (re.compile(
2018-02-09 20:13:09 -05:00
self.message_hide_patterns,
re.IGNORECASE | re.VERBOSE)
2018-02-09 20:24:09 -05:00
if self.message_hide_patterns else None)
2018-02-01 18:04:18 -05:00
# Regex for name patterns that cause user to be banned
2018-02-01 23:02:19 -05:00
self.name_ban_patterns = os.environ['NAME_BAN_PATTERNS']
2018-02-09 20:24:09 -05:00
self.name_ban_re = (re.compile(
2018-02-09 20:13:09 -05:00
self.name_ban_patterns,
re.IGNORECASE | re.VERBOSE)
2018-02-09 20:24:09 -05:00
if self.name_ban_patterns else None)
2018-02-01 23:02:19 -05:00
2018-02-22 18:38:02 -05:00
@MWT(timeout=60*60)
def get_admin_ids(self, bot, chat_id):
""" Returns a list of admin IDs for a given chat. Results are cached for 1 hour. """
2018-02-22 18:38:02 -05:00
return [admin.user.id for admin in bot.get_chat_administrators(chat_id)]
2018-02-01 23:02:19 -05:00
def ban_user(self, update):
2018-02-05 18:19:44 -05:00
""" Ban user """
2018-02-02 14:06:44 -05:00
kick_success = update.message.chat.kick_member(update.message.from_user.id)
2018-02-01 23:02:19 -05:00
2018-02-01 23:29:58 -05:00
2018-02-01 22:11:15 -05:00
def security_check_username(self, bot, update):
2018-02-01 23:02:19 -05:00
""" Test username for security violations """
full_name = (update.message.from_user.first_name + " "
+ update.message.from_user.last_name)
2018-02-09 20:13:09 -05:00
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'))
2018-02-01 23:29:58 -05:00
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)
# Ban the user
2018-02-01 23:02:19 -05:00
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()
2018-02-01 23:02:19 -05:00
2018-02-09 20:13:09 -05:00
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'))
2018-02-01 23:29:58 -05:00
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)
# Ban the user
2018-02-01 23:02:19 -05:00
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()
2018-02-01 18:04:18 -05:00
2018-02-09 20:13:09 -05:00
2018-02-01 21:35:56 -05:00
def security_check_message(self, bot, update):
""" Test message for security violations """
2018-02-01 16:54:27 -05:00
2018-02-01 21:35:56 -05:00
# Remove accents from letters (é->e, ñ->n, etc...)
message = unidecode.unidecode(update.message.text)
# TODO: Replace lookalike unicode characters:
# https://github.com/wanderingstan/Confusables
2018-02-01 16:54:27 -05:00
2018-02-09 20:13:09 -05:00
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'))
2018-02-01 23:29:58 -05:00
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)
2018-02-06 13:29:46 -05:00
# Any message that causes a ban gets deleted
update.message.delete()
# Ban the user
self.ban_user(update)
2018-02-09 20:13:09 -05:00
# 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()
elif self.message_hide_re and self.message_hide_re.search(message):
# Logging
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)
# 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()
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
def logger(self, bot, update):
""" Primary Logger. Handles incoming bot messages and saves them to DB """
2018-02-01 22:11:15 -05:00
try:
user = update.message.from_user
# Limit bot to monitoring certain chats
if update.message.chat_id not in self.chat_ids:
print("Message from user {} is from chat_id not being monitored: {}".format(
user.id,
update.message.chat_id)
)
return
2018-02-09 20:24:09 -05:00
2018-02-01 22:11:15 -05:00
if self.id_exists(user.id):
self.log_message(user.id, update.message.text)
2018-02-01 21:35:56 -05:00
else:
2018-02-09 20:13:09 -05:00
add_user_success = self.add_user(
user.id,
user.first_name,
user.last_name,
user.username)
2018-02-01 22:11:15 -05:00
if add_user_success:
self.log_message(user.id, update.message.text)
print("User added: {}".format(user.id))
else:
print("Something went wrong adding the user {}".format(user.id), file=sys.stderr)
if update.message.text:
print("{} {} ({}) : {}".format(
strftime("%Y-%m-%dT%H:%M:%S"),
user.id,
(user.username or (user.first_name + " " + user.last_name) or "").encode('utf-8'),
update.message.text.encode('utf-8'))
)
2018-02-01 21:35:56 -05:00
if (self.debug or
update.message.from_user.id not in self.get_admin_ids(bot, update.message.chat_id)):
2018-02-01 23:10:09 -05:00
# Security checks
self.security_check_username(bot, update)
self.security_check_message(bot, update)
2018-02-22 18:38:02 -05:00
else:
print("Skipping checks. User is admin: {}".format(user.id))
2018-02-01 23:02:19 -05:00
2018-02-01 21:35:56 -05:00
except Exception as e:
2018-02-09 20:24:09 -05:00
print("Error: {}".format(e))
2018-02-01 21:35:56 -05:00
# DB queries
def id_exists(self, id_value):
s = session()
bool_set = False
for id1 in s.query(User.id).filter_by(id=id_value):
if id1:
bool_set = True
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
s.close()
2018-02-01 16:54:27 -05:00
2018-02-01 21:35:56 -05:00
return bool_set
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
def log_message(self, user_id, user_message):
try:
s = session()
2018-02-09 20:13:09 -05:00
msg1 = Message(user_id=user_id, message=user_message)
2018-02-01 21:35:56 -05:00
s.add(msg1)
s.commit()
s.close()
except Exception as e:
2018-02-09 20:24:09 -05:00
print("Error: {}".format(e))
2018-02-01 21:35:56 -05:00
def add_user(self, user_id, first_name, last_name, username):
try:
s = session()
user = User(
id=user_id,
first_name=first_name,
last_name=last_name,
username=username)
s.add(user)
s.commit()
s.close()
return self.id_exists(user_id)
2018-02-01 21:35:56 -05:00
except Exception as e:
2018-02-09 20:24:09 -05:00
print("Error: {}".format(e))
2018-02-01 21:35:56 -05:00
def error(self, bot, update, error):
""" Log Errors caused by Updates. """
2018-02-01 21:35:56 -05:00
print("Update '{}' caused error '{}'".format(update, error),
file=sys.stderr)
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
def start(self):
""" Start the bot. """
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
# Create the EventHandler and pass it your bot's token.
updater = Updater(os.environ["TELEGRAM_BOT_TOKEN"])
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
# Get the dispatcher to register handlers
dp = updater.dispatcher
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
# on different commands - answer in Telegram
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
# on noncommand i.e message - echo the message on Telegram
2018-02-01 22:11:15 -05:00
dp.add_handler(MessageHandler(
Filters.text,
lambda bot, update : self.logger(bot, update)
))
2018-02-01 21:35:56 -05:00
# dp.add_handler(MessageHandler(Filters.status_update, status))
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
# log all errors
2018-02-01 22:11:15 -05:00
dp.add_error_handler(
lambda bot, update, error : self.error(bot, update, error)
)
2018-01-28 18:29:58 -05:00
2018-02-01 21:35:56 -05:00
# Start the Bot
updater.start_polling()
2018-01-28 18:29:58 -05:00
print("Bot started. Montitoring chats: {}".format(self.chat_ids))
2018-01-29 00:41:12 -05:00
2018-02-01 21:35:56 -05:00
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
2018-01-28 18:29:58 -05:00
if __name__ == '__main__':
2018-02-01 21:35:56 -05:00
c = TelegramMonitorBot()
c.start()