46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
from bot.api import telegram_api
|
||
from bot.config import FEEDBACK_CHAT_ID
|
||
from nlp.toxicity import text2toxicity
|
||
from nlp.replying import get_toxic_reply
|
||
import logging
|
||
|
||
from handlers.handle_private import handle_private
|
||
logger = logging.getLogger(__name__)
|
||
logging.basicConfig(level=logging.INFO)
|
||
|
||
|
||
async def messages_routing(msg, state):
|
||
cid = msg["chat"]["id"]
|
||
uid = msg["from"]["id"]
|
||
text = msg.get("text")
|
||
|
||
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)
|
||
reply_msg = msg.get("reply_to_message")
|
||
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=reply_msg.get("message_id"))
|
||
|
||
elif bool(text):
|
||
toxic_score = text2toxicity(text)
|
||
if toxic_score > 0.71:
|
||
toxic_reply = get_toxic_reply(toxic_score)
|
||
await telegram_api(
|
||
"setMessageReaction",
|
||
chat_id=cid,
|
||
is_big=True,
|
||
message_id=msg.get("message_id"),
|
||
reaction=f'[{{"type":"emoji", "emoji":"{toxic_reply}"}}]'
|
||
)
|
||
|
||
else:
|
||
pass
|