You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加ConversationHandler后异步Telegram Bot出现协程未等待的RuntimeWarning问题求助

添加ConversationHandler后异步Telegram Bot出现协程未等待的RuntimeWarning问题求助

大家好,我最近在给我的异步Telegram Bot添加ConversationHandler之后,整个程序就出问题了,现在运行时会抛出RuntimeWarning,提示协程没被等待。我实在找不到问题出在哪,麻烦各位帮我看看!

我的代码如下:

# 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]

运行后出现的警告信息如下:

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

我之前没添加ConversationHandler的时候,程序还能正常运行,加了之后就出现这些警告了。有没有大佬能帮我排查下问题所在?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 08:49:31