“TeleBot”对象没有属性“message\u handler”

2024-09-28 18:48:34 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行下面的代码时,会显示以下错误消息:'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)
    

那怎么了?我已经安装了pip和其他软件。我在python IDLE上写的。我想做一个能贴标签的电报机器人


Tags: textyousendidmessagedefbotchat
3条回答

这听起来像是版本控制问题。我在谷歌上搜索了一下(在这里问这个问题之前,你应该这样做),并找到了这个错误消息的许多实例。大多数都指向使用了错误版本的PyTelegramBotAPI模块。这是一篇来自某个人的帖子,似乎与你的问题完全相同:

https://www.pythonanywhere.com/forums/topic/26658/

还有其他的点击提示了同样的修复方法。还有一个问题:

I'm writing a telegram bot with python

这似乎是解决你问题的另一条途径

要了解更多信息,只需在谷歌上搜索这个问题的标题

是的

pip3 uninstall telebot

然后

pip3 uninstall PyTelegramBotAPI

然后

pip3 install PyTelegramBotAPI==2.2.3

现在它可以工作了

PyAPI 3.0有很多变化,3.0 API文档还没有出现。在制品。改用2.2.3旧版本,并确保您没有任何其他机器人api,如telebot

请尝试以下代码:

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

相关问题 更多 >