import telebot
from telebot import types
bot = telebot.TeleBot('ВАШ_ТОКЕН')
def create_keyboard():
keyboard = types.InlineKeyboardMarkup()
# Создаем кнопку, где callback_data должна быть строкой
test_button = types.InlineKeyboardButton(text="test", callback_data="test_button")
keyboard.add(test_button)
return keyboard
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, "Hello", reply_markup=create_keyboard())
# Обработчик callback_data
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.data == "test_button":
bot.answer_callback_query(call.id)
bot.send_message(call.message.chat.id, "Вы нажали на кнопку test!")
bot.polling(none_stop=True, interval=0)
КОД:
import telebot
from telebot import types
bot = telebot.TeleBot('7350490491:AAGHKmAV4BlZPuGVXZcwbDLxyCaMvKp3V1Y')
def create_keyboard():
keyboard = types.InlineKeyboardMarkup()
keyboard_buttons = []
test_button = types.InlineKeyboardButton(text="test", callback_data=0)
keyboard_buttons.append(test_button)
keyboard.add(*keyboard_buttons)
return keyboard
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, "Hello", reply_markup=create_keyboard())
bot.polling(none_stop=True, interval=0)
ОШИБКА:
2024-10-27 19:37:39,828 (__init__.py:1221 MainThread) ERROR - TeleBot: "Threaded polling exception: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: can't parse inline keyboard button: Text buttons are unallowed in the inline keyboard"
2024-10-27 19:37:39,830 (__init__.py:1223 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "C:\Users\Алла\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\__init__.py", line 1215, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\Алла\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\util.py", line 150, in raise_exceptions
raise self.exception_info
File "C:\Users\Алла\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\util.py", line 93, in run
task(*args, **kwargs)
File "C:\Users\Алла\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\__init__.py", line 8707, in _run_middlewares_and_handler
result = handler['function'](message)
File "C:\Users\Алла\Desktop\Андрей\Проекты_по_программированию\PycharmProjects\черновики пайчарм\Личный помощник\Личный помощник.py", line 18, in start
bot.send_message(message.chat.id, "Hello", reply_markup=create_keyboard())
File "C:\Users\Алла\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\__init__.py", line 1755, in send_message
apihelper.send_message(
File "C:\Users\Алла\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\apihelper.py", line 273, in send_message
return _make_request(token, method_url, params=payload, method='post')
File "C:\Users\Алла\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\apihelper.py", line 168, in _make_request
json_result = _check_result(method_name, result)
File "C:\Users\Алла\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\apihelper.py", line 195, in _check_result
raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: can't parse inline keyboard button: Text buttons are unallowed in the inline keyboard
"
как реализовать корректное создание кнопки?