A
A
Alexander2020-11-28 09:55:59
Python
Alexander, 2020-11-28 09:55:59

Why is the bot installed on the server sending a long message?

Faced the problem of delaying the sending message of the bot installed on the server. The bot is written on the aiogram library to work with the database using gino, there is the following piece of code:

spoiler

@dp.callback_query_handler(lambda c: c.data and c.data.startswith('bet-list'))
async def bet_list_page_handler(callback_query: types.CallbackQuery):
    chat_id = callback_query.message.chat.id
    page_number = int(callback_query.data.split('-')[-1])
    chat = await Chat.query.where(Chat.chat_id == chat_id).gino.first()

    ###
    now = datetime.utcnow()
    ###

    if chat is not None:
        await bot.delete_message(chat_id, callback_query.message.message_id)
        rnd = await Round.query.where(Round.chat_id == chat.id).gino.first()

        if rnd is None:
            msg = '‍♂️ Раунд еще не начался!'
            keyboard = None
        else:
            players = await Player.query.where(Player.p_round == rnd.id).gino.all()
            prev_page, next_page, players = await get_players_page(players, page_number)

            if not players:
                msg = ' Ставок еще нет!'
                keyboard = None
                m = await bot.send_message(chat_id)
                await Message.create(message_id=m.message_id,
                                     chat_id=chat.id, m_type='LIST')
            else:
                msg = ' Список ставок:\n\n'

                for i, player in enumerate(players, start=(10 * page_number) - 9):
                    user = await User.get(player.user)
                    msg += f'{i}. @{user.username} - <b>${player.rate}</b>\n'

                keyboard = await keyboard_list(prev_page, next_page, page_number)

        await send_message(chat_id, chat.id, msg, 'LIST', reply_markup=keyboard)

        ###
        print((datetime.utcnow() - now).seconds, 'seconds')
        ###



I run a bot on the server, I click on the button that activates this function, I look in the console, the message 0 seconds appears almost instantly (after clicking). That is, as I understand it, the code is executed quickly, but the telegram itself sends a message with a delay of 3-10 seconds.
I looked through htop the load on the processor when the bot button was pressed in tg, the load shows 3-5%, RAM 230mb out of 900mb is used.

On the local computer, messages arrive instantly. What could be the problem? Perhaps I'm not measuring the speed correctly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question