我目前正面临这个问题,我的Python脚本正在访问错误类的属性。显然,这与我项目中的某些名称空间冲突有关。
让我向您展示我当前的实现:
项目结构(所有内容都在同一软件包中):
|- bot_test_project
|- bot.py
|- message.py
bot.py:
import logging
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
API_KEY = "please enter your api key here"
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__)
class Bot:
def __init__(self):
updater = Updater(API_KEY)
self.__bot = updater.bot
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", self.receive_command, pass_user_data=True))
dispatcher.add_handler(MessageHandler(Filters.text, self.receive_message, pass_user_data=True))
updater.start_polling()
def receive_command(self, bot, update, user_data):
print(update.message.id)
def receive_message(self, bot, update, user_data):
print(update.message.id)
Bot()
message.py:
class Message:
def __init__(self, id=None, text=None, photo=None):
self.__id = id
self.__text = text
self.__photo = photo
@property
def id(self):
return self.__id
@property
def text(self):
return self.__text
@property
def photo(self):
return self.__photo
但是,我目前收到以下错误消息:
2018-07-13 23:13:35,915 - telegram.ext.dispatcher - ERROR - An uncaught error was raised while processing the update
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 279, in process_update
handler.handle_update(update, self)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telegram/ext/commandhandler.py", line 173, in handle_update
return self.callback(dispatcher.bot, update, **optional_args)
File "/bot.py", line 23, in receive_command
print(update.message.id)
AttributeError: 'Message' object has no attribute 'id'
receive_message()实际上是第三方库(telegram-bot)的回调函数。因此,所有功能参数都与第三方库类相关。
由于某种原因,Python在我的本地类中寻找text
属性,但是它应该访问更新的class属性。我什至没有导入我的本地课程message
。
我当前正在使用Python 3.6
编辑:
print(type(update.message))
<class 'telegram.message.Message'>
print(dir(update.message))
['ATTACHMENT_TYPES', 'MESSAGE_TYPES', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_effective_attachment', '_id_attrs', '_parse_html', '_parse_markdown', '_quote', 'audio', 'author_signature', 'bot', 'caption', 'caption_entities', 'caption_html', 'caption_html_urled', 'caption_markdown', 'caption_markdown_urled', 'channel_chat_created', 'chat', 'chat_id', 'connected_website', 'contact', 'date', 'de_json', 'delete', 'delete_chat_photo', 'document', 'edit_caption', 'edit_date', 'edit_reply_markup', 'edit_text', 'effective_attachment', 'entities', 'forward', 'forward_date', 'forward_from', 'forward_from_chat', 'forward_from_message_id', 'forward_signature', 'from_user', 'game', 'group_chat_created', 'invoice', 'left_chat_member', 'location', 'media_group_id', 'message_id', 'migrate_from_chat_id', 'migrate_to_chat_id', 'new_chat_members', 'new_chat_photo', 'new_chat_title', 'parse_caption_entities', 'parse_caption_entity', 'parse_entities', 'parse_entity', 'photo', 'pinned_message', 'reply_audio', 'reply_contact', 'reply_document', 'reply_html', 'reply_location', 'reply_markdown', 'reply_media_group', 'reply_photo', 'reply_sticker', 'reply_text', 'reply_to_message', 'reply_venue', 'reply_video', 'reply_video_note', 'reply_voice', 'sticker', 'successful_payment', 'supergroup_chat_created', 'text', 'text_html', 'text_html_urled', 'text_markdown', 'text_markdown_urled', 'to_dict', 'to_json', 'venue', 'video', 'video_note', 'voice']
编辑:
对于每个想知道update
对象是否实际上包含message.text
属性的人-请看看here来说服自己。
答案 0 :(得分:0)
您自己的message.py无关,因为该类来自电报模块telegram.message
。
documentation for .text
说它是可选的-也许您正在处理的消息没有文本?
编辑: 我创建了一个新令牌,并尝试运行您的代码,但无法重现您的问题。这是我的发现:
您自己的message.py
完全不相关。从项目中删除它不会执行任何操作。在其中添加语法错误不会执行任何操作。它甚至没有被导入。我放了一个名为message.py
的jpg文件,什么也没发生。
我尝试打印update.message.text
,在这里工作正常!当update.message.id
失败时,似乎id
不在您链接的Message
类的文档中。我改用update.message.message_id
,在这里工作正常!
我修改了如下功能以回答消息,并且可以正常工作,如屏幕截图所示:
修改:
def receive_message(self, bot, update, user_data):
bot.send_message(
chat_id=update.message.chat_id,
text="You said {} with id {}".format(
update.message.text, update.message.message_id
),
)