Home>

I wrote the code on telebot, but then I decided to rewrite it on aiogram. And ran into a replacement problem:

@bot.callback_query_handler(func=lambda call: True)
def answer(call):

Can the same functions be called on an aiogram? Full code:

import telebot
telebot import types
import time
import db
import config
import functions
bot= telebot.TeleBot(config.token)
db.create_db()
@bot.message_handler(commands=['start'])
def send_welcome(message):
  #Setting
  amount= 1
  desc= 'Test item'
  buy_markup= types.InlineKeyboardMarkup(row_width=2)
  buy_button= types.InlineKeyboardButton(text=f'Buy for {amount}₽', callback_data=f'buy-{amount}-{desc}')
  buy_markup.add(buy_button)
  bot.send_message(message.chat.id, f'Buy for {amount}₽', reply_markup=buy_markup)
@bot.callback_query_handler(func=lambda call: True)
def answer(call):
  #buy button
  if call.data.startswith('buy'):
    call_data= call.data.split('-')
    url, code= db.add_trans(call_data[1], call_data[2])
    buy_markup= types.InlineKeyboardMarkup(row_width=2)
    buy_button_1= types.InlineKeyboardButton(text=f'Go', url=url)
    buy_button_2= types.InlineKeyboardButton(text=f'Check Payment', callback_data=f'check-{code}')
    buy_markup.add(buy_button_1, buy_button_2)
    bot.edit_message_text(f'Pay {call_data[1]}₽ via link:', call.message.chat.id, call.message.message_id, reply_markup=buy_markup)
  #check payment button
  elif call.data.startswith('check'):
    call_data= call.data.split('-')
    status= functions.check_pay(call_data[1])
    if status:
      price= db.update_trans(call_data[1])
      #balance updates or goods issuance
      bot.edit_message_text('Successfully paid!', call.message.chat.id, call.message.message_id, reply_markup='')
    else:
      pass
while True:
  try:
    bot.polling(none_stop=True, interval=0, timeout=5)
  except Exception as E:
    print(E.args)
    time.sleep(1)

Please edit the question to reflect the specific problem with enough detail to be able to give an adequate answer.

Дух сообщества2022-01-19 10:42:13
  • Answer # 1

    If I understand this piece of code correctly

    @bot.callback_query_handler(func=lambda call: True)
    def answer(call):
    

    should accept any callbacks, and process them inside the function itself. In fact, this is analogous

    @db.callback_query_handler(lambda call: True)
    async def answer(call: types.CallbackQuery):
        ...
    # like you can leave it empty at all, it will be the same
    @db.callback_query_handler()
    async def answer(call: types.CallbackQuery):
        ...
    

    But I still recommend filtering this data in advance. In your case it will be something like.

    @dp.callback_query_handler(text_startswith="buy")
    async def answer(call: types.CallbackQuery):
      #buy button
      call_data= call.data.split('-')
      url, code= db.add_trans(call_data[1], call_data[2])
      buy_markup= types.InlineKeyboardMarkup(row_width=2)
      buy_button_1= types.InlineKeyboardButton(text=f'Go', url=url)
      buy_button_2= types.InlineKeyboardButton(text=f'Check Payment', callback_data=f'check-{code}')
      buy_markup.add(buy_button_1, buy_button_2)
      bot.edit_message_text(f'Pay {call_data[1]}₽ via link:', call.message.chat.id, call.message.message_id, reply_markup=buy_markup)
    @dp.callback_query_handler(text_startswith="check")
    async def answer(call: types.CallbackQuery):
      call_data= call.data.split('-')
      status= functions.check_pay(call_data[1])
      if status:
      price= db.update_trans(call_data[1])
        #balance updates or goods issuance
        bot.edit_message_text('Successfully paid!', call.message.chat.id, call.message.message_id, reply_markup='')
    
  • Answer # 2

    If I understand this piece of code correctly

    @bot.callback_query_handler(func=lambda call: True)
    def answer(call):
    

    should accept any callbacks, and process them inside the function itself. In fact, this is analogous

    @db.callback_query_handler(lambda call: True)
    async def answer(call: types.CallbackQuery):
        ...
    # like you can leave it empty at all, it will be the same
    @db.callback_query_handler()
    async def answer(call: types.CallbackQuery):
        ...
    

    But I still recommend filtering this data in advance. In your case it will be something like.

    @dp.callback_query_handler(text_startswith="buy")
    async def answer(call: types.CallbackQuery):
      #buy button
      call_data= call.data.split('-')
      url, code= db.add_trans(call_data[1], call_data[2])
      buy_markup= types.InlineKeyboardMarkup(row_width=2)
      buy_button_1= types.InlineKeyboardButton(text=f'Go', url=url)
      buy_button_2= types.InlineKeyboardButton(text=f'Check Payment', callback_data=f'check-{code}')
      buy_markup.add(buy_button_1, buy_button_2)
      bot.edit_message_text(f'Pay {call_data[1]}₽ via link:', call.message.chat.id, call.message.message_id, reply_markup=buy_markup)
    @dp.callback_query_handler(text_startswith="check")
    async def answer(call: types.CallbackQuery):
      call_data= call.data.split('-')
      status= functions.check_pay(call_data[1])
      if status:
      price= db.update_trans(call_data[1])
        #balance updates or goods issuance
        bot.edit_message_text('Successfully paid!', call.message.chat.id, call.message.message_id, reply_markup='')