D
D
danilkapuh2017-10-06 15:58:43
Python
danilkapuh, 2017-10-06 15:58:43

Error in Telegram Bot Python code. How to fix?

Here is the code -

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()

Help!
Mistake -
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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xdgadd, 2017-10-06
@danilkapuh

You pass the m argument to the start function, but use message later in the code:

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

Just replace message with m:
def start(m):
    bot.send_message(m.chat.id, "Выбери замок")
    ...

PS
def messages(message):
  # этот if не будет работать из-за заглавной 'K' 
  if 'Красный' in message.text.lower():
    bot.send_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