Ошибка: local variable 'markup' referenced before assignment
import telebot
import config
import random
from telebot import types
bot = telebot.TeleBot(config.TOKEN)
@bot.message_handler(commands=['start'])
def welcome(message):
sti = open('welcome.webp', 'rb')
bot.send_sticker(message.chat.id, sti)
# keyboard
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton("Рандомное число")
item2 = types.KeyboardButton("Как дела?")
markup.add(item1, item2)
bot.send_message(message.chat.id,
"Добро Пожаловать, {0.first_name}!\nЯ - <b>{1.first_name}</b>, бот созданный чтобы быть подопытным кроликом.".format(
message.from_user, bot.get_me()),
parse_mode='html', reply_markup=markup)
@bot.message_handler(content_types=["text"])
def lalala(message):
if message.chat.type == "private":
if message.text == 'Рандомное число':
bot.send_message(message.chat.id, str(random.randint(0, 3)))
elif message.text == 'Как дела?':
markup = types.InlineKeyboardMarkup()
item1 = types.InlineKeyboardButton("Хорошо", callback_data='good')
item2 = types.InlineKeyboardButton("Не очень", callback_data='bad')
markup.add(item1, item2)
bot.send_message(message.chat.id, "Отлично, сам(-а) как?", reply_markup=markup),
bot.send_message(message.chat.id, 'Не могу ответить:(')
@bot.callback_query_handler(func=lambda cal: True)
def callback_inline(call):
try:
if call.message:
if call.data == 'good':
bot.send_message(call.message.chat.id, ' Вот и отлично')
call.data == 'bad'
bot.send_message(call.message.chat.id, 'Жалко тебя:(')
# remove inline buttons
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Как дела?",
reply_markup=None)
# show alert
bot.answer_callback_query(chat_id=call.message.chat.id, show_alert=False,
text="ТЕЕЕЕЕЕСТ")
except Exception as e:
print(repr(e))
bot.polling(none_stop=True)
Ответы (1 шт):
Автор решения: Danis
→ Ссылка
проблема в отсутствии пробелов
а также как сказал @gil9red последняя строка должна быть в else
def lalala(message):
if message.chat.type == "private":
if message.text == 'Рандомное число':
bot.send_message(message.chat.id, str(random.randint(0, 3)))
elif message.text == 'Как дела?':
markup = types.InlineKeyboardMarkup()
item1 = types.InlineKeyboardButton("Хорошо", callback_data='good')
item2 = types.InlineKeyboardButton("Не очень", callback_data='bad')
markup.add(item1, item2)
bot.send_message(message.chat.id, "Отлично, сам(-а) как?", reply_markup=markup),
bot.send_message(message.chat.id, 'Не могу ответить:(')
все что идёт после markup = types.InlineKeyboardMarkup() должно было находиться на том же уровне
def lalala(message):
if message.chat.type == "private":
if message.text == 'Рандомное число':
bot.send_message(message.chat.id, str(random.randint(0, 3)))
elif message.text == 'Как дела?':
markup = types.InlineKeyboardMarkup()
item1 = types.InlineKeyboardButton("Хорошо", callback_data='good')
item2 = types.InlineKeyboardButton("Не очень", callback_data='bad')
markup.add(item1, item2)
bot.send_message(message.chat.id, "Отлично, сам(-а) как?", reply_markup=markup),
else:
bot.send_message(message.chat.id, 'Не могу ответить:(')