Spaces:
Runtime error
Runtime error
| import subprocess | |
| from telegram import Update | |
| from telegram.ext import ApplicationBuilder, MessageHandler, CommandHandler, filters, ContextTypes | |
| import nest_asyncio | |
| import random | |
| nest_asyncio.apply() | |
| token = "7133435346:AAFTUXPpxGs9KXy5WZYtkzeCAdpz1MtxVbI" # Your Telegram Bot Token | |
| last_step = {} | |
| async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |
| await update.message.reply_text("Halo! Saya adalah bot untuk menghasilkan sampiran dan isi pantun secara otomatis. Silakan gunakan perintah /sampiran atau /isi untuk memulai.") | |
| async def generate_sampiran(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |
| text_lines = update.message.text.split("\n") | |
| baris1,baris2 = text_lines | |
| if len(text_lines) == 2: | |
| try: | |
| response = 'ini mau bikin sampiran' | |
| except subprocess.CalledProcessError as e: | |
| response = f"Error: {e}" # Tangani kesalahan jika skrip gagal dijalankan | |
| else: | |
| response = "Mohon masukkan dua baris teks." | |
| await update.message.reply_text(response) | |
| async def generate_isi(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |
| text_lines = update.message.text.split("\n") | |
| baris1,baris2 = text_lines | |
| if len(text_lines) == 2: | |
| try: | |
| response = 'ini mau bikin isi' | |
| except subprocess.CalledProcessError as e: | |
| response = f"Error: {e}" # Tangani kesalahan jika skrip gagal dijalankan | |
| else: | |
| response = "Mohon masukkan dua baris teks." | |
| await update.message.reply_text(response) | |
| async def sampiran_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |
| global last_step | |
| last_step[update.message.from_user.id] = 'sampiran' # Update the last step for the user | |
| await update.message.reply_text("Silakan masukkan dua baris isi nanti akan saya berikan sampiran.") | |
| async def isi_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |
| global last_step | |
| last_step[update.message.from_user.id] = 'isi' # Update the last step for the user | |
| await update.message.reply_text("Silakan masukkan dua baris sampiran nanti akan saya berikan isi.") | |
| async def handle_text(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |
| global last_step | |
| user_id = update.message.from_user.id | |
| if user_id in last_step: | |
| if last_step[user_id] == 'sampiran': | |
| await generate_sampiran(update, context) | |
| elif last_step[user_id] == 'isi': | |
| await generate_isi(update, context) | |
| del last_step[user_id] # Remove the user from the tracking after processing | |
| else: | |
| await update.message.reply_text("Mohon gunakan perintah /sampiran atau /isi terlebih dahulu.") | |
| app = ApplicationBuilder().token(token).build() | |
| app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_text)) | |
| app.add_handler(CommandHandler("start", start_command)) | |
| app.add_handler(CommandHandler("sampiran", sampiran_command)) | |
| app.add_handler(CommandHandler("isi", isi_command)) | |
| app.run_polling() |