python-3.x 如何使用电话马拉松取得转发讯息的频道/聊天室/用户名?

vi4fp9gy  于 2023-01-14  发布在  Python
关注(0)|答案(2)|浏览(115)

我尝试通过频道ID获取频道名称:

result = self._client(GetHistoryRequest(
        entity,
        limit=100,
        offset_date=None,
        offset_id=0,
        max_id=0,
        min_id=last_read_message_id,
        add_offset=0
    ))
for message in result.messages:
    if isinstance(message.fwd_from, MessageFwdHeader):
        fwd_channel_id = message.fwd_from.channel_id
        if fwd_channel_id:
            fwd_result = self._client(GetFullChannelRequest( # problem!!!
                InputPeerChannel(message.fwd_from.channel_id, 0)
            ))

message.fwd_from看起来像:

fwd_from=MessageFwdHeader(
  channel_id=1053596007, 
  date=datetime.fromtimestamp(1507891987.0), 
  post_author=None, # None!!!
  from_id=None, 
  channel_post=3030
),

所以,我不能从message.fwd_from中取频道名。而且我也不加入这个频道。
当我尝试调用GetFullChannelRequest时,出现下一个错误:
ChannelInvalidError(...),'无效的通道对象。请确保传递正确的类型,例如,确保请求是为通道设计的,或者查找其他更合适的类型。'
如何正确获取频道名称?

gk7wooem

gk7wooem1#

在这里回答
示例:

result = self._client(GetHistoryRequest(
        entity,
        limit=100,
        offset_date=None,
        offset_id=0,
        max_id=0,
        min_id=last_read_message_id,
        add_offset=0
    ))
for message in result.messages:
    if isinstance(message.fwd_from, MessageFwdHeader):
            entity = self._client.get_input_entity(
                PeerChannel(message.fwd_from.channel_id)
            )
            if message.fwd_from.channel_id:
                fwd_result = self._client(GetFullChannelRequest(entity))
                if hasattr(fwd_result, 'chats') and len(fwd_result.chats) > 0:
                    fwd_title = fwd_result.chats[0].title
rqcrx0a6

rqcrx0a62#

在最新版本telethon中,您可以在消息对象中搜索它,例如从它的通道名称

message.forward.chat.title

对于用户

message.forward.sender.first_name

相关问题