B
B
black_dis2021-10-08 14:36:11
Python
black_dis, 2021-10-08 14:36:11

How to make achievements in asynchronous telegram bot (aiogram)?

I am writing a bot on aiogram with sqlite3 database. This is my first time with async. For example I have:

@tg.message_handler(commands = ['start'])
async def send_welcome(msg: types.Message):
....
@tg.message_handler(content_types = ['text'])
async def sender( msg: types.Message):
....
@tg.callback_query_handler(lambda call: True)
async def callback_inline(call):
....
And here's how I can write a new function that always checks the user's data from the database, so that in if the desired result was achieved, did she send a message about the completed achievement?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-10-08
@black_dis

Roughly speaking, for an asynchronous program, one statement is true: "while you are executing code that is not an await call, the rest of the program is idle."
Each await call (as well as its completion) is an excuse to switch to another task.
So an infinite loop is just:

while True:
  await asyncio.sleep(5) #пока текущая корутина спит, остальной бот работает
  do_stuff() #пока do_stuff() работает, остальной бот спит

The question is different: is an infinite loop necessary?
I would advise you to start by creating a statistics collection system.
For example, create a table like user id - stat name - stat value.
Let's say "id - number_messages - 100500". And in handlers of corresponding events to do UPDATE on this table. And then, in a separate module, periodically check the stats of users, check if they already have an achievement for this stat, and if not, then give it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question