MaTok
MaTok

Reputation: 391

Telethon can't get chat entity anymore

I reactivated a "old" bot and get this error:

telethon\utils.py", line 138, in _raise_cast_fail        
    raise TypeError('Cannot cast {} to any kind of {}.'.format(
TypeError: Cannot cast coroutine to any kind of Peer.

After some trying... I found out that this error occures when I do whitelist chats in the functions:

@bot.on(events.NewMessage(chats=bot.get_entity(config_group),pattern='/hello'))
async def hello(event):

My work around is:

@bot.on(events.NewMessage(pattern='/hello'))
async def hello(event):

    if int(event.chat_id) == int(config_group):

It works but for how long? Im wondering if I do something wrong? (I mean at the whitelist parameter the function isnt awaited - if that is the reason) But it works one week ago so ...

Thanks if you can help :)

Upvotes: 2

Views: 4956

Answers (2)

TheKill-996
TheKill-996

Reputation: 1069

IDs and usernames can be passed to chats= parameters, Telethon will get entity automatically.

@bot.on(events.NewMessage(chats=chat_id, pattern='/hello'))
async def hello(event):
    # do your things

As far as the error you got, get_entity returns coroutine so it must be awaited.

>> TypeError: Cannot cast coroutine to any kind of Peer.

Upvotes: 3

stranger555
stranger555

Reputation: 59

get_entity works fine what is the value of config_group can you say ?

Upvotes: 2

Related Questions