Spaces:
Sleeping
Sleeping
| import asyncio | |
| import logging | |
| import os | |
| from dotenv import load_dotenv | |
| from telegram import Update | |
| from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes | |
| load_dotenv() | |
| TOKEN = os.environ.get("TELEGRAM_TOKEN") | |
| logging.log(logging.INFO, TOKEN) | |
| # Команды | |
| async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| await update.message.reply_text("Привет! Я простой бот. Напиши что-нибудь, и я повторю!") | |
| async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| await update.message.reply_text("Доступные команды:\n/start - начать общение\n/help - помощь") | |
| # Обработка текстовых сообщений | |
| async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| text = update.message.text | |
| print(f"User ({update.message.chat.id}): {text}") | |
| await update.message.reply_text(f"Вы написали: {text}") | |
| # Ошибки | |
| async def error(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
| print(f"Update {update} caused error {context.error}") | |
| # Запуск бота | |
| async def start_bot(): | |
| print(f"Starting bot...${TOKEN}") | |
| app = Application.builder().token(TOKEN).build() | |
| # Регистрация команд | |
| app.add_handler(CommandHandler("start", start_command)) | |
| app.add_handler(CommandHandler("help", help_command)) | |
| # Регистрация обработчика сообщений | |
| app.add_handler(MessageHandler(filters.TEXT, handle_message)) | |
| # Регистрация обработчика ошибок | |
| app.add_error_handler(error) | |
| # Опрос сервера | |
| print("Polling...") | |
| await app.run_polling(timeout=3) | |
| if __name__ == "__main__": | |
| asyncio.run(start_bot()) # З | |