简体   繁体   中英

How to get a User object from a message in group at Telegram Bot

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM