Spam Detector Telegram Bot flags spam in Telegram groups. You can run it in two ways:
- TypeSafe (no training) — set
TYPESAFE_API_KEYand start the bot. TypeSafe Jev returns structured spam judgments (choice, hazard probabilities, severity) that the bot thresholds in code. - Your own SVM — label messages with
/detect, export them with/send_data, train withpython train/train.py. Useful for community-specific Persian slang.
If both are configured, CLASSIFIER_BACKEND=auto uses hybrid mode: either judge can flag a message for admin review, and a TypeSafe outage falls back to the local model.
Flagged messages still go to admins (warning the user, review buttons, delete or confirm). The bot does not auto-delete without an admin click.
- Zero-training TypeSafe path: run immediately with an API key; no
data.csvor pickle files. - Optional local SVM: TF-IDF + linear SVM trained on your CSV for group-specific language.
- Hybrid mode: TypeSafe semantic judgments plus the local model; SVM-only still works offline.
- Confidence-gated flagging: TypeSafe Choice/Score answers are ignored when confidence is below
TYPESAFE_CONFIDENCE_FLOOR; independent Noul hazards can still flag. - Admin tools:
/check(probability + reasons),/detect(label for SVM training),/send_data,/status(which backend is live). - Logging: errors in
bot.log; delete/flag actions forwarded to a logs channel. - Persian support: user-facing copy is Persian. Jev is strongest in English; a local SVM still helps on Persian slang.
core/bot.py: Telegram handlers, commands, and callbacks.core/classifier.py: SVM, TypeSafe, and hybrid backends.core/typesafe_moderation.py: TypeSafe questions, state, and routing policy (the file to edit when spam rules change).train/train.py: Train the SVM fromdata.csv(message,label), savemodel.pklandvectorizer.pkl.preprocessing.py: Shared Persian text normalization for the trainer and the SVM path.requirements.txt: Python packages, includingtypesafe-sdk..env.example: Configuration template.data.csv: SVM training set (not included; user-provided).spam_messages.csv: Labels collected by/detect(same schema asdata.csv).model.pkl&vectorizer.pkl: SVM artifacts after training.bot.log: Generated log file.README.md: This file.
Configuration is read from .env. Persian text is normalized the same way at SVM train and inference time. TypeSafe is given both the original message and the normalized form, plus an English group policy.
- Python 3.10 or higher (
typesafe-sdkrequirement). - Telegram Bot Token (from @BotFather).
- Either a TypeSafe API key from the TypeSafe dashboard or a
data.csvtraining set (or both). - A
.envfile (copy from.env.example) withAUTH_TOKEN,LOGS_CHANNEL_ID, andADMINS_GROUP_ID. - Bot must be an admin in target groups with delete-message permissions.
ربات تشخیص اسپم تلگرام پیامهای اسپم را در گروههای تلگرام علامت میگذارد. دو روش برای اجرا هست:
- TypeSafe (بدون آموزش) — فقط
TYPESAFE_API_KEYرا بگذارید و ربات را اجرا کنید. مدل Jev قضاوت ساختیافته برمیگرداند و ربات در کد تصمیم میگیرد. - SVM خودتان — با
/detectبرچسب بزنید، با/send_dataخروجی بگیرید، باpython train/train.pyآموزش دهید. برای اصطلاحات خاص گروه مفید است.
اگر هر دو پیکربندی شده باشند، حالت hybrid هر دو را با هم استفاده میکند.
پیامهای مشکوک همچنان برای ادمین میروند؛ ربات بدون کلیک ادمین پیام را حذف نمیکند.
- مسیر بدون آموزش TypeSafe: با کلید API فوراً اجرا میشود.
- SVM محلی اختیاری: TF-IDF + SVM خطی روی CSV شما.
- حالت ترکیبی: قضاوت TypeSafe بهعلاوه مدل محلی.
- علامتگذاری مبتنی بر اطمینان: Choice/Score با اطمینان پایین بهتنهایی عمل نمیکنند.
- ابزارهای ادمین:
/check،/detect،/send_data،/status. - ثبت لاگ:
bot.logو کانال لاگ. - پشتیبانی فارسی: متنهای کاربر فارسی است. دقت Jev روی انگلیسی بهتر است؛ SVM محلی برای اصطلاحات فارسی کمک میکند.
core/bot.py: هندلرهای تلگرام.core/classifier.py: بکاندهای SVM، TypeSafe و hybrid.core/typesafe_moderation.py: سوالها و سیاست مسیریابی TypeSafe.train/train.py: آموزش SVM ازdata.csv.preprocessing.py: نرمالسازی متن فارسی برای SVM.requirements.txt: وابستگیها شاملtypesafe-sdk..env.example: قالب پیکربندی.spam_messages.csv: برچسبهای/detect.model.pklوvectorizer.pkl: خروجی آموزش SVM.README.md: این فایل.
- پایتون 3.10 یا بالاتر.
- توکن ربات تلگرام (از @BotFather).
- یا کلید TypeSafe یا فایل
data.csv(یا هر دو). - فایل
.envباAUTH_TOKEN،LOGS_CHANNEL_IDوADMINS_GROUP_ID. - ربات باید ادمین گروه با مجوز حذف پیام باشد.
-
Clone the Repository:
git clone https://github.com/your-repo/Spam-detector-telegram-bot.git cd Spam-detector-telegram-bot -
Install Dependencies:
pip install -r requirements.txt -
Configure: Copy
.env.exampleto.envand fill in your values:cp .env.example .env # required: AUTH_TOKEN, LOGS_CHANNEL_ID, ADMINS_GROUP_ID # to run without training: TYPESAFE_API_KEY # optional: CLASSIFIER_BACKEND, SPAM_THRESHOLD, TYPESAFE_CONFIDENCE_FLOOR, AD_LINK, GROUP_USERNAME -
Choose a classifier:
- No training: set
TYPESAFE_API_KEY. LeaveCLASSIFIER_BACKEND=auto(ortypesafe). - Local SVM: put
data.csvin the repo root (message,labelcolumns; labelsspam/normal) and run:Prints accuracy plus precision/recall/F1; retrain with more data if accuracy is below 90%.python train/train.py - Both: set the API key and train.
autothen selects hybrid.
- No training: set
-
Run the Bot (from the repo root):
python core/bot.py
Add the bot to your group as an admin with delete permissions.
-
کلون کردن مخزن:
git clone https://github.com/your-repo/Spam-detector-telegram-bot.git cd Spam-detector-telegram-bot -
نصب وابستگیها:
pip install -r requirements.txt -
پیکربندی:
.env.exampleرا به.envکپی کنید و مقادیر را پر کنید (حداقلAUTH_TOKEN،LOGS_CHANNEL_ID،ADMINS_GROUP_ID). برای اجرا بدون آموزشTYPESAFE_API_KEYرا هم بگذارید. -
انتخاب طبقهبند:
- بدون آموزش:
TYPESAFE_API_KEY - SVM محلی:
data.csvوpython train/train.py - هر دو: hybrid
- بدون آموزش:
-
اجرای ربات (از ریشهٔ مخزن):
python core/bot.py
ربات را به گروه بهعنوان ادمین با مجوز حذف اضافه کنید.
- Add to Group: Invite the bot, make it an admin with delete rights.
- Commands (admin-only):
/detect: Reply to a message, choose spam/normal; saves to CSV for optional SVM retraining./send_data: Sendsspam_messages.csv./check: Reply to a message; shows spam probability, backend, and TypeSafe reasons when available./status: Shows the live classifier backend and thresholds.
- Automatic Detection: Scans all text messages. If the active backend decides
should_flag, the user is warned and the admins group gets review buttons (delete or 👍). Admin alerts include TypeSafe hazard labels when that backend fired. - Retraining the SVM:
spam_messages.csvuses amessage,labelheader, same asdata.csv. Rename/append it and rerunpython train/train.py. This does not retrain TypeSafe; editcore/typesafe_moderation.pyto change Jev’s questions and cutoffs. - Backends:
CLASSIFIER_BACKEND=auto|typesafe|svm|hybrid. TypeSafe calls fail open (do not freeze the group); hybrid then uses the SVM.
Monitor bot.log for errors. Get a TypeSafe key at console.typesafe.ai.
- اضافه کردن به گروه: ربات را دعوت کنید و ادمین با حق حذف کنید.
- دستورات (فقط ادمین):
/detect: برچسب اسپم/عادی برای آموزش SVM./send_data: ارسال CSV./check: احتمال اسپم و دلایل TypeSafe./status: بکاند فعال و آستانهها.
- تشخیص خودکار: اگر طبقهبند
should_flagبدهد، هشدار به کاربر و اطلاع به ادمینها. - بازآموزش SVM:
spam_messages.csvهمان قالبdata.csvرا دارد. TypeSafe با CSV بازآموزش نمیشود؛ سوالها درcore/typesafe_moderation.pyهستند.
Contributions welcome! Fork, branch, commit, push, PR. Report issues on GitHub.
مشارکت خوش آمدید! فورک، شاخه، کامیت، پوش، PR. مشکلات را در GitHub گزارش دهید.
MIT License - see LICENSE for details.