skip to Main Content

I want to write a telegram bot via Python, but it doesn’t work.

import telebot

bot = telebot.TeleBot("my_token")

@bot.message_handler(content_types=['text'])
def sending(message):
    bot.send_message(message.chat.id, message.text)


# RUN

bot.polling(non_stop=True)


Returns to me the following problem.

AttributeError: ‘TeleBot’ object has no attribute ‘message_handler’

3

Answers


  1. As the source code shows (assuming you import the module obtained from pip, that is this), there is no definition for message_handler. In which case you need to use @bot.route which takes a string as argument as shown in the example within the repository readme (second link or here).

    Example:

    @bot.route('/command ?(.*)')
    def sending(message, cmd):
        bot.send_message(something, something_else)
    
    Login or Signup to reply.
  2. This is a common issue, unfortunately. I guess you installed the lib as “pip install telebot”, which leads to another package. You need to uninstall telebot, install pytelegrambotapi, but leave “import telebot” in code.

    Login or Signup to reply.
  3. Try this one. At first write:

    pip3 uninstall telebot
    

    After you do it write:

    pip3 uninstall PyTelegramBotAPI
    

    And the last step:

    pip3 install PyTelegramBotAPI==2.2.3
    

    It worked for me, I’m working on debian 9. So if you are working on Debian or on linux, it should work for you.

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