Telethon: How to get channel info from forward header

Created on 29 Apr 2017  ·  13Comments  ·  Source: LonamiWebs/Telethon

Hi... when u get a message for example by GetHistoryRequest if the message has fwd_from (type: MessageFwdHeader) it has only from_id, channel_id, channel_post, and date. how can I get forward peer type and channel or user info only by id?

Most helpful comment

Actually you should probably be able to client.get_entity(fwd.from_id) or client.get_entity(PeerChannel(fwd.channel_id)).

All 13 comments

Try constructing an InputChannel with only the ID, and set the access_hash to 0, or -1, for testing purposes. I'm not sure if it has to be this way, but it's my first guess.

Unfortunately, this solution did not work. :(

I tried looking for MTPMessageFwdHeader on the official client and I can't see the use of a hash anywhere. peerFromChannel is also a strange cast (not sure why it has to do that), which in Python would be something like 0x200000000 | (id & 0xFFFFFFFF). And then channelLoaded uses PeerData::FullLoaded… Which I'm not sure how it works. Sorry I'm clueless.

I have the same issue with super groups...

I want to send a message to a super group I'm already a participant. I have saved the chat_id but I don't have the access_hash to make an InputPeerChannel.

For channels, I can re-resolve the username to get the access_hash, but how about super groups? Most probably the /joinchat/XXXXXXXX link has expired already, and I have not stored the access_hash.

Any possible way to find access_hash?

I have the same issue with super groups.

Those are a different story though, because a forwarded message belongs to an user and not the group itself. If you are a participant, you should be able to find it through .get_dialogs().

Shoot, you mentioned you had the chat ID only and not the hash. And this method mentioned on #64 works exactly in that way: only requires an ID.

@Lonami thanks a lot. .get_dialogs() should work. get_full_chat though is only for groups I think. Doesn't work for super groups. ❤️

get_full_chat though is only for groups I think.

I wonder, though, if one could use its equivalent for channels and pass 0 as access_hash. Maybe that way it would resolve the full channel. I don't know.

from telethon.tl.types import InputChannel
from telethon.tl.functions.channels import GetFullChannelRequest
from telethon.tl.functions.messages import GetFullChatRequest

# Assuming the first message on the chat history
# for 'entity' is a message forwarded from a channel
_, msgs, _ = self.get_message_history(entity, limit=1)
fwd = msgs[0].fwd_from

# Attempts:
result = self.invoke(GetFullChatRequest(fwd.channel_id))  # CHAT_ID_INVALID
result = self.invoke(GetFullChannelRequest(InputChannel(fwd.channel_id, 0)))  # CHANNEL_INVALID

¯\_(ツ)_/¯

One second. I just realized something. If you retrieve some messages, say for instance MessagesSlice which is the most common (or Messages, both have the attributes we're interested in), you can see how they both have a chats and users member. You have to look for the channel under those chats, because a Channel is a Chat.

So say you have the ID for the channel you want. To get the channel itself, you would do something similar to (I haven't tested):

channel = next(c for c in messages.chats if c.id == channel_id)

Now you have the channel + the access hash, so it should work. Closing this issue. Another friend just asked me so I realized it could be done this way. Sorry for the delay.

@pazis, so were you able to retrieve access_code for the channel?

Actually you should probably be able to client.get_entity(fwd.from_id) or client.get_entity(PeerChannel(fwd.channel_id)).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

saharokk picture saharokk  ·  3Comments

ksanderer picture ksanderer  ·  3Comments

pazis picture pazis  ·  5Comments

chri1389 picture chri1389  ·  6Comments

adv-zl picture adv-zl  ·  3Comments