A
A
anar662019-04-30 00:55:41
Python
anar66, 2019-04-30 00:55:41

telebot. How to send a message after clicking a button?

Let's say this code:

def start(message):
       markup = types.InlineKeyboardMarkup()
       button1 = types.InlineKeyboardButton(text="Самара", callback_data="Самара") 
       button2 = types.InlineKeyboardButton(text="Москва", callback_data="Москва") 
       markup.add(button1, button2)
       bot.send_message(chat_id=message.chat.id, text="Хде погоду узнать хош?", reply_markup=markup)
@bot.callback_query_handler(func=lambda call: True)
def longname(call):
    if call.data == "Самара":
        bot.send_message(chat_id=message.chat.id, text="/Нетакважно/")

Noo, this does not work and throws an error:
Error code
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/telebot/util.py", line 59, in run
task(*args, **kwargs)
File "chabyinbot.py", line 38, in longname
bot.send_message(chat_id=message.chat.id, text="/Нетакважно/")
NameError: name 'message' is not defined
"
Traceback (most recent call last):
File "chabyinbot.py", line 41, in
bot.polling()
File "/usr/lib/python3.7/site-packages/telebot/__init__.py", line 389, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "/usr/lib/python3.7/site-packages/telebot/__init__.py", line 413, in __threaded_polling
self.worker_pool.raise_exceptions()
File "/usr/lib/python3.7/site-packages/telebot/util.py", line 108, in raise_exceptions
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
File "/usr/lib/python3.7/site-packages/six.py", line 693, in reraise
raise value
File "/usr/lib/python3.7/site-packages/telebot/util.py", line 59, in run
task(*args, **kwargs)
File "chabyinbot.py", line 38, in longname
bot.send_message(chat_id=message.chat.id, text="/Нетакважно/")
NameError: name 'message' is not defined

Attempts to add @bot.message_handler above or below @bot.callback_query_handler(func=lambda call: True) - failed

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Danil K., 2019-06-07
@Danya_Violet

if call.data == 'Город':
        bot.send_message(call.message.chat.id, 'тепло там')

B
bbkmzzzz, 2019-04-30
@bbkmzzzz

And read the trace?
Before throwing a decorator under or over, read what it is and why it is needed.

S
sound901, 2019-04-30
@saund901

you have a "message" argument for the function, but ... supposedly the code does not know what it is, and swears at it, add a handler above the "start" function
@bot.message_handler(commands=['start'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question