Проблема с телеграм ботом на питоне
Писал игрового бота, первая клавиатура работает нормально, а во второй клавиатуре не работают кнопки, т.е я нажимаю на кнопку и ничего не происходит. Кто знает, как пофиксить? Вот код:
import telebot
import config
from telebot import types
bot = telebot.TeleBot(config.TOKEN)
@bot.message_handler(commands=['start'])
def start(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
e1 = types.KeyboardButton("Breaking The Bank")
e2 = types.KeyboardButton("Escaping The Prison")
e3 = types.KeyboardButton("Stealing The Diamond")
e4 = types.KeyboardButton("Infiltrating The Airship")
e5 = types.KeyboardButton("Felling The Complex")
e6 = types.KeyboardButton("Completing The Mission")
markup.add(e1, e2, e3, e4, e5, e6)
bot.send_message(message.chat.id, "Выберете эпизод", reply_markup=markup)
@bot.message_handler(content_types=['text'])
def U(message):
if message.chat.type == 'private':
if message.text == "Breaking The Bank":
markup1 = types.ReplyKeyboardMarkup(resize_keyboard=True)
sh = types.KeyboardButton("Shovel")
ex = types.KeyboardButton("Explosives")
tp1 = types.KeyboardButton("Teleporter")
la = types.KeyboardButton("Laser")
wb = types.KeyboardButton("Wrecking Ball")
di = types.KeyboardButton("Disguise")
markup1.add(sh, ex, tp1, la, wb, di)
introbtb = open("/home/nitrorx/bot/introbtb.mp4", 'rb')
if message.text == "a":
introbtb = open("/home/nitrorx/bot/shovel.mp4", 'rb')
bot.send_video(message.chat.id, introbtb, reply_markup=markup1)
bot.polling(none_stop=True)
Ответы (1 шт):
Автор решения: vladrunk
→ Ссылка
Посмотри пример простого бота https://github.com/vladrunk/MagicNumberBot Создание клавиатуры лучше выноси в отдельные функции.
По поводу этого:
def U(message):
if message.chat.type == 'private':
if message.text == "Breaking The Bank":
markup1 = types.ReplyKeyboardMarkup(resize_keyboard=True)
sh = types.KeyboardButton("Shovel")
ex = types.KeyboardButton("Explosives")
tp1 = types.KeyboardButton("Teleporter")
la = types.KeyboardButton("Laser")
wb = types.KeyboardButton("Wrecking Ball")
di = types.KeyboardButton("Disguise")
markup1.add(sh, ex, tp1, la, wb, di)
introbtb = open("/home/nitrorx/bot/introbtb.mp4", 'rb')
if message.text == "a":
introbtb = open("/home/nitrorx/bot/shovel.mp4", 'rb')
bot.send_video(message.chat.id, introbtb, reply_markup=markup1)
Скорее всего доходит до if message.text=="a" и стопариться, так как отправка сообщения в этом ифе.
Вынеси bot.send_message из ифа.