T
T
tupobarl2022-04-13 14:02:47
Python
tupobarl, 2022-04-13 14:02:47

How to rewrite code from telebot to aiogram?

Hello. Rewrote the code from telebot to aiogram.
Code on telebot (working):

import telebot
from telebot import types

bot = telebot.TeleBot('api')

@bot.message_handler(commands=['start'])
def start(message):
    bot.send_message(message.chat.id,
    f'''
<b>Привет, {message.from_user.first_name}!
Добро пожаловать в Potato Team!</b>''', parse_mode = 'html', reply_markup = start_menu)


start_menu = types.InlineKeyboardMarkup(row_width=2)
start_menu.add(
    types.InlineKeyboardButton(text='Помощь', callback_data='help'))


@bot.callback_query_handler(func=lambda call: True)
def help(call):
    if call.data == 'help':
        bot.send_message(call.message.chat.id, f'''
    <b>Админ/Тех.Поддержка: @barltg</b>''', parse_mode = 'html', reply_markup = backtomenu)
    elif call.data == 'back':
        bot.send_message(call.message.chat.id,
    f'''
<b>Привет, {call.from_user.first_name}!
Добро пожаловать в Potato Team!</b>''', parse_mode = 'html', reply_markup = start_menu)


backtomenu = types.InlineKeyboardMarkup(row_width=2)
backtomenu.add(
    types.InlineKeyboardButton(text='Назад', callback_data='back'))

print("Успешный запуск!")

bot.polling(none_stop = True)


Code for aiogram (rewritten by me, with errors):
import logging
from aiogram import Bot, Dispatcher, executor, types

bot = Bot(token = 'api' )

logging.basicConfig(level=logging.INFO)

dp = Dispatcher(bot)
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
    await message.answer(f'''
<b>Привет, {message.from_user.first_name}!
Добро пожаловать в Potato Team!</b>''', parse_mode = 'html', reply_markup=start_menu)

start_menu = types.InlineKeyboardMarkup(row_width=2)
start_menu.add(
    types.InlineKeyboardButton(text='Помощь', callback_data='help'))

back = types.InlineKeyboardMarkup(row_width=2)
back.add(types.InlineKeyboardButton(text='Назад', callback_data='back'))

@dp.callback_query_handler(lambda call: True)
async def help(call: types.CallbackQuery):
  if call.back == 'help':
    await message.answer(f'''
<b>Админ/Тех.Поддержка - @barltg</b>''', parse_mode = 'html', reply_markup=start_menu)
  elif call.back == 'back':
    await message.answer(call.message.chat.id, f'''
<b>Привет, {message.from_user.first_name}!
Добро пожаловать в Potato Team!</b>''', parse_mode = 'html',reply_markup=start_menu)


if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)

The error itself:
File "<string>", line 24, in help
AttributeError: 'CallbackQuery' object has no attribute 'back'

How to fix it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Nagibovich, 2022-04-13
@nki

Correct
CallbackQuery
spelling Pay attention to the case of letters.

S
soremix, 2022-04-13
@SoreMix

CallbackQuery
Case is incorrect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question