A
A
Alexey Mikhalev2020-07-16 18:47:53
Python
Alexey Mikhalev, 2020-07-16 18:47:53

What can 'Bot' object has no attribute 'message_handler' fix?

I am writing a bot, learning to work with a database. At the moment, the purpose of the code is this: The user enters a command, and the bot adds the id to the database.

# инициализируем бота
bot = Bot(token=config.TOKEN)
dp = Dispatcher(bot)

# инициализируем соединение с БД
db = SQLighter('database.db')

@dp.message_handler(commands=['admins'])
async def subscribe(message: types.Message):
    if(not db.subscriber_exists(message.from_user.id)):
        # если юзера нет в базе, добавляем его
        db.add_subscriber(message.from_user.id)
    else:
        # если он уже есть, то просто обновляем ему статус подписки
        db.update_subscription(message.from_user.id, True)
    
    await message.answer("Вы успешно добавлены в базу")
 
@bot.message_handler(commands=['start'])
def welcome(message):
      .   .   .


After starting, an error appears in the console:
Traceback (most recent call last):
  File "bot.py", line 31, in <module>
    @bot.message_handler(commands=['start'])
AttributeError: 'Bot' object has no attribute 'message_handler'


Because of what this can happen and how to avoid the error so that everything works?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-07-16
@mikkhalev

@dp.message_handler vs @bot.message_handler
would have learned in regular python, not async, otherwise you don’t even understand this

T
To_Anton, 2021-12-11
@To_Anton

@dp.message_handler and @bot.message_handler are different words. Email @dp.message_handler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question