-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
63 lines (53 loc) · 2.04 KB
/
Copy pathbot.py
File metadata and controls
63 lines (53 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
from telegram import Update
from telegram.ext import ApplicationBuilder, MessageHandler, filters, ContextTypes
from openai import OpenAI
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("bot.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
TELEGRAM_TOKEN = "seu_token_aqui"
client = OpenAI(
base_url="https://api.astralai.pro/v1",
api_key="sua_api_key_aqui"
)
CATEGORIAS_PT = {
"harassment": "assédio",
"harassment_threatening": "assédio com ameaça",
"hate": "discurso de ódio",
"hate_threatening": "discurso de ódio com ameaça",
"self_harm": "automutilação",
"self_harm_instructions": "instruções de automutilação",
"self_harm_intent": "intenção de automutilação",
"sexual": "conteúdo sexual",
"sexual_minors": "conteúdo sexual envolvendo menores",
"violence": "violência",
"violence_graphic": "violência gráfica",
}
async def moderar_mensagem(update: Update, context: ContextTypes.DEFAULT_TYPE):
texto = update.message.text
usuario = update.message.from_user
logger.info(f"Mensagem de @{usuario.username} (id {usuario.id}): {texto}")
response = client.moderations.create(input=texto)
result = response.results[0]
logger.info(f"Scores: {result.category_scores}")
if result.flagged:
categorias = [
CATEGORIAS_PT.get(k, k)
for k, v in result.categories.__dict__.items() if v
]
motivo = ", ".join(categorias)
logger.warning(f"BLOQUEADA de @{usuario.username}: {motivo}")
await update.message.reply_text(f"⚠️ Mensagem bloqueada.\n\nMotivo: {motivo}")
else:
logger.info(f"APROVADA de @{usuario.username}")
await update.message.reply_text("Mensagem aprovada!")
app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, moderar_mensagem))
logger.info("Bot iniciado!")
app.run_polling()