每分钟调用函数不适用于heroku webhook

f5emj3cl  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(137)

我有一个电报机器人,Heroku上部署.我试图让它检查一个网站上的新交易每分钟.通常我使用这样的东西:

async def check(wait_for):
    print("Debug: check is awaited")
    while True:
        print("Debug: inside while")
        await asyncio.sleep(wait_for)
        print("after sleep")
        transactions = parsing()

我调用的函数

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.create_task(check(30))
    executor.start_polling(dp, skip_updates=True)

循环
但是如果我用webhook开始代码替换executor.start_polling

start_webhook(
    dispatcher=dp,
    webhook_path=WEBHOOK_PATH,
    on_startup=on_startup,
    on_shutdown=on_shutdown,
    skip_updates=True,
    host=WEBAPP_HOST,
    port=WEBAPP_PORT
)

Check function打印“Debug:等待检查”和“调试:inside while”,然后webhook启动,函数停止工作。如何解决这个问题?

dauxcl2d

dauxcl2d1#

创建自己的start_webhook函数,如下所示:

from aiogram.utils.executor import set_webhook
from aiogram.dispatcher.webhook import DEFAULT_ROUTE_NAME

def start_webhook(dispatcher, webhook_path, *, loop=None, skip_updates=None, on_startup=None, on_shutdown=None, check_ip=False, retry_after=None, route_name=DEFAULT_ROUTE_NAME, **kwargs):
    executor = set_webhook(dispatcher=dispatcher,
                           webhook_path=webhook_path,
                           loop=loop,
                           skip_updates=skip_updates,
                           on_startup=on_startup,
                           on_shutdown=on_shutdown,
                           check_ip=check_ip,
                           retry_after=retry_after,
                           route_name=route_name)
    executor.run_app(loop=loop, **kwargs)

并将'loop'转发到'start_webhook':

start_webhook(
    loop=loop,
    dispatcher=dp,
    webhook_path=WEBHOOK_PATH,
    on_startup=on_startup,
    on_shutdown=on_shutdown,
    skip_updates=True,
    host=WEBAPP_HOST,
    port=WEBAPP_PORT
)

相关问题