I'm writing a telegram bot with python

被刻印的时光 ゝ 提交于 2021-01-05 12:24:30

问题


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'


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/59909321/im-writing-a-telegram-bot-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!