import os import telebot import requests from dotenv import load_dotenv # Đọc token từ file .env load_dotenv() TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') if not TOKEN: raise ValueError("❌ Chưa cấu hình TELEGRAM_BOT_TOKEN trong file .env!") bot = telebot.TeleBot(TOKEN) @bot.message_handler(commands=['start']) def send_welcome(message): bot.reply_to(message, "🎬 Chào bạn! Nhập tên phim muốn xem, mình sẽ lấy link trực tiếp cho nhé.") @bot.message_handler(func=lambda message: True) def search_and_get_link(message): query = message.text # API tìm kiếm để lấy slug search_url = f"https://ophim1.com/v1/api/tim-kiem?keyword={query}" try: search_res = requests.get(search_url).json() items = search_res.get('data', {}).get('items', []) if not items: bot.reply_to(message, "❌ Không tìm thấy phim này rồi!") return # Lấy slug của bộ phim đầu tiên tìm thấy movie_slug = items[0]['slug'] # Gọi API chi tiết để lấy danh sách tập phim và link detail_url = f"https://ophim1.com/phim/{movie_slug}" detail_res = requests.get(detail_url).json() movie_info = detail_res.get('movie', {}) episodes = detail_res.get('episodes', []) if episodes: # Lấy server đầu tiên và tập mới nhất (thường là phần tử cuối trong server_data) server_data = episodes[0].get('server_data', []) if server_data: latest_ep = server_data[-1] watch_link = latest_ep.get('link_embed', '') # Lấy link Embed như trong ảnh bạn gửi response_msg = ( f"✅ **Đã tìm thấy: {movie_info.get('name')}**\n" f"🎭 Tên gốc: {movie_info.get('origin_name')}\n" f"📅 Năm: {movie_info.get('year')}\n" f"🚩 Trạng thái: {movie_info.get('episode_current')}\n\n" f"📺 **[NHẤN VÀO ĐÂY ĐỂ XEM PHIM NGAY]({watch_link})**" ) bot.send_message(message.chat.id, response_msg, parse_mode="Markdown") else: bot.reply_to(message, "Hic, phim này hiện chưa có link xem.") else: bot.reply_to(message, "Phim này hiện chưa cập nhật tập nào.") except Exception as e: print(f"Lỗi: {e}") bot.reply_to(message, "⚠️ Có lỗi khi lấy dữ liệu từ máy chủ phim!") print("Bot xem phim trực tiếp đang chạy...") bot.infinity_polling()