1 line
3.6 KiB
JSON
1 line
3.6 KiB
JSON
{"repo": "arjun200-coder/-codeyogiassienment-helping-bot", "files": {"bot.py": {"746627147f8556c6192a5e63656da1f0cbad4db3": {"t": 1785674368.120096, "n": 3284, "h": "a425087beb94b8a4", "c": "import os\nfrom assignments.data import assignment_data\nimport logging\nimport requests\nfrom dotenv import load_dotenv\nfrom telegram import Update\nfrom telegram.ext import ApplicationBuilder, MessageHandler, filters, ContextTypes\n\nload_dotenv()\n# print(os.getenv(\"OPENROUTER_API_KEY\"))\nimport requests\n\n\n\nBOT_TOKEN = os.getenv(\"BOT_TOKEN\")\nOPENROUTER_API_KEY = os.getenv(\"OPENROUTER_API_KEY\")\n\nlogging.basicConfig(level=logging.INFO)\n\ndef get_openrouter_response(prompt):\n url = \"https://openrouter.ai/api/v1/chat/completions\"\n headers = {\n \"Authorization\": f\"Bearer {OPENROUTER_API_KEY}\",\n \"Content-Type\": \"application/json\",\n \"HTTP-Referer\": \"https://yourdomain.com\", # optional\n \"X-Title\": \"MyTelegramBot\"\n }\n payload = {\n # \"model\": \"mistralai/mistral-7b-instruct\", \n \"model\": \"meta-llama/llama-3-8b-instruct\",\n \"messages\": [\n {\"role\": \"system\", \"content\": (\n \"You are a smart and friendly assistant who automatically replies in the same language as the user. \"\n \"If the user speaks in English, reply in English. If the user writes in Hindi (Devanagari script), reply in Hindi. \"\n \"If the user types in Hinglish (Hindi written in Roman script), reply in Hinglish. \"\n \"Always match the tone and script of the user. Keep your replies natural, helpful, and short. \"\n \"Do not invent stories, metaphors, or irrelevant examples. Stay focused and reply in a real, human style.\"\n \"NEVER talk about laddoos, dreams, cooking, or boss — unless user actually mentions it. \"\n \"Your tone should always be chill, funny, and helpful — like a close Indian friend. \"\n )},\n {\"role\": \"user\", \"content\": prompt}\n ]\n \n }\n response = requests.post(url, headers=headers, json=payload)\n response.raise_for_status()\n return response.json()[\"choices\"][0][\"message\"][\"content\"]\n\n\nasync def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):\n user_message = update.message.text.strip().lower()\n\n # Check if user asked for a specific assignment like \"assignment 18.1\"\n if \"assignment\" in user_message:\n for key in assignment_data:\n if key in user_message:\n data = assignment_data[key]\n reply = (\n f\"📄 HTML CODE:\\nhtml\\n{data['html']}\\n\\n\"\n f\"🎨 CSS CODE:\\ncss\\n{data['css']}\\n\\n\"\n f\"🧠 JS CODE:\\njs\\n{data['js']}\"\n )\n await update.message.reply_text(reply, parse_mode='Markdown')\n return\n await update.message.reply_text(\"❌ Sorry, assignment code not found.\")\n else:\n # fallback to your chatbot response\n try:\n bot_reply = get_openrouter_response(user_message)\n await update.message.reply_text(bot_reply)\n except Exception as e:\n await update.message.reply_text(\"It's time to make the Assignments!!\")\n\n\nif __name__ == '__main__':\n app = ApplicationBuilder().token(BOT_TOKEN).build()\n app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_message))\n print(\"🤖 Bot is running with OpenRouter API...\")\n app.run_polling()"}}}} |