tgbot / app.py
nayminsan's picture
Update app.py
98f03d4 verified
Raw
History Blame Contribute Delete
8.3 kB
import os
import threading
import asyncio
from flask import Flask
from telegram import Update, constants
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
from huggingface_hub import hf_hub_download, HfApi
# --- Configuration ---
CREATOR_ID = 7482647888 # <--- သင်၏ Telegram ID
BOT_USER_FILE = "botuser.txt"
MESSAGES_FILE = "massages.txt"
# Hugging Face Hub Configuration
HF_TOKEN = os.environ.get("HF_TOKEN")
REPO_ID = os.environ.get("REPO_ID")
api = HfApi()
# Global state for active attacks
active_attacks = set()
# --- Helper Functions for Hub Sync ---
def load_from_hub():
if not HF_TOKEN or not REPO_ID:
return
for filename in [BOT_USER_FILE, MESSAGES_FILE]:
try:
hf_hub_download(repo_id=REPO_ID, filename=filename, repo_type="dataset", token=HF_TOKEN, local_dir=".")
except Exception:
pass
def save_to_hub(filename):
if not HF_TOKEN or not REPO_ID:
return
try:
api.upload_file(path_or_fileobj=filename, path_in_repo=filename, repo_id=REPO_ID, repo_type="dataset", token=HF_TOKEN)
except Exception:
pass
# Ensure files exist
load_from_hub()
for file in [BOT_USER_FILE, MESSAGES_FILE]:
if not os.path.exists(file):
with open(file, "w", encoding="utf-8") as f:
pass
# Flask setup
app = Flask(__name__)
@app.route('/')
def home():
return "Bot is running!"
def run_flask():
app.run(host='0.0.0.0', port=7860)
def is_authorized(user_id):
if user_id == CREATOR_ID:
return True
if os.path.exists(BOT_USER_FILE):
with open(BOT_USER_FILE, "r") as f:
authorized_users = f.read().splitlines()
return str(user_id) in authorized_users
return False
# --- Bot Commands ---
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("မင်္ဂလာပါ။ Bot မှ ကြိုဆိုပါတယ်!")
async def get_id(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
await update.message.reply_text(f"သင်၏ Telegram ID သည်: `{user_id}` ဖြစ်ပါသည်။", parse_mode=constants.ParseMode.MARKDOWN)
async def add_user(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update.effective_user.id != CREATOR_ID:
return await update.message.reply_text("Creator သာ သုံးနိုင်ပါသည်။")
target_id = None
if update.message.reply_to_message:
target_id = update.message.reply_to_message.from_user.id
elif context.args:
target_id = context.args[0]
if target_id:
with open(BOT_USER_FILE, "a") as f:
f.write(f"{target_id}\n")
save_to_hub(BOT_USER_FILE)
await update.message.reply_text(f"User ID `{target_id}` ကို ထည့်သွင်းပြီးပါပြီ။")
async def add_msg(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_authorized(update.effective_user.id):
return await update.message.reply_text("ခွင့်ပြုချက်မရှိပါ။")
new_msg = ""
if update.message.reply_to_message:
new_msg = update.message.reply_to_message.text
elif context.args:
new_msg = " ".join(context.args)
if new_msg:
# စာသားကို နောက်တစ်ကြောင်းတွင် သိမ်းဆည်းရန်
with open(MESSAGES_FILE, "a+", encoding="utf-8") as f:
f.seek(0, 2) # သွားရာနေရာကို အဆုံးသို့ပို့
if f.tell() > 0:
f.seek(f.tell() - 1)
last_char = f.read(1)
if last_char != '\n':
f.write('\n')
f.write(f"{new_msg}\n")
save_to_hub(MESSAGES_FILE)
await update.message.reply_text("စာသားကို နောက်တစ်ကြောင်းတွင် ထည့်သွင်းပြီးပါပြီ။")
else:
await update.message.reply_text("ထည့်သွင်းလိုသော စာသားပေးပါ။")
async def atk(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_authorized(update.effective_user.id):
return await update.message.reply_text("ခွင့်ပြုချက်မရှိပါ။")
target_id = None
if update.message.reply_to_message:
target_id = update.message.reply_to_message.from_user.id
elif context.args:
target_id = context.args[0]
if not target_id:
return await update.message.reply_text("Target ID ပေးပါ။")
target_id = str(target_id)
if target_id in active_attacks:
return await update.message.reply_text("ဤ User အား Attack လုပ်နေဆဲ ဖြစ်ပါသည်။")
active_attacks.add(target_id)
await update.message.reply_text(f"User `{target_id}` အား Attack စတင်ပါပြီ...\nရပ်တန့်လိုပါက /stop ဟု ရိုက်ပါ။", parse_mode=constants.ParseMode.MARKDOWN)
try:
while target_id in active_attacks:
# Loop တိုင်းတွင် ဖိုင်ကိုပြန်ဖတ်သဖြင့် စာသားအသစ်များပါဝင်လာမည်
if not os.path.exists(MESSAGES_FILE): break
with open(MESSAGES_FILE, "r", encoding="utf-8") as f:
messages = f.read().splitlines()
if not messages:
await asyncio.sleep(5)
continue
for msg in messages:
if target_id not in active_attacks:
break
if not msg.strip(): continue
mention_text = f"[\u200b](tg://user?id={target_id}){msg}"
try:
await context.bot.send_message(chat_id=update.effective_chat.id, text=mention_text, parse_mode=constants.ParseMode.MARKDOWN)
await asyncio.sleep(0.5)
except Exception as e:
print(f"Error: {e}")
await asyncio.sleep(5)
await asyncio.sleep(1)
finally:
if target_id in active_attacks:
active_attacks.remove(target_id)
await update.message.reply_text(f"User `{target_id}` အား Attack လုပ်ခြင်း ရပ်တန့်သွားပါပြီ။")
async def stop_atk(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_authorized(update.effective_user.id):
return await update.message.reply_text("ခွင့်ပြုချက်မရှိပါ။")
target_id = None
if update.message.reply_to_message:
target_id = update.message.reply_to_message.from_user.id
elif context.args:
target_id = context.args[0]
if target_id:
target_id = str(target_id)
if target_id in active_attacks:
active_attacks.remove(target_id)
await update.message.reply_text(f"User `{target_id}` အား Attack လုပ်ခြင်းကို ရပ်တန့်လိုက်ပါပြီ။")
else:
await update.message.reply_text("ဤ User သည် Attack စာရင်းတွင် မရှိပါ။")
else:
await update.message.reply_text("Target အား Reply ပြန်၍ /stop ရိုက်ပါ။")
if __name__ == '__main__':
TOKEN = os.environ.get('TELEGRAM_TOKEN')
if not TOKEN:
print("Error: TELEGRAM_TOKEN not found.")
else:
threading.Thread(target=run_flask, daemon=True).start()
application = ApplicationBuilder().token(TOKEN).build()
application.add_handler(CommandHandler('start', start))
application.add_handler(CommandHandler('id', get_id))
application.add_handler(CommandHandler('adduser', add_user))
application.add_handler(CommandHandler('addmsg', add_msg))
application.add_handler(CommandHandler('atk', atk))
application.add_handler(CommandHandler('stop', stop_atk))
application.run_polling()