Spaces:
Runtime error
Runtime error
| import logging | |
| import os | |
| from telegram import Update, ReplyKeyboardMarkup | |
| from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes | |
| from duckduckgo_search import DDGS | |
| # Configure Logging | |
| logging.basicConfig( | |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
| level=logging.INFO | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # Telegram Bot Token | |
| TELEGRAM_TOKEN = "8375523966:AAFWEUIBp33GU4ECB-dXcDHSLWEUToxYpv8" | |
| # Chinese Main Menu Keyboard | |
| MAIN_MENU = ReplyKeyboardMarkup( | |
| [ | |
| ["🧹 清除记忆", "💎 会员中心"], | |
| ["💻 编程模式", "🧠 深度思考"], | |
| ["🌐 全网搜索", "❓ 使用帮助"] | |
| ], | |
| resize_keyboard=True | |
| ) | |
| async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| """Send a welcome message with the main menu.""" | |
| user = update.effective_user | |
| await update.message.reply_text( | |
| f"👋 **您好,{user.first_name}!**\n\n" | |
| "我是 **OpenClaw 全能机器人 (Cloud Version)** ☁️\n" | |
| "已成功部署在 Hugging Face 云端,为您提供 24/7 服务!\n\n" | |
| "👇 **请点击下方菜单开始使用**:", | |
| reply_markup=MAIN_MENU, | |
| parse_mode="Markdown" | |
| ) | |
| async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| """Handle incoming text messages.""" | |
| text = update.message.text | |
| if text == "💎 会员中心": | |
| await update.message.reply_text( | |
| "💎 **尊贵的云端 VIP 会员**\n\n" | |
| "✅ **状态**: 永久激活 (Owner)\n" | |
| "⚡️ **权益**: 无限 AI 调用 / 全网搜索\n" | |
| "📅 **有效期**: 2099-12-31\n\n" | |
| "感谢您的支持!", | |
| reply_markup=MAIN_MENU, | |
| parse_mode="Markdown" | |
| ) | |
| elif text == "🧹 清除记忆": | |
| await update.message.reply_text("🧹 **记忆已清除!**\n让我们重新开始。", reply_markup=MAIN_MENU, parse_mode="Markdown") | |
| elif text == "❓ 使用帮助": | |
| await update.message.reply_text( | |
| "❓ **使用指南**\n\n" | |
| "1. **直接聊天**: 我会自动回复。\n" | |
| "2. **全网搜索**: 点击按钮或输入 '搜索 内容'。\n" | |
| "3. **会员中心**: 查看您的 VIP 权益。", | |
| reply_markup=MAIN_MENU, | |
| parse_mode="Markdown" | |
| ) | |
| elif text == "💻 编程模式": | |
| await update.message.reply_text("💻 **编程模式已激活**\n请发送您的代码需求,我会用 Python 为您解答。", reply_markup=MAIN_MENU, parse_mode="Markdown") | |
| elif text == "🧠 深度思考": | |
| await update.message.reply_text("🧠 **深度思考模式已激活**\n适合处理复杂的逻辑推理问题。", reply_markup=MAIN_MENU, parse_mode="Markdown") | |
| elif text == "🌐 全网搜索": | |
| await update.message.reply_text("🔍 请直接输入您想搜索的内容,例如:\n`搜索 2026年冬奥会`", parse_mode="Markdown") | |
| elif text.startswith("搜索 "): | |
| query = text[3:].strip() | |
| if not query: | |
| await update.message.reply_text("🔍 请输入搜索关键词。") | |
| return | |
| await update.message.reply_text(f"🔍 正在全网搜索: **{query}**...", parse_mode="Markdown") | |
| try: | |
| # Use DuckDuckGo Search | |
| with DDGS() as ddgs: | |
| results = list(ddgs.text(query, max_results=5)) | |
| if not results: | |
| await update.message.reply_text("❌ 未找到相关结果。") | |
| return | |
| reply_content = f"🌍 **关于 '{query}' 的搜索结果**:\n\n" | |
| for i, r in enumerate(results, 1): | |
| reply_content += f"{i}. **{r['title']}**\n{r['body']}\n[链接]({r['href']})\n\n" | |
| await update.message.reply_text(reply_content, parse_mode="Markdown", disable_web_page_preview=True) | |
| except Exception as e: | |
| logger.error(f"Search error: {e}") | |
| await update.message.reply_text(f"❌ 搜索失败: {e}") | |
| else: | |
| # Default echo / simple chat response | |
| await update.message.reply_text(f"🤖 **收到消息**: {text}\n(云端 AI 接口正在连接中...)", parse_mode="Markdown") | |
| if __name__ == "__main__": | |
| print("🦞 Starting OpenClaw Bot on Hugging Face Spaces...") | |
| # No proxy needed on HF Spaces | |
| application = Application.builder().token(TELEGRAM_TOKEN).build() | |
| application.add_handler(CommandHandler("start", start)) | |
| application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) | |
| application.run_polling() | |