B
B
B0w12022-03-08 16:50:55
Python
B0w1, 2022-03-08 16:50:55

How to port code from console python to aiogram?

Hello! I ran into a problem, I can not transfer the code from python to aiogram. This script sends a message at a specific time of the day. Please help
Python code:

import schedule
import requests

def greeting():
  todos_dict = {
    '08:00': 'Drink coffee'
  }

  print("Day's tasks")
  for k, v in todos_dict.items():
    print(f'{k} - {v}')

def main():
  #greeting()

  #schedule.every(4).seconds.do(greeting)
  #schedule.every(4).minutes.do(greeting)
  #schedule.every(4).hour.do(greeting)

  schedule.every().day.at('16:27').do(greeting)

  while True:
    schedule.run_pending()

if __name__ == "__main__":
  main()


I tried to convert it to the aiogram library:
import schedule
import requests

async def hello_users():
  await bot.send_message(message.chat.id == CHANNEL_ID, f'Доброе утро, хакеры!')

async def main():
  schedule.every().day.at('16:35').do(hello_users)

  while True:
    schedule.run_pending()

if __name__ == "__main__":
  asyncio.run(main())


Throws an error when the correct time is reached:
C:\Users\sasha\AppData\Local\Programs\Python\Python310\lib\site-packages\schedule\__init__.py:100: RuntimeWarning: coroutine 'hello_users' was never awaited
self._run_job (job)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-03-08
@alex22122

1. Teach how an asynchronous program works, and why a loop like

while True:
    schedule.run_pending()

paralyzes her work.
2. Port your code using asynchronous aioschedule.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question