import logging from telegram import Update from telegram.ext import Updater, CommandHandler, CallbackContext from pyrad.client import Client from pyrad.packet import AccessRequest, AccessAccept, AccessReject from pyrad.dictionary import Dictionary from pyrad.server import Server from pyrad.util import str2addr import socket # تنظیمات ربات تلگرام TELEGRAM_TOKEN = "7786920503:AAHtEhx1GJvSFCySgKanAMwZsXzULUrt2JM" RADIOS_IP = "193.151.141.30" RADIOS_SECRET = b"123456789" USER_RADIUS = "admin" # ایجاد اتصال به میکروتیک از طریق RADIUS def connect_to_radius(): radius_client = Client(server=RADIOS_IP, secret=RADIOS_SECRET, dict=Dictionary("path_to_radius.dict")) radius_client.AuthPort = 1812 return radius_client # ساخت یوزر جدید از طریق RADIUS def add_user(update: Update, context: CallbackContext): username = context.args[0] if len(context.args) > 0 else "new_user" password = context.args[1] if len(context.args) > 1 else "default_password" radius_client = connect_to_radius() req = radius_client.CreateRequest(AccessRequest) req["User-Name"] = req.CreateString(username) req["User-Password"] = req.CreateString(password) try: reply = radius_client.SendPacket(req) if isinstance(reply, AccessAccept): update.message.reply_text(f"User {username} added successfully.") else: update.message.reply_text("Failed to add user.") except Exception as e: update.message.reply_text(f"Error: {str(e)}") # حذف یوزر از طریق RADIUS def remove_user(update: Update, context: CallbackContext): username = context.args[0] if len(context.args) > 0 else "user_to_remove" radius_client = connect_to_radius() req = radius_client.CreateRequest(AccessRequest) req["User-Name"] = req.CreateString(username) try: reply = radius_client.SendPacket(req) if isinstance(reply, AccessAccept): update.message.reply_text(f"User {username} removed successfully.") else: update.message.reply_text("Failed to remove user.") except Exception as e: update.message.reply_text(f"Error: {str(e)}") # تنظیمات ربات تلگرام def main(): updater = Updater(TELEGRAM_TOKEN, use_context=True) dispatcher = updater.dispatcher dispatcher.add_handler(CommandHandler("adduser", add_user)) dispatcher.add_handler(CommandHandler("removeuser", remove_user)) updater.start_polling() updater.idle() if __name__ == '__main__': main()