How to get a User object from a message in group a

2019-08-08 21:21发布

Recently I was working on a simple Telegram bot written in python (with the python-telegram-bot library). In this bot I can get the last message object using following command :

bot.getUpdates()[-1].message

and according the Telegram Doc a message object is contains some attributes such as message_id,from,date,chat ,... which the from attribute returns a User object which is the sender of a message. But since from is a python keyword (used at import time) thus we can not use it which raise a SyntaxError.

As an alternative way we can use chat attribute which returns a User object in personal chats and a GroupChat object in groups that it doesn't contains and information about the user.Also I couldn't find any direct way to report this bug.

So the question is that is there any way to do this job? or any alternative solution maybe?

1条回答
地球回转人心会变
2楼-- · 2019-08-08 21:48

Since it was clarified that you're using the library, the solution is simple. The author of the library renamed the Python-incompatible from attribute to from_user. So just do:

user = bot.getUpdates()[-1].from_user
查看更多
登录 后发表回答