我怎样才能摆脱电报机器人循环 application.run_polling()?

问题描述 投票:0回答:1
def bot_start():
    application = ApplicationBuilder().token("api_key").build()


    async def stop(update, context):
        await context.bot.send_message(chat_id=update.message.chat_id, text='Terminating Bot...')
        await application.stop()
        await Updater.shutdown(application.bot)
        await application.shutdown()

    async def error(update, context):
        err = f"Update: {update}\nError: {context.error}"
        logging.error(err, exc_info=context.error)
        await context.bot.send_message(chat_id=user_id, text=err)

   application.add_handler(CommandHandler('stop', stop))
   application.add_error_handler(error)

   application.run_polling()

我尽我所能阻止它,但我不能,因为它不允许调用 bot_start() 后出现的其他代码行运行。它基本上永远不会到达他们。

python telegram python-telegram-bot
1个回答
1
投票

Application.run_polling
是一种方便的方法,可以启动一切并保持机器人运行,直到您发出进程关闭信号。如果
Application
是您的 python 进程中唯一长时间运行的东西,则主要使用它。如果你想在你的机器人旁边运行其他东西,你可以手动调用
run_polling
的文档中列出的方法。您可能还想看看this example,其中展示了使用自定义 webhook 服务器的设置,而不是内置的 PTB。


免责声明:我目前是

python-telegram-bot
.

的维护者
© www.soinside.com 2019 - 2024. All rights reserved.