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
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.
I did
Then
And then
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
Your code is correct, try after installing below command:
Note: if you have installed any other packages for
import telebot
then uninstall those and try after that.Try this code:
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)
Something that worked for me its launching .py directly from file not from cmd
What helped me in this situation: you have to define your token as a string. Like that:
But not like that:
I wrote ‘import PyTelegramBotAPI’
then imported this by click in intellij idea
after that replaced ‘PyTelegramBotAPI’ with ‘telebot’ -> ‘import telebot’
super crutches
Solution:
Thanks to:
https://github.com/eternnoir/pyTelegramBotAPI/issues/341#issuecomment-302945129