@danilkapuh

Ошибка в коде Telegram Bot Python. Как исправить?

Вот код -
import telebot
from telebot import types

token = 'Не покажу я свой токен)))'

bot = telebot.TeleBot(token)

@bot.message_handler(commands=['start'])
def start(m):
     bot.send_message(<b>message</b>.chat.id, "Выбери замок")
     keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
     keyboard.add(*[types.KeyboardButton(name) for name in ['Красный',
                                                            'Чёрный ',
                                                            'Чёрно-Белый ',
                                                            'Белый ']])
     msg = bot.send_message(m.chat.id, 'Какой замок выбираешь?',
                      reply_markup=keyboard)
     bot.register_next_step_handler(msg, messages)

@bot.message_handler(content_types=["text"])
def messages(message):
  if 'Красный' in message.text.lower():
    bot.send_message(message.chat.id, 'Вы выбрали красный замок')
  elif 'доктор' in message.text.lower() or 'ватсон' in message.text.lower():
    bot.send_message(message.chat.id, 'Вы выбрали Ватсона')
      
bot.polling()


Помогите!

Ошибка -
F:\Python36\python.exe C:/Users/danil/PycharmProjects/Bot/bot.py
Traceback (most recent call last):
  File "C:/Users/danil/PycharmProjects/Bot/bot.py", line 28, in <module>
    bot.polling()
  File "F:\Python36\lib\site-packages\telebot\__init__.py", line 222, in polling
    self.__threaded_polling(none_stop, interval, timeout)
  File "F:\Python36\lib\site-packages\telebot\__init__.py", line 246, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "F:\Python36\lib\site-packages\telebot\util.py", line 103, in raise_exceptions
    six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
  File "F:\Python36\lib\site-packages\six.py", line 686, in reraise
    raise value
  File "F:\Python36\lib\site-packages\telebot\util.py", line 54, in run
    task(*args, **kwargs)
  File "C:/Users/danil/PycharmProjects/Bot/bot.py", line 11, in start
    bot.send_message(message.chat.id, "Выбери замок")
NameError: name 'message' is not defined
  • Вопрос задан
  • 4273 просмотра
Решения вопроса 1
@xdgadd
ML/Python/Cpp
Вы передаёте в функцию start аргумент m, но дальше в коде используете message:
def start(m):
    bot.send_message(message.chat.id, "Выбери замок")
     ...


Просто замените message на m:
def start(m):
    bot.send_message(m.chat.id, "Выбери замок")
    ...


P.S.
def messages(message):
  # этот if не будет работать из-за заглавной 'K' 
  if 'Красный' in message.text.lower():
    bot.send_message(message.chat.id, 'Вы выбрали красный замок')
    ....
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы