welcomecenterbot/handlers/messages_routing.py
2024-09-26 16:12:05 +03:00

74 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
import math
from store import redis
from bot.api import telegram_api
from bot.config import FEEDBACK_CHAT_ID
from nlp.toxicity_detector import detector
from handlers.handle_private import handle_private
logger = logging.getLogger('handlers.messages_routing')
logging.basicConfig(level=logging.DEBUG)
latest_toxic = {}
async def messages_routing(msg, state):
cid = msg["chat"]["id"]
uid = msg["from"]["id"]
text = msg.get("text")
reply_msg = msg.get("reply_to_message")
if cid == uid:
# сообщения в личке с ботом
logger.info("private chat message")
await handle_private(msg, state)
elif str(cid) == FEEDBACK_CHAT_ID:
# сообщения из группы обратной связи
logger.info("feedback chat message")
logger.debug(msg)
if reply_msg:
reply_chat_id = reply_msg.get("chat", {}).get("id")
if reply_chat_id != FEEDBACK_CHAT_ID:
await telegram_api("sendMessage", chat_id=reply_chat_id, text=text, reply_to_message_id=reply_msg.get("message_id"))
elif bool(text):
mid = msg.get("message_id")
if reply_msg and text == '/score@welcomecenter_bot':
rmsg = reply_msg.get("message_id")
await telegram_api(
"sendMessage",
chat_id=cid,
reply_to_message_id=rmsg,
text=f"{latest_toxic.get(f"{cid}:{rmsg}", 0)}% токсичности"
)
await telegram_api(
"deleteMessage",
chat_id=cid,
message_id=mid
)
else:
toxic_score = detector(text)
toxic_perc = math.floor(toxic_score*100)
latest_toxic[cid] = toxic_perc
latest_toxic[f"{cid}:{mid}"] = toxic_perc
logger.info(f'\ntext: {text}\ntoxic: {toxic_perc}%')
if toxic_score > 0.81:
if toxic_score > 0.90:
await redis.set(f"removed:{uid}:{cid}:{mid}", text)
await telegram_api(
"deleteMessage",
chat_id=cid,
message_id=mid
)
else:
await telegram_api(
"setMessageReaction",
chat_id=cid,
is_big=True,
message_id=mid,
reaction=f'[{{"type":"emoji", "emoji":"🙉"}}]'
)
else:
pass