Download main.py from thenamk3/telgram: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/thenamk3/telgram/resolve/main/main.py
- Command line
-
hf download hf://thenamk3/telgram/main.py
-
curl -L -o main.py https://huggingface.co/thenamk3/telgram/resolve/main/main.py
2.35 kB
| # Cài đặt các thư viện cần thiết (chạy trên Hugging Face Spaces) | |
| !pip install python-telegram-bot fastapi uvicorn --quiet | |
| from fastapi import FastAPI | |
| from telegram import Update | |
| from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext | |
| import re | |
| import os | |
| # Khởi tạo ứng dụng FastAPI | |
| app = FastAPI() | |
| # Thay thế bằng token bot của bạn từ BotFather | |
| TOKEN = '7868920126:AAGec4AJR8gGNttsDcVEYGMKLkqexH-MJng' | |
| # Đường dẫn file chứa ID | |
| ID_FILE_PATH = "ids.txt" | |
| # Hàm lưu ID vào file | |
| def save_id(user_id: str): | |
| with open(ID_FILE_PATH, "a") as file: | |
| file.write(user_id + "\n") | |
| # Hàm xử lý tin nhắn | |
| def handle_message(update: Update, context: CallbackContext) -> None: | |
| message_text = update.message.text | |
| # Tìm các ID có dạng NemG-xxxxx hoặc NemG-xxxxxxx | |
| match = re.search(r"NemG-\d{5,7}", message_text) | |
| if match: | |
| user_id = match.group(0) | |
| save_id(user_id) | |
| update.message.reply_text(f"ID '{user_id}' đã được lưu thành công!") | |
| # Hàm để gửi link xem danh sách ID | |
| def show_ids(update: Update, context: CallbackContext) -> None: | |
| try: | |
| with open(ID_FILE_PATH, "r") as file: | |
| ids = file.read() | |
| # Tạo link để xem file qua API | |
| link = "https://nemg.me/ids" | |
| update.message.reply_text(f"Danh sách ID:\n{ids}\nHoặc xem link: {link}") | |
| except FileNotFoundError: | |
| update.message.reply_text("Chưa có ID nào được lưu.") | |
| # Khởi động bot Telegram | |
| def start_bot(): | |
| updater = Updater(TOKEN, use_context=True) | |
| dispatcher = updater.dispatcher | |
| # Thêm handler cho tin nhắn và lệnh | |
| dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message)) | |
| dispatcher.add_handler(CommandHandler("showids", show_ids)) | |
| # Bắt đầu bot | |
| updater.start_polling() | |
| updater.idle() | |
| # Endpoint API để lấy danh sách ID | |
| def read_ids(): | |
| try: | |
| with open(ID_FILE_PATH, "r") as file: | |
| ids = file.read() | |
| return {"ids": ids} | |
| except FileNotFoundError: | |
| return {"message": "Không có ID nào được lưu."} | |
| # Chạy bot Telegram khi ứng dụng FastAPI khởi động | |
| import threading | |
| threading.Thread(target=start_bot).start() |