''TeleBot' object has no attribute 'message_handler'

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.



Solution 1:[1]

Try this code:

pip3 uninstall telebot
pip3 uninstall PyTelegramBotAPI
pip3 install pyTelegramBotAPI
pip3 install --upgrade pyTelegramBotAPI

Solution 2:[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

Solution 3:[3]

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.

Solution 4:[4]

I have deleted everything and download again and it helped me.

Solution 5:[5]

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.

Solution 6:[6]

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)

Solution 7:[7]

Something that worked for me its launching .py directly from file not from cmd

Solution 8:[8]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neuron - Freedom for Ukraine
Solution 2 Zoe stands with Ukraine
Solution 3 CryptoFool
Solution 4 Aruzhan
Solution 5
Solution 6 Sergey Gerasimov
Solution 7 Darcka
Solution 8 mqrkin