A
A
AlexHDreal2020-07-20 15:27:14
Python
AlexHDreal, 2020-07-20 15:27:14

There are inline buttons “like”, “dislike” Telegram bot. How to change the value of a button after clicking on it?

I decided to put the variables quant1 and quant2 into the button lines. But I don't know how to change them by clicking on the button. Made this code. But on clicking, an error pops up:

**File "bottwo.py", line 86, in callback_vote_action
quant1 += 1
TypeError: can only concatenate tuple (not "int") to tuple**

It seems clear that it does not work with numbers in a line wants, but then how to make it work?

vote_cb = CallbackData('vote', 'action')  # vote:<action>
likes = {}  # user_id: amount_of_likes

  def get_keyboard():
    quant1 = 0
    quant2 = 0

    butlike = types.InlineKeyboardButton(' ' + str(quant1), callback_data=vote_cb.new(action='up'))
    butdilike = types.InlineKeyboardButton(' ' + str(quant2), callback_data=vote_cb.new(action='down'))

    return types.InlineKeyboardMarkup().row(butlike, butdilike)


  @dp.callback_query_handler(vote_cb.filter(action=['up', 'down']))
  async def callback_vote_action(query: types.CallbackQuery, callback_data: dict):
    logging.info('Получил данные обратного вызова: %r', callback_data)  # callback_data содержит всю информацию из данных обратного вызова
    await query.answer()  # Не забудьте ответить на запрос обратного вызова как можно скорее
    callback_data_action = callback_data['action']
    likes_count = likes.get(query.from_user.id, 0)


    if callback_data_action == 'up':
      likes_count += 1
      for quant1 in get_keyboard():
        quant1 += 1

      if likes_count == 1:
        pass
    else:
      likes_count -= 1
      for quant2 in get_keyboard():
        quant2 += 1

      if likes_count == 0:
        pass


    likes[query.from_user.id] = likes_count  # обновление количества лайков в хранилище

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Angelex, 2020-07-20
@angelex

You need to change the message, namely the keyboard when posting ( edit_reply_markup() ).
Apparently you are using the aiogram library, it can speed up the process:
query.message.edit_reply_markup( reply_markup = ... )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question