2024-06-03 10:27:42 +00:00
|
|
|
|
import logging
|
|
|
|
|
import math
|
2024-09-27 06:23:55 +00:00
|
|
|
|
from state.redis import redis
|
|
|
|
|
from state.scan import get_average_pattern
|
2024-09-28 07:14:38 +00:00
|
|
|
|
from bot.api import telegram_api, download_file
|
2024-01-07 09:19:46 +00:00
|
|
|
|
from bot.config import FEEDBACK_CHAT_ID
|
|
|
|
|
from handlers.handle_private import handle_private
|
2024-09-28 08:51:24 +00:00
|
|
|
|
from nlp.segment_text import segment_text
|
2024-09-27 06:23:55 +00:00
|
|
|
|
from nlp.toxicity_detector import detector
|
2024-09-27 10:42:43 +00:00
|
|
|
|
from nlp.normalize import normalize
|
2024-09-28 07:06:04 +00:00
|
|
|
|
from nlp.ocr import ocr_recognize
|
2024-06-03 10:27:42 +00:00
|
|
|
|
|
2024-09-27 06:23:55 +00:00
|
|
|
|
logger = logging.getLogger("handlers.messages_routing")
|
2024-09-27 08:28:41 +00:00
|
|
|
|
|
2024-01-07 09:19:46 +00:00
|
|
|
|
|
|
|
|
|
async def messages_routing(msg, state):
|
|
|
|
|
cid = msg["chat"]["id"]
|
|
|
|
|
uid = msg["from"]["id"]
|
2024-09-28 07:06:04 +00:00
|
|
|
|
text = msg.get("caption", msg.get("text", ""))
|
2024-09-26 12:50:50 +00:00
|
|
|
|
reply_msg = msg.get("reply_to_message")
|
2024-01-07 09:19:46 +00:00
|
|
|
|
|
2024-09-28 08:51:24 +00:00
|
|
|
|
if str(cid) == FEEDBACK_CHAT_ID:
|
2024-01-07 09:19:46 +00:00
|
|
|
|
# сообщения из группы обратной связи
|
2024-09-26 17:07:55 +00:00
|
|
|
|
logger.info("feedback chat message: ", msg)
|
2024-01-07 09:19:46 +00:00
|
|
|
|
logger.debug(msg)
|
|
|
|
|
if reply_msg:
|
|
|
|
|
reply_chat_id = reply_msg.get("chat", {}).get("id")
|
2024-09-29 05:12:05 +00:00
|
|
|
|
if reply_chat_id != FEEDBACK_CHAT_ID.replace('-', '-100'):
|
2024-09-27 06:23:55 +00:00
|
|
|
|
await telegram_api(
|
|
|
|
|
"sendMessage",
|
|
|
|
|
chat_id=reply_chat_id,
|
|
|
|
|
text=text,
|
|
|
|
|
reply_to_message_id=reply_msg.get("message_id"),
|
|
|
|
|
)
|
2024-09-28 08:51:24 +00:00
|
|
|
|
return
|
2024-01-07 09:19:46 +00:00
|
|
|
|
|
2024-09-28 08:51:24 +00:00
|
|
|
|
elif cid == uid:
|
|
|
|
|
# сообщения в личке с ботом
|
|
|
|
|
logger.info("private chat message: ", msg)
|
|
|
|
|
await handle_private(msg, state)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
elif bool(text) or msg.get("photo"):
|
2024-09-26 10:07:01 +00:00
|
|
|
|
mid = msg.get("message_id")
|
2024-09-27 06:23:55 +00:00
|
|
|
|
if text == "/toxic@welcomecenter_bot":
|
2024-09-26 19:42:21 +00:00
|
|
|
|
# latest in chat
|
2024-09-26 15:48:41 +00:00
|
|
|
|
latest_toxic_message_id = await redis.get(f"toxic:{cid}")
|
2024-09-26 19:42:21 +00:00
|
|
|
|
|
|
|
|
|
# reply_to message_id
|
|
|
|
|
reply_to_msg_id = mid
|
2024-09-26 17:17:42 +00:00
|
|
|
|
if reply_msg:
|
2024-09-26 19:42:21 +00:00
|
|
|
|
reply_to_msg_id = reply_msg.get("message_id")
|
|
|
|
|
if not reply_to_msg_id and latest_toxic_message_id:
|
|
|
|
|
reply_to_msg_id = int(latest_toxic_message_id)
|
2024-09-27 10:51:55 +00:00
|
|
|
|
|
2024-09-27 11:14:51 +00:00
|
|
|
|
# count one msg toxicity
|
2024-09-27 10:51:55 +00:00
|
|
|
|
if reply_to_msg_id:
|
|
|
|
|
# count one message score
|
|
|
|
|
one_score = await redis.get(f"toxic:{cid}:{uid}:{reply_to_msg_id}")
|
|
|
|
|
reply_text = ""
|
|
|
|
|
if one_score:
|
|
|
|
|
logger.debug(one_score)
|
|
|
|
|
reply_text += f"{int(one_score)}% токсичности\n"
|
2024-09-27 10:34:44 +00:00
|
|
|
|
|
2024-09-27 11:14:51 +00:00
|
|
|
|
# count overall toxycity
|
|
|
|
|
try:
|
|
|
|
|
toxic_pattern = f"toxic:{cid}:{uid}:*"
|
|
|
|
|
toxic_score = await get_average_pattern(toxic_pattern)
|
|
|
|
|
|
|
|
|
|
if toxic_score:
|
|
|
|
|
emoji = (
|
|
|
|
|
"😳"
|
|
|
|
|
if toxic_score > 90
|
|
|
|
|
else "😟"
|
|
|
|
|
if toxic_score > 80
|
|
|
|
|
else "😏"
|
|
|
|
|
if toxic_score > 60
|
|
|
|
|
else "🙂"
|
|
|
|
|
if toxic_score > 20
|
|
|
|
|
else "😇"
|
|
|
|
|
)
|
|
|
|
|
reply_text += (
|
|
|
|
|
f"Средняя токсичность сообщений: {toxic_score}% {emoji}"
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
if reply_text:
|
|
|
|
|
await telegram_api(
|
|
|
|
|
"sendMessage",
|
|
|
|
|
chat_id=cid,
|
|
|
|
|
reply_to_message_id=reply_to_msg_id,
|
|
|
|
|
text=reply_text,
|
|
|
|
|
)
|
2024-09-27 10:13:23 +00:00
|
|
|
|
try:
|
|
|
|
|
await telegram_api("deleteMessage", chat_id=cid, message_id=mid)
|
2024-09-27 10:30:29 +00:00
|
|
|
|
except Exception:
|
2024-09-27 10:13:23 +00:00
|
|
|
|
pass
|
2024-09-27 06:23:55 +00:00
|
|
|
|
elif text == "/removed@welcomecenter_bot":
|
2024-09-27 10:13:23 +00:00
|
|
|
|
try:
|
|
|
|
|
await telegram_api("deleteMessage", chat_id=cid, message_id=mid)
|
2024-09-27 10:30:29 +00:00
|
|
|
|
except Exception:
|
2024-09-27 10:13:23 +00:00
|
|
|
|
pass
|
2024-09-26 12:38:14 +00:00
|
|
|
|
else:
|
2024-09-28 08:51:24 +00:00
|
|
|
|
# on screen recognition
|
|
|
|
|
for photo in msg.get("photo", []):
|
|
|
|
|
file_id = photo.get("file_id")
|
|
|
|
|
if file_id:
|
|
|
|
|
async for temp_file_path in download_file(file_id):
|
|
|
|
|
text += ocr_recognize(temp_file_path)
|
|
|
|
|
text += '\n'
|
|
|
|
|
|
2024-09-28 07:14:38 +00:00
|
|
|
|
normalized_text = normalize(text)
|
2024-09-28 08:51:24 +00:00
|
|
|
|
segmented_text = segment_text(normalized_text)
|
|
|
|
|
toxic_score = detector(segmented_text)
|
2024-09-27 06:23:55 +00:00
|
|
|
|
toxic_perc = math.floor(toxic_score * 100)
|
2024-09-28 07:14:38 +00:00
|
|
|
|
|
2024-09-29 05:23:28 +00:00
|
|
|
|
nospaces_text = text.replace(' ', '')
|
2024-09-29 05:36:26 +00:00
|
|
|
|
logger.info(f're-check this one: {nospaces_text}')
|
2024-09-29 05:23:28 +00:00
|
|
|
|
nospaces_text_score = detector(nospaces_text)
|
2024-09-29 05:29:11 +00:00
|
|
|
|
nospaces_text_perc = math.floor(toxic_score * 100)
|
2024-09-29 05:33:44 +00:00
|
|
|
|
logger.info(f'no spaces text toxic: {nospaces_text_perc}%')
|
|
|
|
|
if (nospaces_text != text and nospaces_text_score > toxic_score) or nospaces_text_perc > 95:
|
|
|
|
|
text_perc = nospaces_text_perc
|
2024-09-28 07:14:38 +00:00
|
|
|
|
|
2024-09-26 13:28:29 +00:00
|
|
|
|
await redis.set(f"toxic:{cid}", mid)
|
2024-09-27 06:23:55 +00:00
|
|
|
|
await redis.set(f"toxic:{cid}:{uid}:{mid}", toxic_perc, ex=60 * 60 * 24 * 3)
|
|
|
|
|
logger.info(f"\ntext: {text}\ntoxic: {toxic_perc}%")
|
2024-09-26 12:38:14 +00:00
|
|
|
|
if toxic_score > 0.81:
|
|
|
|
|
if toxic_score > 0.90:
|
|
|
|
|
await redis.set(f"removed:{uid}:{cid}:{mid}", text)
|
2024-09-27 10:13:23 +00:00
|
|
|
|
try:
|
|
|
|
|
await telegram_api("deleteMessage", chat_id=cid, message_id=mid)
|
2024-09-27 10:30:29 +00:00
|
|
|
|
except Exception:
|
2024-09-27 10:13:23 +00:00
|
|
|
|
pass
|
2024-09-26 12:38:14 +00:00
|
|
|
|
else:
|
|
|
|
|
await telegram_api(
|
|
|
|
|
"setMessageReaction",
|
|
|
|
|
chat_id=cid,
|
|
|
|
|
is_big=True,
|
|
|
|
|
message_id=mid,
|
2024-09-27 06:23:55 +00:00
|
|
|
|
reaction='[{"type":"emoji", "emoji":"🙉"}]',
|
2024-09-26 12:38:14 +00:00
|
|
|
|
)
|
2024-02-12 12:50:35 +00:00
|
|
|
|
|
2024-01-07 09:19:46 +00:00
|
|
|
|
else:
|
|
|
|
|
pass
|