如何使用 Telethon 获取转发消息的 channel/chat/user 名称?
How to get channel/chat/user name of forwarded message using Telethon?
我尝试通过频道 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(...), 'Invalid channel object. Make sure to pass
the right types, for instance making sure that the request is designed
for channels or otherwise look for a different one more suited.'
如何正确获取频道名称?
回答here
示例:
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
我尝试通过频道 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(...), 'Invalid channel object. Make sure to pass the right types, for instance making sure that the request is designed for channels or otherwise look for a different one more suited.'
如何正确获取频道名称?
回答here
示例:
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