Why can't Telebot see the KeyboardButton attribute for the user's location?



  • The code. On the stage, the bot makes a button that the skirts the laser shares with its coordinates.

    @bot.message_handler(commands=['location']) #Запрос локации
    def request_location(message):
        keyboard = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
        button_geo = types.KeyboardButton(text="Отправить местоположение", request_location=True)
        keyboard.add(button_geo)
        bot.send_message(message.chat.id, "Поделись местоположением", reply_markup=keyboard)
    bot.polling()
    

    After team starts /location "AttributeError: 'module' object has no attribute 'KeyboardButton'," although the documentation of this attribute has: https://tlgrm.ru/docs/bots/api#keyboardbutton

    The error lies somewhere in the code at the location of "types.KeyboardButton."



  • I suspect that the problem is in your import section. Here's my working option:

    import config
    from telebot import TeleBot, types
    

    bot = TeleBot(config.TELEGRAM_BOT_TOKEN)

    @bot.message_handler(commands=['location']) #Запрос локации
    def request_location(message):
    keyboard = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
    button_geo = types.KeyboardButton(text="Отправить местоположение", request_location=True)
    keyboard.add(button_geo)
    bot.send_message(message.chat.id, "Поделись местоположением", reply_markup=keyboard)

    if name == "main":
    bot.polling()




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2