简体   繁体   English

如何从Telegram Bot的分组消息中获取User对象

[英]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 library, the solution is simple. 由于已经明确说明您正在使用库,因此解决方案很简单。 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.

相关问题 如何在机器人消息电报机器人之后从用户那里获取消息更新 - How to get message updates from the user after the bot message telegram bot 使用电报机器人 pytelegrambotapi 从群组回复用户的消息 - Reply to user's message from group using telegram bot pytelegrambotapi 如何从用户电报机器人获取信息? - How can I get information from the user telegram bot? 如何通过python电报机器人库在固定时间或间隔时间从机器人向用户发送消息? - How to send message from bot to user at a fixed time or at intervals through python telegram bot library? 如何在 Telegram Bot 中获取用户名? - How to get the user's name in Telegram Bot? 如何从我的机器人获取消息 ID 电报频道并删除该消息? - How to get the message id telegram channel from my bot and remove that message? 在没有用户命令的情况下使用 python 电报机器人发送消息? - Sending a message with a python telegram bot without a command from the user? 在python电报机器人中将每个用户的每条消息分开 - Separate each message from each user in python telegram bot Python Telegram Bot 让机器人响应消息 - Python Telegram Bot get the bot to respond to message Python-telegram-bot:如何在电报 api 中通过机器人获取自定义编号的最新消息? - Python-telegram-bot: How can I get customized number latest message by bot in telegram api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM