我目前面临的问题是,我的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
属性,但它应该访问更新的类属性。我甚至还没有导入我的本地类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来说服自己。
发布于 2018-07-14 02:58:48
您自己的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
),
)
https://stackoverflow.com/questions/51330846
复制相似问题