Getting an error in bot using Aiogram in Python

232 views Asked by At

I have a bot on aiogram. It is on a vps server. After 5-10 hours it starts giving the following error:

2023-11-24 07:01:42,847 - [ERROR] - aiogram.dispatcher - (dispatcher.py)._listen_updates(218) 
  - Failed to fetch updates - TelegramConflictError: Telegram server says - Conflict: can't use getUpdates method while webhook is active; use deletewebhook to delete the webhook first 
2023-11-24 07:01:42,847 - [WARNING] - aiogram.dispatcher - (dispatcher.py)._Listen_updates(220) 
  - Sleep for 5.017048 seconds and try again… (tryings = 12641, bot id=***)`

In the file run.py is all the basic, I don’t understand why there is an error, although I used the method:

await bot.delete_webhook(drop_pending_updates=True)`

In all other bots I don't have this problem and I don't understand why.

Here's the run.py

import asyncio
import logging
import os

from aiogram import Bot, Dispatcher, F, Router
from aiogram.filters import Command
from aiogram.fsm.storage.memory import MemoryStorage
from dotenv import load_dotenv
from utils.commands import set_commands

from user.user_handler import (start_handler, Form, start_of_recording_count, start_of_recording_height_call,
                               start_of_recording_material_call, start_of_recording_address, start_of_recording_phone,
                               start_of_recording_date, start_of_recording_sealing_call,
                               start_of_recording_zapil_quantity_k, start_of_recording_zapil_quantity_v,
                               start_of_recording_sealing_up, start_of_recording_sealing_uniq,
                               start_of_recording_freight_elevator_callback)
from user.user_callback import (about_info_callback, main_menu_return_callback, contacts_callback,
                                services_list_callback, price_list_callback, sign_up_callback)
from request.sql_request import create_table

router = Router()


async def start_bot(bot: Bot):
    load_dotenv()
    await set_commands(bot)
    await create_table()
    await bot.send_message(os.getenv("ADMIN_ID"), text='Бот запущен!')


async def main() -> None:
    load_dotenv()
    logging.basicConfig(level=logging.INFO,
                        format="%(asctime)s - [%(levelname)s] - %(name)s - "
                               "(%(filename)s).%(funcName)s(%(lineno)d) - %(message)s")

    dp = Dispatcher(storage=MemoryStorage())
    dp.include_router(router)
    bot = Bot(os.getenv("TOKEN"), parse_mode="HTML")

    dp.startup.register(start_bot)

    dp.message.register(start_handler, Command('start'))

    dp.message.register(start_of_recording_count, Form.start_of_recording_count)
    dp.message.register(start_of_recording_address, Form.start_of_recording_address)
    dp.message.register(start_of_recording_phone, Form.start_of_recording_phone)
    dp.message.register(start_of_recording_date, Form.start_of_recording_date)

    dp.message.register(start_of_recording_zapil_quantity_v, Form.start_of_recording_zapil_quantity_v)
    dp.message.register(start_of_recording_zapil_quantity_k, Form.start_of_recording_zapil_quantity_k)

    dp.message.register(start_of_recording_sealing_up, Form.start_of_recording_sealing_up)
    dp.message.register(start_of_recording_sealing_uniq, Form.start_of_recording_sealing_uniq)

    dp.callback_query.register(start_of_recording_freight_elevator_callback, F.data.startswith('yes_freight_elevator'))
    dp.callback_query.register(start_of_recording_freight_elevator_callback, F.data.startswith('no_freight_elevator'))

    dp.callback_query.register(start_of_recording_zapil_quantity_v, F.data.startswith('zapil_v'))
    dp.callback_query.register(start_of_recording_zapil_quantity_v, F.data.startswith('zapil_k'))
    dp.callback_query.register(start_of_recording_zapil_quantity_v, F.data.startswith('zapil_m'))
    dp.callback_query.register(start_of_recording_zapil_quantity_v, F.data.startswith('zapil_master'))

    dp.callback_query.register(start_of_recording_sealing_call, F.data.startswith('sealing_up'))
    dp.callback_query.register(start_of_recording_sealing_call, F.data.startswith('sealing_uniq'))
    dp.callback_query.register(start_of_recording_sealing_call, F.data.startswith('sealing_master'))

    dp.callback_query.register(about_info_callback, F.data.startswith('about_info'))
    dp.callback_query.register(main_menu_return_callback, F.data.startswith('main_menu_return'))
    dp.callback_query.register(contacts_callback, F.data.startswith('contacts'))
    dp.callback_query.register(services_list_callback, F.data.startswith('services_list'))
    dp.callback_query.register(price_list_callback, F.data.startswith('price_list'))
    dp.callback_query.register(sign_up_callback, F.data.startswith('sign_up'))

    dp.callback_query.register(start_of_recording_material_call, F.data.startswith('mdf_choices'))
    dp.callback_query.register(start_of_recording_material_call, F.data.startswith('duropolimer_choices'))
    dp.callback_query.register(start_of_recording_material_call, F.data.startswith('shadow_choices'))
    dp.callback_query.register(start_of_recording_material_call, F.data.startswith('shopinrovan_choices'))

    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('80'))
    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('90'))
    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('100'))
    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('110'))
    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('120'))
    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('130'))
    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('140'))
    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('150'))
    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('160'))
    dp.callback_query.register(start_of_recording_height_call, F.data.startswith('170'))

    await bot.delete_webhook(drop_pending_updates=True)
    await dp.start_polling(bot)


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

I used the following but it still doesn't work:

await bot.delete_webhook(drop_pending_updates=True)
0

There are 0 answers