اصنع لي كود بلغة بايثون لبوت تليكرام استضافة بايثون ،
Browse files- editor.html +4 -1
- index.html +2 -1
- telegram_bot.py +107 -0
editor.html
CHANGED
|
@@ -67,7 +67,10 @@
|
|
| 67 |
<a href="/" class="text-gray-500 hover:text-undefined-700 flex items-center gap-1">
|
| 68 |
<i data-feather="arrow-left" class="h-4 w-4"></i> Back to Home
|
| 69 |
</a>
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
</div>
|
| 72 |
</div>
|
| 73 |
</nav>
|
|
|
|
| 67 |
<a href="/" class="text-gray-500 hover:text-undefined-700 flex items-center gap-1">
|
| 68 |
<i data-feather="arrow-left" class="h-4 w-4"></i> Back to Home
|
| 69 |
</a>
|
| 70 |
+
<a href="https://t.me/your_bot_username" target="_blank" class="text-gray-500 hover:text-undefined-700 flex items-center gap-1">
|
| 71 |
+
<i data-feather="send" class="h-4 w-4"></i> Telegram Bot
|
| 72 |
+
</a>
|
| 73 |
+
</div>
|
| 74 |
</div>
|
| 75 |
</div>
|
| 76 |
</nav>
|
index.html
CHANGED
|
@@ -100,7 +100,8 @@
|
|
| 100 |
<a href="#" class="nav-link text-gray-500 hover:text-undefined-700 px-3 py-2 rounded-md text-sm font-medium">Pricing</a>
|
| 101 |
<a href="#" class="nav-link text-gray-500 hover:text-undefined-700 px-3 py-2 rounded-md text-sm font-medium">Docs</a>
|
| 102 |
<a href="#" class="nav-link text-gray-500 hover:text-undefined-700 px-3 py-2 rounded-md text-sm font-medium">About</a>
|
| 103 |
-
|
|
|
|
| 104 |
</div>
|
| 105 |
<div class="hidden md:block">
|
| 106 |
<div class="ml-4 flex items-center md:ml-6">
|
|
|
|
| 100 |
<a href="#" class="nav-link text-gray-500 hover:text-undefined-700 px-3 py-2 rounded-md text-sm font-medium">Pricing</a>
|
| 101 |
<a href="#" class="nav-link text-gray-500 hover:text-undefined-700 px-3 py-2 rounded-md text-sm font-medium">Docs</a>
|
| 102 |
<a href="#" class="nav-link text-gray-500 hover:text-undefined-700 px-3 py-2 rounded-md text-sm font-medium">About</a>
|
| 103 |
+
<a href="https://t.me/your_bot_username" target="_blank" class="nav-link text-gray-500 hover:text-undefined-700 px-3 py-2 rounded-md text-sm font-medium">Telegram Bot</a>
|
| 104 |
+
</div>
|
| 105 |
</div>
|
| 106 |
<div class="hidden md:block">
|
| 107 |
<div class="ml-4 flex items-center md:ml-6">
|
telegram_bot.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```python
|
| 2 |
+
import os
|
| 3 |
+
import logging
|
| 4 |
+
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
| 5 |
+
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, CallbackQueryHandler
|
| 6 |
+
import subprocess
|
| 7 |
+
|
| 8 |
+
# Enable logging
|
| 9 |
+
logging.basicConfig(
|
| 10 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
|
| 11 |
+
)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
# Bot token from environment variable
|
| 15 |
+
TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
| 16 |
+
|
| 17 |
+
def start(update: Update, context: CallbackContext) -> None:
|
| 18 |
+
"""Send a message when the command /start is issued."""
|
| 19 |
+
user = update.effective_user
|
| 20 |
+
keyboard = [
|
| 21 |
+
[InlineKeyboardButton("Run Python Code", callback_data='run_code')],
|
| 22 |
+
[InlineKeyboardButton("Help", callback_data='help')]
|
| 23 |
+
]
|
| 24 |
+
reply_markup = InlineKeyboardMarkup(keyboard)
|
| 25 |
+
update.message.reply_text(
|
| 26 |
+
f'Hi {user.first_name}! I am PythonSandbox Bot.\n'
|
| 27 |
+
'I can execute Python code in a secure sandbox environment.',
|
| 28 |
+
reply_markup=reply_markup
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def help_command(update: Update, context: CallbackContext) -> None:
|
| 32 |
+
"""Send a message when the command /help is issued."""
|
| 33 |
+
update.message.reply_text(
|
| 34 |
+
'Send me Python code and I will execute it for you!\n\n'
|
| 35 |
+
'Commands:\n'
|
| 36 |
+
'/start - Start the bot\n'
|
| 37 |
+
'/help - Show this help message\n\n'
|
| 38 |
+
'How to use:\n'
|
| 39 |
+
'1. Send your Python code directly\n'
|
| 40 |
+
'2. Or use the inline buttons to interact'
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def handle_code(update: Update, context: CallbackContext) -> None:
|
| 44 |
+
"""Handle incoming Python code messages."""
|
| 45 |
+
code = update.message.text
|
| 46 |
+
|
| 47 |
+
# Save code to temporary file
|
| 48 |
+
with open('temp.py', 'w') as f:
|
| 49 |
+
f.write(code)
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
# Execute the code in a secure environment
|
| 53 |
+
result = subprocess.run(
|
| 54 |
+
['python', 'temp.py'],
|
| 55 |
+
capture_output=True,
|
| 56 |
+
text=True,
|
| 57 |
+
timeout=10
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if result.returncode == 0:
|
| 61 |
+
output = result.stdout if result.stdout else "Code executed successfully (no output)"
|
| 62 |
+
else:
|
| 63 |
+
output = f"Error:\n{result.stderr}"
|
| 64 |
+
|
| 65 |
+
update.message.reply_text(f"Output:\n```\n{output}\n```", parse_mode='Markdown')
|
| 66 |
+
|
| 67 |
+
except subprocess.TimeoutExpired:
|
| 68 |
+
update.message.reply_text("Execution timed out (10s limit)")
|
| 69 |
+
except Exception as e:
|
| 70 |
+
update.message.reply_text(f"An error occurred: {str(e)}")
|
| 71 |
+
finally:
|
| 72 |
+
# Clean up
|
| 73 |
+
if os.path.exists('temp.py'):
|
| 74 |
+
os.remove('temp.py')
|
| 75 |
+
|
| 76 |
+
def button(update: Update, context: CallbackContext) -> None:
|
| 77 |
+
"""Handle button presses."""
|
| 78 |
+
query = update.callback_query
|
| 79 |
+
query.answer()
|
| 80 |
+
|
| 81 |
+
if query.data == 'run_code':
|
| 82 |
+
query.edit_message_text("Please send me your Python code to execute.")
|
| 83 |
+
elif query.data == 'help':
|
| 84 |
+
help_command(update, context)
|
| 85 |
+
|
| 86 |
+
def main() -> None:
|
| 87 |
+
"""Start the bot."""
|
| 88 |
+
updater = Updater(TOKEN)
|
| 89 |
+
dispatcher = updater.dispatcher
|
| 90 |
+
|
| 91 |
+
# Command handlers
|
| 92 |
+
dispatcher.add_handler(CommandHandler("start", start))
|
| 93 |
+
dispatcher.add_handler(CommandHandler("help", help_command))
|
| 94 |
+
|
| 95 |
+
# Message handler for Python code
|
| 96 |
+
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_code))
|
| 97 |
+
|
| 98 |
+
# Button handler
|
| 99 |
+
dispatcher.add_handler(CallbackQueryHandler(button))
|
| 100 |
+
|
| 101 |
+
# Start the Bot
|
| 102 |
+
updater.start_polling()
|
| 103 |
+
updater.idle()
|
| 104 |
+
|
| 105 |
+
if __name__ == '__main__':
|
| 106 |
+
main()
|
| 107 |
+
```
|