G
G
Goldd lol2022-04-19 23:51:03
Python
Goldd lol, 2022-04-19 23:51:03

Why writes NameError: name 'message' is not defined?

Writes an error here is the code I have been looking for an error for 50 minutes. Help me please.
Here is the code:

import telebot

from telebot import types

bot = telebot.TeleBot('5266385225:AAFVFCtM_exp4w4l90vE4OFoxJfX5nCpmJE')

keyboard1 = telebot.types.ReplyKeyboardMarkup()
keyboard2 = telebot.types.ReplyKeyboardMarkup()
keyboard4 = telebot.types.ReplyKeyboardMarkup()
keyboard1.row('Парить', 'Статистика')
keyboard2.row('Вкусы')
keyboard4.row('Быстрый пар', 'Долгий затяг')

score = '0'
score2 = '0'
score3 = '0'

@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id, 'Привет, это игра Elf Bar.', reply_markup=keyboard1)

@bot.message_handler(content_types=['text'])
def send_text(message):
    if message.text.lower() == 'парить':
        bot.send_message(message.chat.id, 'Привет, выбери вкус, и будешь парить.', reply_markup=keyboard2)
    elif message.text.lower() == 'вкусы':
            markup = types.InlineKeyboardMarkup(row_width=2)
            button1 = types.InlineKeyboardButton("1", callback_data='1')
            button2 = types.InlineKeyboardButton("2", callback_data='2')
            button3 = types.InlineKeyboardButton("3", callback_data='3')
            button4 = types.InlineKeyboardButton("4", callback_data='4')
            button5 = types.InlineKeyboardButton("5", callback_data='5')
            button6 = types.InlineKeyboardButton("6", callback_data='6')
            button7 = types.InlineKeyboardButton("7", callback_data='7')
            button8 = types.InlineKeyboardButton("8", callback_data='8')
            button9 = types.InlineKeyboardButton("9", callback_data='9')
            button10 = types.InlineKeyboardButton("10", callback_data='10')
            button11 = types.InlineKeyboardButton("11", callback_data='11')
            button12 = types.InlineKeyboardButton("12", callback_data='12')
            button13 = types.InlineKeyboardButton("13", callback_data='13')
            button14 = types.InlineKeyboardButton("14", callback_data='14')
            button15 = types.InlineKeyboardButton("15", callback_data='15')
            button16 = types.InlineKeyboardButton("16", callback_data='16')
            button17 = types.InlineKeyboardButton("17", callback_data='17')
            button18 = types.InlineKeyboardButton("18", callback_data='18')
            button19 = types.InlineKeyboardButton("19", callback_data='19')
            button20 = types.InlineKeyboardButton("20", callback_data='20')

            markup.add(button1, button2, button3, button4, button5, button6,
            button7, button8, button9, button10, button11, button12, button13,
            button14, button15, button16, button17, button18, button19, button20)

            bot.send_message(message.chat.id, 'Выбери вкус который тебе понравится.', reply_markup=markup)
    elif message.text.lower() == 'статистика':
        bot.send_message(message.chat.id, 'Всего затянуто:' + score)
        bot.send_message(message.chat.id, 'Затянуто:' + score2)
        bot.send_message(message.chat.id, 'Долгих затяг:' + score3)
    elif message.text.lower() == 'и я тебя люблю':
        bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhF9iXw5mfaWV-hrqxrR4QHTPZ5FHiAACaRwAAjSIsUrkQKSLbxLsIiQE')

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    try:
        if call.message:
            if call.data == '1':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhF9iXw5mfaWV-hrqxrR4QHTPZ5FHiAACaRwAAjSIsUrkQKSLbxLsIiQE', reply_markup=keyboard4)
            elif call.data == '2':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '3':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '4':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '5':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '6':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '7':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '8':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '9':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '10':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '11':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '12':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '13':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '14':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '15':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '16':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '17':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '18':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '19':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            elif call.data == '20':
                bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAEEhGFiXw5qEEgJY2Rr7z68OxIPkNyCDAACmRkAAhfhsUqrkc1BkIizECQE', reply_markup=keyboard4)

            # 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(callback_query_id=call.id, show_alert=False,
                #text="ЭТО ТЕСТОВОЕ УВЕДОМЛЕНИЕ!!11")

    except Exception as e:
        print(repr(e))

# RUN
bot.polling(none_stop=True)

Help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Goldd lol, 2022-04-20
@Hacker2282288

I just didn't write 'call' before message.chat.id
. I was looking for the problem for over an hour.
And I'm 13 ahahaha don't believe it's really 13

V
Vindicar, 2022-04-19
@Vindicar

def callback_inline(call):
The function has neither a message parameter nor a message local variable. And then you have access to such a variable named message - message.chat.id.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question