Как изменить inline кнопки при нажатии telebot
Делаю бота для игры "Супер крестики нолики". По правилам игры первым ходом игрок (крестик) выбирает сначала поле на которое он будет ходить, а потом клетку в этом поле. Так вот, у меня сначала выводится поле из inline кнопок 3 на 3, в которых игрок выбирает поле на которое он будет ходить, а после это сообщение должно изменяться на такое же поле из кнопок 3 на 3, но при этом должно выводиться уже другое поле (grid_one, grid_two и т.д. до nine).
Вопрос: Как изменить в сообщении при нажатии определённой кнопки поле из кнопок?
Код:
Функции:
def game_grid(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid = InlineKeyboardMarkup()
grid.row_width = 3
for i in range(1, 10, 3):
grid.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid
def grid_one(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid_one = types.InlineKeyboardMarkup(row_width=3)
for i in range(1, 10, 3):
grid_one.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid_one
def grid_two(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid_two = types.InlineKeyboardMarkup(row_width=3)
for i in range(1, 10, 3):
grid_two.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid_two
def grid_three(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid_three = types.InlineKeyboardMarkup(row_width=3)
for i in range(1, 10, 3):
grid_three.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid_three
def grid_four(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid_four = types.InlineKeyboardMarkup(row_width=3)
for i in range(1, 10, 3):
grid_four.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid_four
def grid_five(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid_five = types.InlineKeyboardMarkup(row_width=3)
for i in range(1, 10, 3):
grid_five.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid_five
def grid_six(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid_six = types.InlineKeyboardMarkup(row_width=3)
for i in range(1, 10, 3):
grid_six.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid_six
def grid_seven(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid_seven = types.InlineKeyboardMarkup(row_width=3)
for i in range(1, 10, 3):
grid_seven.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid_seven
def grid_eight(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid_eight = types.InlineKeyboardMarkup(row_width=3)
for i in range(1, 10, 3):
grid_eight.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid_eight
def grid_nine(values_dict, all_moves_set):
game_value = packaging_values(values_dict, all_moves_set)
grid_nine = types.InlineKeyboardMarkup(row_width=3)
for i in range(1, 10, 3):
grid_nine.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'1-{i + 0}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'1-{i + 1}-{game_value}'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'1-{i + 2}-{game_value}'))
return grid_nine
def empty_game_grid(values_dict):
grid = InlineKeyboardMarkup()
grid.row_width = 3
for i in range(1, 10, 3):
grid.add(InlineKeyboardButton(text=show_value(values_dict, i + 0),
callback_data=f'none'),
InlineKeyboardButton(text=show_value(values_dict, i + 1),
callback_data=f'none'),
InlineKeyboardButton(text=show_value(values_dict, i + 2),
callback_data=f'none'))
return grid
def show_value(values_dict, index):
index = str(index)
value = str(values_dict[index])
return value
def packaging_values(values_dict, all_moves_set):
value_str = ''
for i in values_dict:
value_str += str(values_dict[i])
value_str += '-'
for i in all_moves_set:
value_str += str(i)
return value_str
def unpacking_values(value_str):
data = value_str.split('-')
move = data[1]
dict_str = data[2]
set_str = data[3]
values_dict = {'1': ' ', '2': ' ', '3': ' ',
'4': ' ', '5': ' ', '6': ' ',
'7': ' ', '8': ' ', '9': ' '}
for i in range(1, 10):
values_dict[str(i)] = dict_str[i - 1]
all_moves_set = set(set_str)
return move, values_dict, all_moves_set
def check_win(values_dict):
check_data = [
[values_dict['1'], values_dict['2'], values_dict['3']],
[values_dict['4'], values_dict['5'], values_dict['6']],
[values_dict['7'], values_dict['8'], values_dict['9']],
[values_dict['1'], values_dict['4'], values_dict['7']],
[values_dict['2'], values_dict['5'], values_dict['8']],
[values_dict['3'], values_dict['6'], values_dict['9']],
[values_dict['1'], values_dict['5'], values_dict['9']],
[values_dict['3'], values_dict['5'], values_dict['7']]
]
for i in check_data:
if i[0] == i[1] == i[2] == '❌':
return True, 'Вы победили'
if i[0] == i[1] == i[2] == '⭕':
return True, 'Вы проиграли'
return False, 'none'
def check_win_small(values_dict):
check_data = [
[values_dict['1'], values_dict['2'], values_dict['3']],
[values_dict['4'], values_dict['5'], values_dict['6']],
[values_dict['7'], values_dict['8'], values_dict['9']],
[values_dict['1'], values_dict['4'], values_dict['7']],
[values_dict['2'], values_dict['5'], values_dict['8']],
[values_dict['3'], values_dict['6'], values_dict['9']],
[values_dict['1'], values_dict['5'], values_dict['9']],
[values_dict['3'], values_dict['5'], values_dict['7']]
]
for i in check_data:
if i[0] == i[1] == i[2] == '❌':
values_dict_main = {'1': ' ', '2': ' ', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': ' ', '8': ' ', '9': ' '}
return values_dict_main
if i[0] == i[1] == i[2] == '⭕':
values_dict_main = {'1': ' ', '2': ' ', '3': ' ', '4': ' ', '5': ' ', '6': ' ', '7': ' ', '8': ' ', '9': ' '}
return values_dict_main
return False, 'none'
message_handler:
elif message.text == 'Игра с ботом':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
stop_playbot = types.KeyboardButton('Закончить игру')
markup.add(stop_playbot)
bot.send_message(message.chat.id,'Игра с ботом'.format(message.from_user), reply_markup=markup, parse_mode="html")
all_moves_set = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}
values_dict = {'1': ' ', '2': ' ', '3': ' ',
'4': ' ', '5': ' ', '6': ' ',
'7': ' ', '8': ' ', '9': ' '}
bot.send_message(message.from_user.id, text="Игра началась",
reply_markup=game_grid(values_dict, all_moves_set))
callback:
@bot.callback_query_handler(func=lambda call: True)
def callback_data(call):
if call.data[0] == '1':
move, values_dict, all_moves_set = unpacking_values(call.data)
if move in all_moves_set:
if move == 1:
bot.edit_message_reply_markup(call.message.from_user.id, text="Выберите клетку",
reply_markup=grid_one(values_dict, all_moves_set))
if move == 1:
bot.edit_message_reply_markup(call.message.from_user.id, text="Выберите клетку",
reply_markup=grid_two(values_dict, all_moves_set))
if move == 1:
bot.edit_message_reply_markup(call.message.from_user.id, text="Выберите клетку",
reply_markup=grid_three(values_dict, all_moves_set))
if move == 1:
bot.edit_message_reply_markup(call.message.from_user.id, text="Выберите клетку",
reply_markup=grid_four(values_dict, all_moves_set))
if move == 1:
bot.edit_message_reply_markup(call.message.from_user.id, text="Выберите клетку",
reply_markup=grid_five(values_dict, all_moves_set))
if move == 1:
bot.edit_message_reply_markup(call.message.from_user.id, text="Выберите клетку",
reply_markup=grid_six(values_dict, all_moves_set))
if move == 1:
bot.edit_message_reply_markup(call.message.from_user.id, text="Выберите клетку",
reply_markup=grid_seven(values_dict, all_moves_set))
if move == 1:
bot.edit_message_reply_markup(call.message.from_user.id, text="Выберите клетку",
reply_markup=grid_eight(values_dict, all_moves_set))
if move == 1:
bot.edit_message_reply_markup(call.message.from_user.id, text="Выберите клетку",
reply_markup=grid_nine(values_dict, all_moves_set))
values_dict[move] = '❌'
all_moves_set.remove(move)