[英]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 ). 最近,我正在研究一个简单的用python(带有
python-telegram-bot
库 )编写的Telegram机器人。 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. 根据电报文档 ,消息对象包含一些属性,例如
message_id,from,date,chat
,...,其中from
属性返回一个User对象,该对象是消息的发送者。 But since from
is a python keyword (used at import time) thus we can not use it which raise a SyntaxError. 但是由于
from
是一个python关键字 (在导入时使用),因此我们不能使用它引发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. 作为一种替代方法,我们可以使用
chat
属性,该属性在个人聊天中返回一个User对象,在不包含该分组的GroupChat
对象以及有关该用户的信息中返回。此外,我也找不到任何直接的方式来报告此错误。
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 python-telegram-bot library, the solution is simple. 由于已经明确说明您正在使用python-telegram-bot库,因此解决方案很简单。 The author of the library renamed the Python-incompatible
from
attribute to from_user
. 该库的作者将与Python不兼容的
from
属性重命名为from_user
。 So just do: 所以做:
user = bot.getUpdates()[-1].from_user
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.