Telethon: Unable to get the username of the public channel in a received message after telethon update

Hi, earlier I was using the below code with telethon version 0.12.*

from telethon.tl.types import UpdatesTg

def update_handler(self, update_object):
    if type(update_object) is UpdatesTg:
        update_object = update_object.to_dict()
        user = update_object['chats'][0]['username'].upper() # username of the public channel
        msg = update_object['updates'][0]['message']['message']

In the user variable, I used to get the username of the public channel from which I received the message.

I updated telethon today to 0.15.3.2 and UpdatesTg seem not a part of it (or I’m unable to figure it out). So, I modified the code as follows: from telethon.tl.types import UpdateNewChannelMessage

from telethon.tl.types import UpdateNewChannelMessage

def update_handler(self, update_object):
    if type(update_object) is UpdateNewChannelMessage:
        update_object = update_object.to_dict()
        msg = update_object['message']['message']
        dt = update_object['message']['date']
        user = # how to get the username of the public channel as in previous version

username of the channel doesn’t seem to be part of the UpdateNewChannelMessage object.

Please tell me how do I get the username of the channel which sent the message.

Thank you very much 😃

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 28 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Lonami, can you please provide me with a working wiki link? The one posted does not actually work.

I was using 0.15.5.7 where I could use UpdateNewChannelMessage.Message.to_id value on any UpdateNewChannelMessage. I always passed the value, e.g. PeerChannel(channel_id=123456789), to get_entity() and got returned an object containing the channel title and so on.

I had extreme problems with receiving messages too late. Like described here and in several other threads: https://github.com/LonamiWebs/Telethon/issues/237

After reading this recent thread (237) I thought the problem could be fixed so I tried to update to 0.16.0.6 using pip yesterday. Unfortunately, it completely broke all my code.

Previously working:

if isinstance(update, UpdateNewChannelMessage):
    if hasattr(message, 'message'):
        chan = client.get_entity(message.to_id)

        # Finally, I need this ...
        print(chan.title)

Now, this just throws:

telethon.errors.rpc_error_list.ChannelInvalidError: (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')

While message.to_id contains object like:

PeerChannel(channel_id=123456789)

As I was progressively getting more and more clueless, I tried the following approaches along with many other things:

chan = client.get_entity(message.to_id)
chan = client.get_entity(PeerChannel(message.to_id))
chan = client.get_entity(PeerChannel(message.to_id.channel_id))
chan = client.get_input_entity(message.to_id)
chan = client.get_input_entity(PeerChannel(message.to_id))
chan = client.get_input_entity(PeerChannel(message.to_id.channel_id))

chan = client.GetChannelsRequest(client.get_input_entity(message.to_id))
...

… none of which worked for me. I am pretty running out of ideas.

I have come here in the search for any help because my project is actually ruined. My bookmarks of Telethon docs working just a few days back do not work anymore and it looks like a lot of things have changed in Telethon and I am failing to find the info what to change to restore previous functionality.

I have just seen someone asking exactly the same question on TelethonChat and other people having the same issue since November. Can someone help me please? Thank you in advance.