[Python] Python Async Problem and ConversationHandler

Discussão em 'Python' iniciado por Stack, Junho 19, 2025.

  1. Stack

    Stack Membro Participativo

    main*

    async def main():
    app = ApplicationBuilder().token(TOKEN).build()
    for handler in handlers:
    app.add_handler(handler)
    print("Бот запущен!")
    await app.run_polling() # Запуск polling

    if __name__ == "__main__":
    try:
    asyncio.run(main())
    except Exception as e:
    print(f"Ошибка: {e}")


    handlers*

    CITY,TIME=range(2)


    async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user_name=update.effective_user.first_name
    await update.message.reply_text(f"Привет {user_name}! \n"
    f"Это бот ,который будет уведомлять вас каждое утро о погоде на день \n"
    f"В каком городе вы живете?")
    return CITY


    async def city_input(update: Update, context: ContextTypes.DEFAULT_TYPE):
    city=update.message.text
    await update.message.reply_text(f"Отлично! Вы живете в городе {city}, прекрасное место!\n"
    f"В какое время отправлять прогноз на день(6,12,18 часов и полночь)?\n"
    f"Пожалуйста отправьте в формате ЧЧ")
    return TIME

    async def time_input(update: Update, context: ContextTypes.DEFAULT_TYPE):
    time=update.message.text
    await update.message.reply_text(f"Отлично! Я буду присылать вам уведомление в {time}")
    return ConversationHandler.END

    async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
    await update.message.reply_text(
    "Диалог отменен. Если хотите начать снова, введите /start.",
    reply_markup=None,
    )
    return ConversationHandler.END
    conv_handlers=ConversationHandler(
    entry_points=[CommandHandler("start",start)],
    states={
    CITY:[MessageHandler(filters.TEXT & ~filters.COMMAND,city_input)],
    TIME:[MessageHandler(filters.TEXT & ~filters.COMMAND,time_input)]
    },
    fallbacks=[CommandHandler("cancel", cancel), MessageHandler(filters.ALL, cancel)]
    )
    handlers=[conv_handlers]


    when I added ConversationHandler, everything broke and now it gives an error. sys:1: RuntimeWarning: coroutine 'Application.shutdown' was never awaited RuntimeWarning: Enable tracemalloc to get the object allocation traceback sys:1: RuntimeWarning: coroutine 'Application.bootstrap_initialize' was never awaited RuntimeWarning: Enable tracemalloc to get the object allocation traceback help pls:__(

    Continue reading...

Compartilhe esta Página