N
N
nurs9482020-12-17 09:58:52
Python
nurs948, 2020-12-17 09:58:52

Error 'message' is not defined what could be the problem?

there is a handler

@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
    bot.send_message(message.from_user.id, f'Здравствуйте {message.from_user.first_name}Добро пожаловать в наш магазин детской одежды!')
    markup = telebot.types.InlineKeyboardMarkup(row_width=1)
    button1 = telebot.types.InlineKeyboardButton(text=' Контакты', 
    callback_data='contacts')
    button2 = telebot.types.InlineKeyboardButton(text=' Адреса', 
    callback_data='address')
    button3 = telebot.types.InlineKeyboardButton(text=' Поиск товара по номеру', 
    callback_data='search')
    markup.add(button1, button2, button3)
    bot.send_message(chat_id=message.chat.id, 
    text='Выберите один из команд ниже',reply_markup=markup)
    register_user(message.from_user.id, message.from_user.username,
                  message.from_user.first_name, message.from_user.last_name)


there is another callback
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.data == 'contacts':
        bot.send_message(chat_id=<b>message.chat.id</b>, 
        text='Контакты',reply_markup=markup)
        print('press button "contacts"')
    if call.data == 'address':
        print('press button "address"')
    if call.data == 'search':
        print('press button "search"')


Question: why, when sending a response, the script cannot receive the data message.chat_id ???

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Krostelev, 2020-12-17
@twistfire92

Because the message variable is not defined. you need to start from call
call.message.chat.id

A
Anonymous Coder, 2020-12-19
@Geza420

massage.chat.id = id of the chat where the message was sent from.
call.message.chat.id = id of the chat where the callback was sent from.
you don’t receive a message here, but the callback
call.message.chat.id is what you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question