pythontelegramtelegram-botaiogram

Problems with telegram bot in Python & Aiograme


I have problems with telegram bot in Python. PIP = Aiogram.

My code:

import logging
from aiogram import Bot, Dispatcher, types
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton

# Установка токена бота (замените 'YOUR_BOT_TOKEN' на ваш токен)
TOKEN = 'TOKEN'

# Включаем логгирование
logging.basicConfig(level=logging.INFO)

# Инициализируем бота и диспетчера
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)

# Создаем клавиатуру
menu_markup = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
menu_markup.add(KeyboardButton('Кнопка 1'), KeyboardButton('Кнопка 2'))
menu_markup.add(KeyboardButton('Кнопка 3'), KeyboardButton('Кнопка 4'))
menu_markup.add(KeyboardButton('Кнопка 5'), KeyboardButton('Кнопка 6'))

# Обработчик команды /start
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
    await message.answer("Привет! Выберите опцию:", reply_markup=menu_markup)

# Обработчик нажатия на кнопку
@dp.message_handler(lambda message: message.text in ['Кнопка 1', 'Кнопка 2', 'Кнопка 3', 'Кнопка 4', 'Кнопка 5', 'Кнопка 6'])
async def handle_button_click(message: types.Message):
    # Получаем текст для отправки
    text_to_send = "Текст, который ты хочешь отправить"

    # Создаем клавиатуру для "Проверить оплату"
    check_payment_markup = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
    check_payment_markup.add(KeyboardButton('Проверить Оплату'))

    # Отправляем текст и клавиатуру
    await message.answer(text_to_send, reply_markup=check_payment_markup)

# Обработчик нажатия на кнопку "Проверить Оплату"
@dp.message_handler(lambda message: message.text == 'Проверить Оплату')
async def check_payment(message: types.Message):
    # Отправляем сообщение о том, что оплата не обнаружена
    await message.answer("Оплата не обнаружена")

if __name__ == '__main__':
    from aiogram import executor

    # Запускаем бота
    executor.start_polling(dp, skip_updates=True)

My error:

Traceback (most recent call last):
  File "/Volumes/Files/Telega-Bot(lick)/bot.py", line 13, in <module>
    dp = Dispatcher(bot)
         ^^^^^^^^^^^^^^^
TypeError: Dispatcher.__init__() takes 1 positional argument but 2 were given

I just can’t understand why the problem is, I looked through other forms of the answer and I can’t find it anywhere. How to solve this problem ? Thank you


Solution

  • From the documentation:

    The Dispatcher class no longer accepts a Bot instance in its initializer. Instead, the Bot instance should be passed to the dispatcher only for starting polling or handling events from webhooks. This approach also allows for the use of multiple bot instances simultaneously (“multibot”).

    And here:

    dp = Dispatcher()
    
    async def main():
        bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
        await dp.start_polling(bot)