remove query_log.py, add ban and delete logging
This commit is contained in:
+9
-38
@@ -1,12 +1,9 @@
|
|||||||
import csv
|
|
||||||
import shelve
|
import shelve
|
||||||
from datetime import datetime as dt
|
from datetime import datetime as dt
|
||||||
from os.path import exists
|
import sys
|
||||||
from threading import Thread
|
|
||||||
from time import sleep
|
|
||||||
|
|
||||||
from flask import Flask, request, abort
|
from flask import Flask, request, abort
|
||||||
from telebot import types
|
from telebot import types
|
||||||
|
import logging
|
||||||
|
|
||||||
from . import reminder
|
from . import reminder
|
||||||
from . import trolling
|
from . import trolling
|
||||||
@@ -14,12 +11,11 @@ from . import admin
|
|||||||
from . import report
|
from . import report
|
||||||
from .config import bot, URL_RX, ALLOWED_WORDS, ADMIN_ID, TOKEN, PYTHONCHATRU
|
from .config import bot, URL_RX, ALLOWED_WORDS, ADMIN_ID, TOKEN, PYTHONCHATRU
|
||||||
from .me import get_me
|
from .me import get_me
|
||||||
from .query_log import logging
|
|
||||||
|
|
||||||
# https://core.telegram.org/bots/api Telegram Bot API
|
# https://core.telegram.org/bots/api Telegram Bot API
|
||||||
# https://github.com/eternnoir/pyTelegramBotAPI/tree/master/examples
|
# https://github.com/eternnoir/pyTelegramBotAPI/tree/master/examples
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO, format=' %(message)s')
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
print(">>> LutzBot is running! <<<")
|
print(">>> LutzBot is running! <<<")
|
||||||
@@ -36,11 +32,8 @@ zen_rows = ['Beautiful is better than ugly.', 'Explicit is better than implicit.
|
|||||||
'If the implementation is easy to explain, it may be a good idea.',
|
'If the implementation is easy to explain, it may be a good idea.',
|
||||||
"Namespaces are one honking great idea — let's do more of those!"]
|
"Namespaces are one honking great idea — let's do more of those!"]
|
||||||
|
|
||||||
|
if trolling:
|
||||||
def wait_for_readers(action, chat_id, msg_id):
|
print('trolling loaded')
|
||||||
""" Delete message after limited time. """
|
|
||||||
sleep(30)
|
|
||||||
action(chat_id, msg_id)
|
|
||||||
|
|
||||||
|
|
||||||
def check_spam_list(type_message: types.Message) -> bool:
|
def check_spam_list(type_message: types.Message) -> bool:
|
||||||
@@ -56,10 +49,7 @@ def check_spam_list(type_message: types.Message) -> bool:
|
|||||||
@bot.message_handler(func=check_spam_list)
|
@bot.message_handler(func=check_spam_list)
|
||||||
def moderate_messages(message: types.Message):
|
def moderate_messages(message: types.Message):
|
||||||
""" Ban user and delete their message. """
|
""" Ban user and delete their message. """
|
||||||
warn = bot.send_message(message.chat.id,
|
logging.warning(f'Banned: {message.from_user.first_name} - {message.text}')
|
||||||
f'👮🏼 <a href="tg://user?id={message.from_user.id}">{message.from_user.first_name}</a>'
|
|
||||||
f' заблокирован (запрещённые ссылки).', parse_mode='HTML')
|
|
||||||
Thread(target=wait_for_readers, args=(bot.delete_message, message.chat.id, warn.id)).start()
|
|
||||||
bot.delete_message(message.chat.id, message.id)
|
bot.delete_message(message.chat.id, message.id)
|
||||||
bot.ban_chat_member(message.chat.id, message.from_user.id)
|
bot.ban_chat_member(message.chat.id, message.from_user.id)
|
||||||
if message.chat.id == PYTHONCHATRU:
|
if message.chat.id == PYTHONCHATRU:
|
||||||
@@ -78,11 +68,9 @@ def check_caption_spam_list(type_message: types.Message) -> bool:
|
|||||||
@bot.message_handler(func=check_caption_spam_list, content_types=['video'])
|
@bot.message_handler(func=check_caption_spam_list, content_types=['video'])
|
||||||
def catch_videos(message: types.Message):
|
def catch_videos(message: types.Message):
|
||||||
"""Catch offensive videos"""
|
"""Catch offensive videos"""
|
||||||
warn = bot.send_message(message.chat.id,
|
logging.warning(f'Banned: {message.from_user.first_name} - {message.video.file_name}')
|
||||||
f'👮🏼 <a href="tg://user?id={message.from_user.id}">{message.from_user.first_name}</a> заблокирован (наркотики).')
|
|
||||||
bot.delete_message(message.chat.id, message.id)
|
bot.delete_message(message.chat.id, message.id)
|
||||||
bot.ban_chat_member(message.chat.id, message.from_user.id)
|
bot.ban_chat_member(message.chat.id, message.from_user.id)
|
||||||
Thread(target=wait_for_readers, args=(bot.delete_message, message.chat.id, warn.id)).start()
|
|
||||||
with shelve.open('chat_stats') as chat_stats:
|
with shelve.open('chat_stats') as chat_stats:
|
||||||
chat_stats['Banned'] += 1
|
chat_stats['Banned'] += 1
|
||||||
|
|
||||||
@@ -97,10 +85,12 @@ def check_no_allowed(word_list, msg):
|
|||||||
def check_delete_list(type_message: types.Message) -> bool:
|
def check_delete_list(type_message: types.Message) -> bool:
|
||||||
""" Check for URLs in message and delete. """
|
""" Check for URLs in message and delete. """
|
||||||
if URL_RX.search(type_message.text) and check_no_allowed(ALLOWED_WORDS, type_message.text):
|
if URL_RX.search(type_message.text) and check_no_allowed(ALLOWED_WORDS, type_message.text):
|
||||||
|
logging.info(f'Deleting: {type_message.text}')
|
||||||
return True
|
return True
|
||||||
if type_message.entities:
|
if type_message.entities:
|
||||||
for entity in type_message.entities:
|
for entity in type_message.entities:
|
||||||
if entity.url and check_no_allowed(ALLOWED_WORDS, entity.url):
|
if entity.url and check_no_allowed(ALLOWED_WORDS, entity.url):
|
||||||
|
logging.info(f'Deleting: {entity.url}')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -122,7 +112,6 @@ def send_lutz_command(message):
|
|||||||
parse_mode='HTML',
|
parse_mode='HTML',
|
||||||
disable_notification=True,
|
disable_notification=True,
|
||||||
)
|
)
|
||||||
logging(message)
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['faq'])
|
@bot.message_handler(commands=['faq'])
|
||||||
@@ -133,7 +122,6 @@ def send_lutz_command(message):
|
|||||||
parse_mode='HTML',
|
parse_mode='HTML',
|
||||||
disable_notification=True,
|
disable_notification=True,
|
||||||
)
|
)
|
||||||
logging(message)
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['lutz'])
|
@bot.message_handler(commands=['lutz'])
|
||||||
@@ -146,7 +134,6 @@ def send_lutz_command(message):
|
|||||||
├ by Mark Lutz
|
├ by Mark Lutz
|
||||||
└ Released June 2013""",
|
└ Released June 2013""",
|
||||||
parse_mode='HTML')
|
parse_mode='HTML')
|
||||||
logging(message)
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['lib', 'library', 'book', 'books'])
|
@bot.message_handler(commands=['lib', 'library', 'book', 'books'])
|
||||||
@@ -157,21 +144,6 @@ def send_lutz_command(message):
|
|||||||
parse_mode='HTML',
|
parse_mode='HTML',
|
||||||
disable_notification=True,
|
disable_notification=True,
|
||||||
)
|
)
|
||||||
logging(message)
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['log'])
|
|
||||||
def send_log(message):
|
|
||||||
""" Send the last log rows. """
|
|
||||||
if message.from_user.id == ADMIN_ID:
|
|
||||||
file_path = f"logs/log_{dt.now().strftime('%Y-%m')}.csv"
|
|
||||||
if exists(file_path):
|
|
||||||
with open(file_path, "r", newline='', encoding='utf-8') as f:
|
|
||||||
all_rows = list(csv.reader(f))
|
|
||||||
text = ''
|
|
||||||
for row in all_rows[-min(len(all_rows), 5):]:
|
|
||||||
text += str(row) + '\n'
|
|
||||||
bot.send_message(ADMIN_ID, f'<code>{text}</code>', parse_mode='html')
|
|
||||||
|
|
||||||
|
|
||||||
@bot.inline_handler(lambda query: True)
|
@bot.inline_handler(lambda query: True)
|
||||||
@@ -192,7 +164,6 @@ def default_query(inline_query):
|
|||||||
def command_me(message):
|
def command_me(message):
|
||||||
""" Send info about user and chat id [Service]. """
|
""" Send info about user and chat id [Service]. """
|
||||||
bot.send_message(message.chat.id, get_me(message), parse_mode='HTML')
|
bot.send_message(message.chat.id, get_me(message), parse_mode='HTML')
|
||||||
logging(message)
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['remind'])
|
@bot.message_handler(commands=['remind'])
|
||||||
|
|||||||
+2
-3
@@ -10,8 +10,7 @@ bot = TeleBot(TOKEN)
|
|||||||
|
|
||||||
ADMIN_ID = 280887861 # Rykov7
|
ADMIN_ID = 280887861 # Rykov7
|
||||||
PYTHONCHATRU = -1001338616632 # pythonchatru
|
PYTHONCHATRU = -1001338616632 # pythonchatru
|
||||||
TEST_CHAT = -1001622893830
|
|
||||||
|
|
||||||
URL_RX = re.compile(r'\w+\.\w+/\w+')
|
URL_RX = re.compile(r'\w+\.\w+/\w+')
|
||||||
ALLOWED_WORDS = ['pastebin', 'github', 'google', 'nometa', 'python',
|
ALLOWED_WORDS = ['pastebin', 'github', 'google', 'nometa', 'python', 'django', 'flask', 'fastapi',
|
||||||
'stackoverflow', 'habr', 'medium', 'youtube']
|
'stackoverflow', 'habr', 'medium', 'youtube', 'stepik', 'telegraph']
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
""" Logging in file. """
|
|
||||||
|
|
||||||
import csv
|
|
||||||
from os.path import exists
|
|
||||||
import datetime as dt
|
|
||||||
|
|
||||||
|
|
||||||
def logging(message):
|
|
||||||
log_file_csv = f'logs/log_{dt.datetime.now().strftime("%Y-%m")}.csv'
|
|
||||||
if not exists(log_file_csv):
|
|
||||||
with open(log_file_csv, 'a') as f:
|
|
||||||
logs = csv.writer(f)
|
|
||||||
logs.writerow(['DATE AND TIME', 'ID', 'FIRST NAME', 'USERNAME',
|
|
||||||
'CHAT', 'LANG', 'PREMIUM', 'QUERY'])
|
|
||||||
|
|
||||||
with open(log_file_csv, 'a') as f:
|
|
||||||
logs = csv.writer(f)
|
|
||||||
logs.writerow(
|
|
||||||
[dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message.from_user.id,
|
|
||||||
message.from_user.first_name, message.from_user.username,
|
|
||||||
message.chat.id, message.from_user.language_code, message.from_user.is_premium, message.text])
|
|
||||||
Reference in New Issue
Block a user