پرش به محتوای اصلی
دانشگاه هوش مصنوعی — Daneshgah AI
برنامه‌نویسی و اتوماسیونخروجی: متنسطح: پیشرفته

پرامپت ساخت ربات تلگرام با قابلیت پاسخ‌دهی خودکار

این پرامپت، کد یک ربات تلگرام را تولید می‌کند که پیام‌ کاربران را به‌طور خودکار تحلیل و پاسخ مناسب ارسال می‌کند.

```python import os import re from telegram import Update from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, ContextTypes, filters # Securely read the bot token from an environment variable TELEGRAM_TOKEN = os.environ.get('TELEGRAM_TOKEN') # Set your bot token as an environment variable # Sample event data (could be loaded from a database or an API in production) EVENT_SCHEDULE = [ {'name': 'Python Meetup', 'date': '2024-07-10', 'time': '18:00'}, {'name': 'AI Conference', 'date': '2024-07-15', 'time': '09:00'}, {'name': 'Hackathon', 'date': '2024-07-22', 'time': '12:00'} ] def format_event_list(): """Formats the event schedule into a readable string.""" result = "Upcoming Events:\n" for event in EVENT_SCHEDULE: result += f"- {event['name']}: {event['date']} at {event['time']}\n" return result def is_schedule_query(text: str) -> bool: """Detects if user message is a schedule query.""" pattern = r'(event[s]?|schedule|date[s]?|time[s]?|upcoming)' # simple keyword detection return re.search(pattern, text, re.IGNORECASE) is not None async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): """Handles incoming messages and replies if it's a schedule query.""" user_text = update.message.text if is_schedule_query(user_text): reply = format_event_list() else: reply = "Hi! Ask me about event schedules." await update.message.reply_text(reply) async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): await update.message.reply_text('Welcome! Ask me about upcoming event dates and times.') if __name__ == '__main__': if not TELEGRAM_TOKEN: raise EnvironmentError('TELEGRAM_TOKEN environment variable not set.') app = ApplicationBuilder().token(TELEGRAM_TOKEN).build() app.add_handler(CommandHandler('start', start)) app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) # Start the bot app.run_polling() ``` --- # Deployment & Running 1. Install requirements: ``` pip install python-telegram-bot==20.* ``` 2. Set your Telegram bot token as an environment variable: ``` export TELEGRAM_TOKEN=your-bot-token-goes-here ``` 3. Save the script (e.g., `bot.py`) and run: ``` python bot.py ``` # Notes - Never hardcode your token. Use environment variables as shown. - For production, use a database instead of the sample event list. - You can host this bot locally or on platforms like Heroku, railway, etc.

نمونه خروجی همین پرامپت — ساخته‌شده با ChatGPT (gpt-4.1). نتیجه‌ی شما با ابزار و تنظیمات دیگر می‌تواند متفاوت باشد.

متن پرامپت

Act as a senior Python developer. Write complete Python code for a Telegram bot that automatically replies to user messages about #subject. The bot should connect via the Telegram Bot API, read incoming messages, analyze user queries, and send relevant, helpful responses. Include detailed comments and easy-to-follow deployment instructions. Make sure the code uses best practices for security (such as environment variables for the bot token) and is compatible with the latest version of the python-telegram-bot library. Explain each step briefly after the code.
نسخه‌ی پرشده که نمونه‌ی بالا با آن ساخته شد
Act as a senior Python developer. Write complete Python code for a Telegram bot that automatically replies to user messages about event schedules. The bot should connect via the Telegram Bot API, read incoming messages, detect if users ask about dates/times of upcoming events, and send a relevant response. Include clear comments, and explain how to deploy and run it easily. Requirements: compatible with python-telegram-bot library, secure token handling, concise documentation.

پرامپت را برای خودتان پر کنید

جاهای خالی را به فارسی پر کنید؛ پرامپت آماده‌ی کپی پایین ساخته می‌شود.

ویژه‌ی VIP: همین پرامپت را در خود سایت اجرا کنید و جواب کامل را به فارسی بگیرید. ورود به حساب

کاربرد پرامپت

این پرامپت امکان تولید سریع و هوشمند یک ربات تلگرام را فراهم می‌کند که به‌طور خودکار به پیام کاربران پاسخ می‌دهد. کافی است موضوع و قابلیت دلخواه را تعیین کنید تا کد کامل، مستند و قابل اجرا برای شما تولید شود. این پرامپت مخصوص کاربرانی است که می‌خواهند بدون تجربه‌ی عمیق برنامه‌نویسی، یک ربات تعاملی تلگرام بسازند یا ایده‌ی خود را با سرعت به نمونه اولیه تبدیل نمایند.

مزایا

  • امکان شخصی‌سازی کامل موضوع و رفتار ربات
  • دریافت کد مستند و آماده اجرا حتی برای مبتدیان
  • استفاده از کتابخانه‌های به‌روز و امن مانند python-telegram-bot
  • صرفه‌جویی چشمگیر در زمان توسعه و یادگیری عملی برنامه‌نویسی

مخاطبان هدف

  • ادمین‌های کانال یا گروه تلگرام نیازمند ربات پاسخگو
  • برنامه‌نویسان مبتدی تا متوسط
  • استارتاپ‌ها و کسب‌وکارهای کوچک برای ارتباط سریع با مشتری
  • مدرسین یا دانش‌آموزانی که می‌خواهند پروژه ربات ارائه دهند

نحوه استفاده

در این پرامپت باید نقش (مثلاً senior Python developer)، موضوع یا عملکرد ربات (مثل پرسش درباره زمان رویدادها)، تنظیمات خاص (مثلاً استفاده از کتابخانه خاص)، و جزئیات راه‌اندازی را بنویسید.

مثال‌ها:

  • "...for a Telegram bot that manages simple polls" (ربات برای نظرسنجی ساده)
  • "...to help users find tickets for local concerts" (ربات جستجوی بلیت کنسرت)
  • "...for customer FAQ auto-reply" (پاسخگو به سوالات متداول)
  • "...in JavaScript using telegraf.js library" (بلد بودن به جاوااسکریپت به‌جای پایتون)

در اجرای خروجی، می‌توانید نسبت ۱۶:۹ یا ۹:۱۶ را انتخاب نکنید چون این پرامپت متنی است.

نکته‌های حرفه‌ای

  • اگر می‌خواهید ربات تعامل پیچیده‌تری داشته باشد، ویژگی‌های بیشتری مثل دکمه، ارسال عکس یا اتصال به پایگاه داده را در پرامپت ذکر کنید.
  • برای افزایش امنیت، تأکید کنید که راهنمای مخفی کردن توکن در محیط اجرا یا استفاده از environment variables را هم توضیح دهد.
  • اگر نیاز به نصب ربات روی سرور خاص (مثل Heroku) دارید، این خواسته را صریح بنویسید.

پرسش‌های متداول درباره این ویدیو

ChatGPT، Gemini یا Claude همگی مناسب هستند؛ ChatGPT نسخه ۴ یا بالاتر ترجیح دارد.

پرامپت‌های مشابه