Python Telebot Проблема с inline keyboard
Проблема в том что, я создал игру и у игры есть свои кнопки в вниз наверх лево вправо, она работает все нормально, но я решил создать еще вопросы ответы с inline keyboard, и тут когда я нажимаю на какую то из кнопок под сообщением вылазит ошибка
Traceback (most recent call last):
File "error.py", line 121, in <module>
bot.polling(none_stop=True)
File "C:\Users\nigga22nd\AppData\Local\Programs\Python\Python38\lib\site-packa
ges\telebot\__init__.py", line 660, in polling
self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, a
llowed_updates)
File "C:\Users\nigga22nd\AppData\Local\Programs\Python\Python38\lib\site-packa
ges\telebot\__init__.py", line 722, in __threaded_polling
raise e
File "C:\Users\nigga22nd\AppData\Local\Programs\Python\Python38\lib\site-packa
ges\telebot\__init__.py", line 682, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\nigga22nd\AppData\Local\Programs\Python\Python38\lib\site-packa
ges\telebot\util.py", line 135, in raise_exceptions
raise self.exception_info
File "C:\Users\nigga22nd\AppData\Local\Programs\Python\Python38\lib\site-packa
ges\telebot\util.py", line 87, in run
task(*args, **kwargs)
File "error.py", line 66, in callback_func
user_data = maps[query.from_user.id]
KeyError: 1309129529
Код:
import config
import time
import traceback
from selenium import webdriver
from selenium.webdriver.common.by import By
import telebot
import random
from telebot import types
import requests
from bs4 import BeautifulSoup as BS
from mg import get_map_cell
# bot = telebot.TeleBot(TOKEN)
bot = telebot.TeleBot(config.TOKEN)
cols, rows = 8, 8 # Переменные из игры
# Клавиатура для игры
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(telebot.types.InlineKeyboardButton('←', callback_data='left'),
telebot.types.InlineKeyboardButton('↑', callback_data='up'),
telebot.types.InlineKeyboardButton('↓', callback_data='down'),
telebot.types.InlineKeyboardButton('→', callback_data='right'))
maps = {}
# функция для игры
def get_map_str(map_cell, player):
map_str = ""
for y in range(rows * 2 - 1):
for x in range(cols * 2 - 1):
if map_cell[x + y * (cols * 2 - 1)]:
map_str += "⬛"
elif (x, y) == player:
map_str += "?"
else:
map_str += "⬜"
map_str += "\n"
return map_str
# Command /play
@bot.message_handler(commands=['play'])
def play_message(message):
map_cell = get_map_cell(cols, rows)
user_data = {
'map': map_cell,
'x': 0,
'y': 0,
}
maps[message.from_user.id] = user_data
bot.send_message(message.from_user.id, "To win you need to reach the opposite corner")
bot.send_message(message.from_user.id, get_map_str(map_cell, (0, 0)), reply_markup=keyboard)
# Функция для игры
@bot.callback_query_handler(func=lambda call: True)
def callback_func(query):
user_data = maps[query.from_user.id]
new_x, new_y = user_data['x'], user_data['y']
if query.data == 'left':
new_x -= 1
if query.data == 'right':
new_x += 1
if query.data == 'up':
new_y -= 1
if query.data == 'down':
new_y += 1
if new_x < 0 or new_x > 2 * cols - 2 or new_y < 0 or new_y > rows * 2 - 2:
return None
if user_data['map'][new_x + new_y * (cols * 2 - 1)]:
return None
user_data['x'], user_data['y'] = new_x, new_y
if new_x == cols * 2 - 2 and new_y == rows * 2 - 2:
you_won = random.choice(["You won?", "'Well done' won such an easy game???"])
bot.edit_message_text(chat_id=query.message.chat.id,
message_id=query.message.id,
text=you_won)
return None
bot.edit_message_text(chat_id=query.message.chat.id,
message_id=query.message.id,
text=get_map_str(user_data['map'], (new_x, new_y)),
reply_markup=keyboard)
# Condition with text
@bot.message_handler(content_types="text")
def get_token(message):
if "teslabot" in message.text.lower():
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("How are you)", callback_data="How are you)")
item2 = types.InlineKeyboardButton("Who are you??", callback_data="Who are you??")
markup.add(item1, item2)
bot.send_message(message.chat.id, "Did you call me?", reply_markup=markup)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
try:
if call.message:
if call.data == "How are you)":
bot.send_message(call.message.chat.id, "Yes, ok, sort of")
elif call.data == "Who are you??":
bot.send_message(call.message.chat.id, "I do not know who I am:(")
except Exception as e:
print(repr(e))
# RUN
bot.polling(none_stop=True)