Spaces:
Sleeping
Sleeping
import os | |
from telegram import Update | |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes | |
TOKEN = os.environ["TELEGRAM_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("Starting bot...") | |
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(polling_interval=3) |