Description
import subprocess
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
import logging
تنظیمات لاگینگ
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(name)
توکن تلگرام خود را وارد کنید
TELEGRAM_TOKEN = '7124126923:AAHVVtgGQlJA32M_shpw-9WhehfMLndNcHY'
توابع برای دستورات مختلف
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text("خوش آمدید!")
def help_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text(
"/start - شروع\n"
"/help - کمک\n"
"/update - بروزرسانی\n"
"/create - ساخت ربات جدید\n"
"/createmanual - ساخت ربات جدید به صورت دستی\n"
"/fix - بازیابی اطلاعات\n"
"/autolaunch - راهاندازی خودکار\n"
"/clear - پاک کردن ربات\n"
"/install - نصب پیش نیازها\n"
)
def update_bot():
subprocess.run(["git", "fetch", "--all"])
subprocess.run(["git", "reset", "--hard", "origin/persian"])
subprocess.run(["git", "pull", "origin", "persian"])
subprocess.run(["chmod", "+x", "bot"])
logger.info("بروزرسانی اطلاعات با موفقیت انجام شد.")
def update_command(update: Update, context: CallbackContext) -> None:
update_bot()
update.message.reply_text("بروزرسانی اطلاعات با موفقیت انجام شد.")
def create_command(update: Update, context: CallbackContext) -> None:
# کد ساخت ربات جدید
update.message.reply_text("ربات جدید با موفقیت ساخته شد.")
def createmanual_command(update: Update, context: CallbackContext) -> None:
# کد ساخت ربات به صورت دستی
update.message.reply_text("لطفاً شماره دلخواه خود را وارد کنید.")
def fix_command(update: Update, context: CallbackContext) -> None:
subprocess.run(["git", "reset", "--hard", "FETCH_HEAD"])
update.message.reply_text("بازیابی اطلاعات به آخرین بروزرسانی انجام شد.")
def autolaunch_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text("راهاندازی خودکار رباتها هر 20 دقیقه فعال شد.")
def clear_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text("لطفاً شماره شناسه رباتی که میخواهید پاک کنید را وارد کنید.")
def install_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text("نصب پیش نیازهای ربات در حال انجام است.")
هندلر خطا
def error_handler(update: Update, context: CallbackContext) -> None:
logger.error(msg="Exception while handling an update:", exc_info=context.error)
if update and update.message:
update.message.reply_text('خطایی رخ داده است. لطفاً دوباره تلاش کنید.')
def main() -> None:
updater = Updater(TELEGRAM_TOKEN)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_command))
dispatcher.add_handler(CommandHandler("update", update_command))
dispatcher.add_handler(CommandHandler("create", create_command))
dispatcher.add_handler(CommandHandler("createmanual", createmanual_command))
dispatcher.add_handler(CommandHandler("fix", fix_command))
dispatcher.add_handler(CommandHandler("autolaunch", autolaunch_command))
dispatcher.add_handler(CommandHandler("clear", clear_command))
dispatcher.add_handler(CommandHandler("install", install_command))
dispatcher.add_error_handler(error_handler)
updater.start_polling()
updater.idle()
if name == 'main':
main()