skip to Main Content

When I run the code below the following error message is displayed: 'TeleBot' object has no attribute 'message_handler'.

import telebot
from telebot import types
keyboard1 = telebot.types.ReplyKeyboardMarkup()
keyboard1.row('Ok', 'Bye')

bot = telebot.TeleBot('API')

@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id, 'Hi what do you want /start', reply_markup=keyboard1)
    
@bot.message_handler(content_types=['text'])
def send_text(message):
    if message.text.lower() =='Hello':
        bot.send_message(message.chat.id, message.text.upper() )
    elif message.text.lower() =='Bye':
        bot.send_message(message.chat.id,'see you soom' )
    elif message.text.lower() == 'I love you':
        bot.send_sticker(message.chat.id, 'API')

@bot.message_handler(content_types=['sticker'])
def sticker_id(message):
    print(message)

bot.polling(none_stop=True)
    

So what is wrong? I have installed pip and others. I wrote it on python IDLE. I wanted to make a telegram bot which gives stickers.

9

Answers


  1. This sounds like a versioning issue. I did a little googling (which you should have done before asking this here), and came up with many instances of this error message. Most of them point to the use of the wrong version of the PyTelegramBotAPI module. Here’s a post from someone with seemingly the exact same issue you are having:
    https://www.pythonanywhere.com/forums/topic/26658/

    There are other hits that suggest the same sort of fix. There’ also this SO question: I'm writing a telegram bot with python

    which seems to go a different route towards solving your problem.

    For more information, just google the title of this question.

    Login or Signup to reply.
  2. I did

    pip3 uninstall telebot
    

    Then

    pip3 uninstall PyTelegramBotAPI
    

    And then

    pip3 install PyTelegramBotAPI==2.2.3
    

    And it works now!

    PyTelegramBotAPI 3.0 have lot of changes and 3.0 API docs are not there yet. WIP. Use 2.2.3 older version instead and make sure you don’t have any other bot api like telebot

    Login or Signup to reply.
  3. Your code is correct, try after installing below command:

    pip install pyTelegramBotAPI 
    

    Note: if you have installed any other packages for import telebot then uninstall those and try after that.

    Login or Signup to reply.
  4. Try this code:

    pip3 uninstall telebot
    pip3 uninstall PyTelegramBotAPI
    pip3 install pyTelegramBotAPI
    pip3 install --upgrade pyTelegramBotAPI
    
    Login or Signup to reply.
  5. Wasted a whole hour trying to solve the problem. Other possible variant which worked for me:

    With latest (4.2.2) TeleBot version, it didn’t work without explicitly specifying some arguments. Like:

    bot.infinity_polling(timeout=10, logger_level=logging.DEBUG)

    Login or Signup to reply.
  6. Something that worked for me its launching .py directly from file not from cmd

    Login or Signup to reply.
  7. What helped me in this situation: you have to define your token as a string. Like that:

    API_TOKEN = 'xxxxxxx'
    
    bot = telebot.TeleBot(API_TOKEN)
    

    But not like that:

    bot = telebot.TeleBot('xxxxxxx')
    
    Login or Signup to reply.
  8. I wrote ‘import PyTelegramBotAPI’
    then imported this by click in intellij idea
    after that replaced ‘PyTelegramBotAPI’ with ‘telebot’ -> ‘import telebot’

    super crutches

    Login or Signup to reply.
  9. Solution:

    pip3 uninstall pytelegrambotapi
    pip3 install pytelegrambotapi
    

    Thanks to:

    https://github.com/eternnoir/pyTelegramBotAPI/issues/341#issuecomment-302945129

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search