B
B
Barl2022-04-20 23:49:20
Python
Barl, 2022-04-20 23:49:20

How to solve aiogram.utils.exceptions.BotBlocked: Forbidden: bot was blocked by the user aiogram?

I wanted to register a mailing function using my database in a txt file, but it gives an error in the header

async def check_user():
    f = open('users.txt')
    users = []
    for line in f:
        users.append(line.replace('\n', ''))
    return users

@dp.callback_query_handler(IsAdmin(), text='mail')
async def get_users_mail(call: types.CallbackQuery):
    await bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=f'''<b>Введите сообщение для рассылки:</b>''', reply_markup=cancel)
    await MessageMail.message.set()

@dp.message_handler(IsAdmin(), state=MessageMail.message)
async def send_users_mail(message: types.Message, state: FSMContext):
    users = await check_user()
    for i in users:
        try:
            await bot.send_message(i, message.text)
            await message.answer("<b>Рассылка завершена!</b>")
            await message.answer(f'''<b>Админ панель.
Доступные команды:</b>''', reply_markup=admin_menu)
        except BotBlocked:
            pass

    await state.finish()


And I even wrote try: except: and imported from aiogram.utils.exceptions import BotBlocked. But the error is the same. How to solve it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2022-04-21
@justbarl

the message about the completion of the mailing and the output of the admin panel should be taken out of the try block and the loop, respectively.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question