store-toxic-scores-3-days

This commit is contained in:
Untone 2024-09-26 16:28:29 +03:00
parent 6d1372fd7f
commit 43845713c2

View File

@ -9,8 +9,6 @@ from handlers.handle_private import handle_private
logger = logging.getLogger('handlers.messages_routing') logger = logging.getLogger('handlers.messages_routing')
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
latest_toxic = {}
async def messages_routing(msg, state): async def messages_routing(msg, state):
cid = msg["chat"]["id"] cid = msg["chat"]["id"]
uid = msg["from"]["id"] uid = msg["from"]["id"]
@ -33,24 +31,26 @@ async def messages_routing(msg, state):
elif bool(text): elif bool(text):
mid = msg.get("message_id") mid = msg.get("message_id")
if text == '/score@welcomecenter_bot': if text == '/toxic@welcomecenter_bot':
rmsg = reply_msg.get("message_id", latest_toxic[cid]) latest_toxic_message_id = await redis.get(f"toxic:{cid}")
await telegram_api( if latest_toxic_message_id:
"sendMessage", rmsg = reply_msg.get("message_id", latest_toxic_message_id)
chat_id=cid, await telegram_api(
reply_to_message_id=rmsg, "sendMessage",
text=f"{latest_toxic.get(f"{cid}:{rmsg}", 0)}% токсичности" chat_id=cid,
) reply_to_message_id=rmsg,
await telegram_api( text=f"{latest_toxic.get(f"{cid}:{rmsg}", 0)}% токсичности"
"deleteMessage", )
chat_id=cid, await telegram_api(
message_id=mid "deleteMessage",
) chat_id=cid,
message_id=mid
)
else: else:
toxic_score = detector(text) toxic_score = detector(text)
toxic_perc = math.floor(toxic_score*100) toxic_perc = math.floor(toxic_score*100)
latest_toxic[cid] = mid await redis.set(f"toxic:{cid}", mid)
latest_toxic[f"{cid}:{mid}"] = toxic_perc await redis.set(f"toxic:{cid}:{mid}", toxic_perc, ex=60*60*24*3)
logger.info(f'\ntext: {text}\ntoxic: {toxic_perc}%') logger.info(f'\ntext: {text}\ntoxic: {toxic_perc}%')
if toxic_score > 0.81: if toxic_score > 0.81:
if toxic_score > 0.90: if toxic_score > 0.90: