routing-fix
This commit is contained in:
parent
d82225f9cd
commit
f76f4fffca
|
@ -1,4 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
BOT_TOKEN = os.environ.get("BOT_TOKEN") or ""
|
BOT_TOKEN = os.environ.get("BOT_TOKEN") or ""
|
||||||
|
REDIS_URL = os.environ.get("REDIS_URL") or ""
|
||||||
FEEDBACK_CHAT_ID = os.environ.get("FEEDBACK_CHAT_ID", "").replace("-", "-100")
|
FEEDBACK_CHAT_ID = os.environ.get("FEEDBACK_CHAT_ID", "").replace("-", "-100")
|
||||||
|
|
|
@ -2,6 +2,7 @@ from bot.config import FEEDBACK_CHAT_ID
|
||||||
from bot.announce import edit_announce
|
from bot.announce import edit_announce
|
||||||
from bot.api import telegram_api
|
from bot.api import telegram_api
|
||||||
import logging
|
import logging
|
||||||
|
from store import get_all_removed
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
@ -11,15 +12,20 @@ start_message = {
|
||||||
'ru': "Доброе утро! Можешь напечатать здесь любое сообщение для передачи в чат"
|
'ru': "Доброе утро! Можешь напечатать здесь любое сообщение для передачи в чат"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def handle_private(msg, state):
|
async def handle_private(msg, state):
|
||||||
text = msg.get("text")
|
text = msg.get("text")
|
||||||
sender = msg.get("from", {})
|
sender = msg.get("from", {})
|
||||||
|
uid = sender.get("id")
|
||||||
lang = sender.get("language_code", "ru")
|
lang = sender.get("language_code", "ru")
|
||||||
if lang != "ru" and lang != "en":
|
if lang != "ru" and lang != "en":
|
||||||
lang = "en"
|
lang = "en"
|
||||||
if text.startswith("/"):
|
if text.startswith("/"):
|
||||||
if text == '/start':
|
if text == '/start':
|
||||||
await telegram_api("sendMessage", chat_id=sender.get("id"), text=start_message[lang])
|
await telegram_api("sendMessage", chat_id=uid, text=start_message[lang])
|
||||||
|
elif text == '/removed':
|
||||||
|
removed_messages = await get_all_removed(uid)
|
||||||
|
await telegram_api("sendMessage", chat_id=uid, text="\n".join(removed_messages.values()))
|
||||||
else:
|
else:
|
||||||
await telegram_api("forwardMessage", from_chat_id=sender.get("id"), message_id=msg.get("id"), chat_id=FEEDBACK_CHAT_ID)
|
await telegram_api("forwardMessage", from_chat_id=sender.get("id"), message_id=msg.get("id"), chat_id=FEEDBACK_CHAT_ID)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
|
from store import redis
|
||||||
from bot.api import telegram_api
|
from bot.api import telegram_api
|
||||||
from bot.config import FEEDBACK_CHAT_ID
|
from bot.config import FEEDBACK_CHAT_ID
|
||||||
from nlp.toxicity_detector import detector
|
from nlp.toxicity_detector import detector
|
||||||
|
@ -36,8 +36,9 @@ async def messages_routing(msg, state):
|
||||||
logger.info(f'\ntext: {text}\ntoxic: {math.floor(toxic_score*100)}%')
|
logger.info(f'\ntext: {text}\ntoxic: {math.floor(toxic_score*100)}%')
|
||||||
if toxic_score > 0.71:
|
if toxic_score > 0.71:
|
||||||
if toxic_score > 0.85:
|
if toxic_score > 0.85:
|
||||||
|
await redis.execute("SET", f"removed:{uid}:{cid}:{mid}", text)
|
||||||
await telegram_api(
|
await telegram_api(
|
||||||
"deletemessage",
|
"deleteMessage",
|
||||||
chat_id=cid,
|
chat_id=cid,
|
||||||
message_id=mid
|
message_id=mid
|
||||||
)
|
)
|
||||||
|
|
3
main.py
3
main.py
|
@ -16,7 +16,7 @@ state = dict()
|
||||||
async def start():
|
async def start():
|
||||||
logger.info("\tstarted")
|
logger.info("\tstarted")
|
||||||
async with ClientSession() as session:
|
async with ClientSession() as session:
|
||||||
offset = 0 # начальное значение offset
|
offset = 0 # init offset
|
||||||
while True:
|
while True:
|
||||||
response = await telegram_api("getUpdates", offset=offset, allowed_updates=['message', 'message_reaction'])
|
response = await telegram_api("getUpdates", offset=offset, allowed_updates=['message', 'message_reaction'])
|
||||||
if isinstance(response, dict):
|
if isinstance(response, dict):
|
||||||
|
@ -48,5 +48,4 @@ async def start():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Запуск асинхронного цикла
|
|
||||||
asyncio.run(start())
|
asyncio.run(start())
|
||||||
|
|
21
store.py
Normal file
21
store.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
from bot.config import REDIS_URL
|
||||||
|
import redis.asyncio as r
|
||||||
|
|
||||||
|
# Connect to Redis
|
||||||
|
redis = r.Redis.from_url(REDIS_URL)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_all_removed(uid):
|
||||||
|
key = f"removed:{uid}:*"
|
||||||
|
|
||||||
|
# Create a dictionary to hold the keys and values
|
||||||
|
texts = []
|
||||||
|
|
||||||
|
# Use scan_iter to find all keys matching the pattern
|
||||||
|
for key in await redis.scan_iter(pattern):
|
||||||
|
# Fetch the value for each key
|
||||||
|
value = await redis.get(key)
|
||||||
|
if value:
|
||||||
|
texts.append(value)
|
||||||
|
|
||||||
|
return texts
|
Loading…
Reference in New Issue
Block a user