Spaces:
Runtime error
Runtime error
File size: 17,072 Bytes
61babb1 faba822 9271535 faba822 604263b 9271535 4968a43 61babb1 9271535 74f0db8 4968a43 9271535 4968a43 4f1a06c 9271535 61babb1 5f6b870 61babb1 74f0db8 4968a43 9271535 74f0db8 4968a43 9271535 61babb1 9271535 4f1a06c 9271535 deeb4a7 e9c315d 9271535 4968a43 61babb1 f3e7fd0 4968a43 61babb1 9271535 61babb1 9271535 61babb1 4f1a06c 9271535 4968a43 2e8340f 9271535 4968a43 9271535 4968a43 9271535 4968a43 9271535 964d4f3 9271535 2e8340f 9271535 2e8340f 9271535 61babb1 2e8340f 9271535 2e8340f 61babb1 9271535 4968a43 61babb1 4968a43 9271535 4968a43 61babb1 9271535 4968a43 83ce55b 61babb1 4968a43 2e8340f 61babb1 4968a43 9271535 61babb1 4968a43 9271535 4968a43 9271535 3b97a72 9271535 4968a43 61babb1 4968a43 0b855d4 9271535 4968a43 9271535 61babb1 9271535 4968a43 964d4f3 9271535 4968a43 964d4f3 83ce55b 9271535 4968a43 9271535 4968a43 9271535 61babb1 9271535 4968a43 9271535 4968a43 9271535 61babb1 9271535 0b855d4 4968a43 9271535 0b855d4 4968a43 9271535 4968a43 9271535 964d4f3 9271535 964d4f3 9271535 964d4f3 604263b 4968a43 9271535 604263b 4968a43 9271535 4968a43 9271535 4968a43 9271535 4f1a06c 9271535 4968a43 9271535 4f1a06c 9271535 4968a43 2e8340f 4968a43 9271535 4f1a06c 964d4f3 deeb4a7 4f1a06c 964d4f3 b44f37f 4f1a06c 2e8340f 4f1a06c 2e8340f b202111 faba822 61babb1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | #!/usr/bin/env python3
import os
import re
import json
import asyncio
import logging
import tempfile
from datetime import datetime
from typing import Dict, List, Optional, Any
import aiohttp
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
Application,
CommandHandler,
MessageHandler,
ContextTypes,
filters,
CallbackQueryHandler
)
from telegram.request import HTTPXRequest
from google import genai
import firebase_admin
from firebase_admin import credentials, firestore
# ============= КОНФИГУРАЦИЯ =============
TG_BOT_TOKEN = "8531345451:AAE8qJlFEwIcuQdUiIFk2viZS9xLLMk2c_o"
ADMIN_ID = 8746779101
BOOST_LINK = "https://t.me/boost/chat_crx"
GEMINI_API_KEY = "AIzaSyC59FksQAoeVeBKzVpxpL8zByEg3DAkKgE"
GEMINI_MODEL_NAME = "gemini-2.5-flash"
RAPIDAPI_KEY = "5626a251afmsh7fc1baa1c873297p11bf87jsn3267c0694304"
RAPIDAPI_HOST = "veo-3-video.p.rapidapi.com"
HF_MUSICGEN_URL = "https://api-inference.huggingface.co/models/facebook/musicgen-medium"
HF_TOKEN = "hf_public"
# Cloudflare Worker прокси
CF_WORKER_URL = "https://tg-proxy.artyomanisimov37.workers.dev"
# ============= FIRESTORE =============
db = None
try:
if not firebase_admin._apps:
cred = credentials.Certificate("firebase_admin.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
logging.info("✅ Firebase подключён")
except Exception as e:
logging.error(f"❌ Firebase ошибка: {e}")
# ============= GEMINI =============
genai_client = genai.Client(api_key=GEMINI_API_KEY)
# ============= CONTENT AGGREGATOR =============
class ContentAggregator:
@staticmethod
async def search_web(query: str) -> List[Dict[str, str]]:
results = []
try:
url = f"https://html.duckduckgo.com/html/?q={query.replace(' ', '+')}"
async with aiohttp.ClientSession() as session:
async with session.get(url, headers={"User-Agent": "Mozilla/5.0"}) as resp:
html = await resp.text()
links = re.findall(r'<a[^>]*href="//([^"]+)"[^>]*>([^<]+)</a>', html)
for link, title in links[:5]:
if not any(x in link for x in ["youtube.com", "facebook.com", "twitter.com"]):
results.append({"title": title.strip(), "link": f"https://{link}"})
except Exception as e:
logging.error(f"Ошибка поиска: {e}")
return results
@staticmethod
async def fetch_page_content(url: str) -> str:
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=10) as resp:
html = await resp.text()
text = re.sub(r'<[^>]+>', ' ', html)
text = re.sub(r'\s+', ' ', text)
return text[:4000]
except:
return ""
@staticmethod
async def generate_unique_content(topic: str) -> Dict[str, Any]:
logging.info(f"Агент собирает контент: {topic}")
search_results = await ContentAggregator.search_web(topic)
contexts = []
for res in search_results[:3]:
content = await ContentAggregator.fetch_page_content(res["link"])
if content:
contexts.append(content)
aggregated = "\n\n---\n\n".join(contexts)
prompt = f"""
Ты – контент-мейкер. На основе данных по теме "{topic}":
{aggregated[:6000]}
Создай JSON с полями:
- "article": уникальная статья 800-1500 слов
- "video_ideas": массив из 10 идей для видео
- "insights": массив из 5 инсайтов, которых нет в исходных данных
"""
try:
# ИСПОЛЬЗУЕМ .aio ДЛЯ АСИНХРОННЫХ ВЫЗОВОВ
response = await genai_client.aio.models.generate_content(
model=GEMINI_MODEL_NAME,
contents=prompt
)
match = re.search(r'\{.*\}', response.text, re.DOTALL)
if match:
return json.loads(match.group())
return {"error": "Не удалось распарсить JSON", "raw": response.text}
except Exception as e:
return {"error": str(e)}
# ============= MUSIC GENERATOR =============
class MusicGenerator:
@staticmethod
async def generate_music(prompt: str) -> Optional[str]:
headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN and HF_TOKEN != "hf_public" else {}
payload = {"inputs": prompt, "parameters": {"max_new_tokens": 256}}
try:
async with aiohttp.ClientSession() as session:
async with session.post(HF_MUSICGEN_URL, headers=headers, json=payload, timeout=60) as resp:
if resp.status == 200:
audio_data = await resp.read()
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
tmp.write(audio_data)
return tmp.name
else:
logging.warning(f"MusicGen вернул {resp.status}, используем демо")
return None
except Exception as e:
logging.error(f"MusicGen ошибка: {e}")
return None
# ============= VIDEO GENERATOR =============
class VideoGenerator:
@staticmethod
async def generate_video(prompt: str) -> Optional[str]:
url = "https://veo-3-video.p.rapidapi.com/v1/jobs"
payload = {
"model": "veo-3",
"prompt": prompt,
"duration": 8,
"aspect_ratio": "16:9"
}
headers = {
"x-rapidapi-key": RAPIDAPI_KEY,
"x-rapidapi-host": RAPIDAPI_HOST,
"Content-Type": "application/json"
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload, headers=headers) as resp:
data = await resp.json()
job_id = data.get("job_id")
if not job_id:
return None
await asyncio.sleep(15)
status_url = f"https://veo-3-video.p.rapidapi.com/v1/jobs/{job_id}"
async with session.get(status_url, headers=headers) as status_resp:
status_data = await status_resp.json()
return status_data.get("video_url")
except Exception as e:
logging.error(f"Видео ошибка: {e}")
return "https://www.w3schools.com/html/mov_bbb.mp4"
# ============= BOT STATE =============
class BotState:
def __init__(self):
self.whitelist = set()
self.premium_users = set()
self.load_data()
def load_data(self):
if db:
try:
doc = db.collection("bot_config").document("whitelist").get()
if doc.exists:
self.whitelist = set(doc.to_dict().get("users", []))
prem = db.collection("bot_config").document("premium").get()
if prem.exists:
self.premium_users = set(prem.to_dict().get("users", []))
self.whitelist.add(ADMIN_ID)
except Exception as e:
logging.error(f"Загрузка БД: {e}")
def is_allowed(self, user_id: int) -> bool:
return user_id in self.whitelist or user_id == ADMIN_ID or user_id in self.premium_users
bot_state = BotState()
# ============= HANDLERS =============
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
user = update.effective_user
if not bot_state.is_allowed(user.id):
await update.message.reply_text("⛔ Доступ ограничен. Обратитесь к админу.")
return
keyboard = [
[InlineKeyboardButton("💎 Premium", url=BOOST_LINK)],
[InlineKeyboardButton("🤖 Уникальный контент", callback_data="gen_content")],
[InlineKeyboardButton("🎵 Музыка", callback_data="gen_music")],
[InlineKeyboardButton("🎬 Видео", callback_data="gen_video")],
[InlineKeyboardButton("📚 Помощь", callback_data="help")]
]
await update.message.reply_text(
f"👋 Привет, {user.first_name}!\nЯ ИИ бот на **Gemini 2.5 Flash**\n\n"
"/ask [вопрос] – задать вопрос\n"
"/content [тема] – создать статью + идеи\n"
"/music [описание] – сгенерировать трек (Premium)\n"
"/video [описание] – сгенерировать видео (Premium)",
reply_markup=InlineKeyboardMarkup(keyboard)
)
async def ask_ai(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
if not bot_state.is_allowed(user_id):
return
query = " ".join(context.args)
if not query:
await update.message.reply_text("Пример: /ask Как работает ИИ?")
return
msg = await update.message.reply_text("🤔 Думаю...")
try:
# ИСПОЛЬЗУЕМ .aio
resp = await genai_client.aio.models.generate_content(model=GEMINI_MODEL_NAME, contents=query)
await msg.edit_text(resp.text[:4000], parse_mode='Markdown')
except Exception as e:
await msg.edit_text("❌ Извините, сервера Google сейчас перегружены или произошла ошибка. Попробуйте позже.")
logging.error(f"Ошибка Gemini: {e}")
async def create_content(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
if not bot_state.is_allowed(user_id):
return
topic = " ".join(context.args)
if not topic:
await update.message.reply_text("Укажите тему: /content космос")
return
msg = await update.message.reply_text("🔍 Агент ищет информацию... (30-60 сек)")
data = await ContentAggregator.generate_unique_content(topic)
if "error" in data:
await msg.edit_text(f"Ошибка: {data['error']}")
return
answer = f"📄 *{topic}*\n\n"
answer += f"**Статья:**\n{data.get('article', '')[:1500]}...\n\n"
answer += "**💡 Идеи для видео:**\n" + "\n".join(f"{i+1}. {idea}" for i, idea in enumerate(data.get('video_ideas', [])[:5]))
answer += "\n\n**🔍 Инсайты:**\n" + "\n".join(f"• {insight}" for insight in data.get('insights', [])[:3])
await msg.edit_text(answer[:4000], parse_mode='Markdown')
async def generate_music(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
if user_id not in bot_state.premium_users and user_id != ADMIN_ID:
await update.message.reply_text(f"🌟 Premium функция. Бустите канал: {BOOST_LINK}")
return
prompt = " ".join(context.args)
if not prompt:
await update.message.reply_text("Опишите музыку: /music грустная фортепианная мелодия")
return
msg = await update.message.reply_text("🎵 Сочиняю... до 30 сек")
audio_path = await MusicGenerator.generate_music(prompt)
if audio_path and os.path.exists(audio_path):
await update.message.reply_audio(audio=open(audio_path, 'rb'), caption=f"🎵 {prompt[:100]}")
os.unlink(audio_path)
else:
await update.message.reply_audio(audio="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
caption="Демо-трек (API занят)")
await msg.delete()
async def generate_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
if user_id not in bot_state.premium_users and user_id != ADMIN_ID:
await update.message.reply_text(f"🎬 Premium функция. Бустите: {BOOST_LINK}")
return
prompt = " ".join(context.args)
if not prompt:
await update.message.reply_text("Опишите видео: /video кот в космосе")
return
msg = await update.message.reply_text("🎬 Генерирую видео... 1-2 мин")
video_url = await VideoGenerator.generate_video(prompt)
if video_url:
await update.message.reply_video(video=video_url, caption=f"🎬 {prompt[:100]}")
else:
await update.message.reply_text("Не удалось создать видео, попробуйте позже")
await msg.delete()
async def handle_text(update: Update, context: ContextTypes.DEFAULT_TYPE):
text = update.message.text
user_id = update.effective_user.id
if not bot_state.is_allowed(user_id):
return
# Вспомогательная функция с обработкой ошибок API
async def process_prompt(prompt_text):
try:
# ИСПОЛЬЗУЕМ .aio
resp = await genai_client.aio.models.generate_content(model=GEMINI_MODEL_NAME, contents=prompt_text)
await update.message.reply_text(resp.text[:4000], parse_mode='Markdown')
except Exception as e:
logging.error(f"Gemini API Error: {e}")
await update.message.reply_text("⚠️ Ошибка: Сервера Google сейчас перегружены. Пожалуйста, попробуйте чуть позже.")
if text.lower().startswith(("ии ", "ai ")):
prompt = text[3:].strip()
if prompt:
await process_prompt(prompt)
elif len(text) > 20 and not text.startswith("/"):
await process_prompt(text)
async def premium_info(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(f"💎 Premium даёт видео, музыку, приоритет.\nПолучите: {BOOST_LINK}")
async def admin_whitelist(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update.effective_user.id != ADMIN_ID:
return
if len(context.args) < 2:
await update.message.reply_text("/w add|rem|prem [user_id]")
return
cmd, uid = context.args[0], int(context.args[1])
if cmd == "add":
bot_state.whitelist.add(uid)
if db:
db.collection("bot_config").document("whitelist").set({"users": list(bot_state.whitelist)})
await update.message.reply_text(f"✅ {uid} в белом списке")
elif cmd == "prem":
bot_state.premium_users.add(uid)
if db:
db.collection("bot_config").document("premium").set({"users": list(bot_state.premium_users)})
await update.message.reply_text(f"🌟 {uid} теперь Premium")
async def callback_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
data = query.data
if data == "help":
await query.edit_message_text("/ask – вопрос\n/content – статья+идеи\n/music – трек (Premium)\n/video – клип (Premium)")
elif data == "gen_content":
await query.edit_message_text("Отправьте: /content [тема]")
elif data == "gen_music":
await query.edit_message_text("Premium команда: /music [описание]")
elif data == "gen_video":
await query.edit_message_text("Premium команда: /video [описание]")
# ============= MAIN =============
def main():
logging.basicConfig(level=logging.INFO)
# Оставляем только настройки таймаутов, proxy_url убран
request = HTTPXRequest(
connect_timeout=30.0,
read_timeout=40.0,
write_timeout=40.0,
pool_timeout=15.0,
connection_pool_size=20
)
# CF_WORKER_URL передан в base_url
application = (
Application.builder()
.token(TG_BOT_TOKEN)
.base_url(f"{CF_WORKER_URL}/bot")
.request(request)
.build()
)
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("ask", ask_ai))
application.add_handler(CommandHandler("content", create_content))
application.add_handler(CommandHandler("music", generate_music))
application.add_handler(CommandHandler("video", generate_video))
application.add_handler(CommandHandler("premium", premium_info))
application.add_handler(CommandHandler("w", admin_whitelist))
application.add_handler(CallbackQueryHandler(callback_handler))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_text))
application.run_polling()
if __name__ == "__main__":
main() |