TypeError: argument of type 'function' is not iterable
Делал бота для телеграма,который просто будет реагировать на кнопки. Фрагмент кода бота:
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
userid = message.from_user.id
mainmenu(userid)
def mainmenu(userid): # Главное меню
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
itembtn1 = types.KeyboardButton('1')
itembtn2 = types.KeyboardButton('2')
itembtn3 = types.KeyboardButton('3')
markup.add(itembtn1, itembtn2, itembtn3)
bot.send_message(userid, "Главное меню:", reply_markup=markup)
@bot.message_handler(lambda message: message.text == "1")
def hack(message: types.Message):
text = "Вы выбрали 1"
photo = open('/img/choose.gif', 'r')
bot.send_photo(message.from_user.id, photo, caption=text)
bot.infinity_polling()
По итогу выводит ошибку:
2021-12-05 11:00:37,559 (__init__.py:621 MainThread) ERROR - TeleBot: "Infinity polling exception: argument of type 'function' is not iterable"
2021-12-05 11:00:37,561 (__init__.py:623 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "D:\Python\lib\site-packages\telebot\__init__.py", line 617, in infinity_polling
self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "D:\Python\lib\site-packages\telebot\__init__.py", line 664, in polling
self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
File "D:\Python\lib\site-packages\telebot\__init__.py", line 726, in __threaded_polling
raise e
File "D:\Python\lib\site-packages\telebot\__init__.py", line 685, in __threaded_polling
polling_thread.raise_exceptions()
File "D:\Python\lib\site-packages\telebot\util.py", line 105, in raise_exceptions
raise self.exception_info
File "D:\Python\lib\site-packages\telebot\util.py", line 87, in run
task(*args, **kwargs)
File "D:\Python\lib\site-packages\telebot\__init__.py", line 414, in __retrieve_updates
self.process_new_updates(updates)
File "D:\Python\lib\site-packages\telebot\__init__.py", line 494, in process_new_updates
self.process_new_messages(new_messages)
File "D:\Python\lib\site-packages\telebot\__init__.py", line 527, in process_new_messages
self._notify_command_handlers(self.message_handlers, new_messages)
File "D:\Python\lib\site-packages\telebot\__init__.py", line 3310, in _notify_command_handlers
if self._test_message_handler(message_handler, message):
File "D:\Python\lib\site-packages\telebot\__init__.py", line 3244, in _test_message_handler
if not self._test_filter(message_filter, filter_value, message):
File "D:\Python\lib\site-packages\telebot\__init__.py", line 3277, in _test_filter
return message.content_type == 'text' and util.extract_command(message.text) in filter_value
TypeError: argument of type 'function' is not iterable
Может, кто-то сталкивался с подобным и знает решение? Заранее говорю "спасибо")
Ответы (1 шт):
Автор решения: GrAnd
→ Ссылка
Вместо
@bot.message_handler(lambda message: message.text == "1")
используйте
@bot.callback_query_handler(func=lambda message: message.text == "1")
Вот тут можете посмотреть пример.