mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-06-18 19:16:43 +00:00
Support getting entities by exact name/title match
This commit is contained in:
parent
c31635cc34
commit
4050d1ca00
@ -438,13 +438,19 @@ class Session:
|
|||||||
(phone,))
|
(phone,))
|
||||||
else:
|
else:
|
||||||
username, _ = utils.parse_username(key)
|
username, _ = utils.parse_username(key)
|
||||||
c.execute('select id, hash from entities where username=?',
|
if username:
|
||||||
(username,))
|
c.execute('select id, hash from entities where username=?',
|
||||||
|
(username,))
|
||||||
|
|
||||||
if isinstance(key, int):
|
if isinstance(key, int):
|
||||||
c.execute('select id, hash from entities where id=?', (key,))
|
c.execute('select id, hash from entities where id=?', (key,))
|
||||||
|
|
||||||
result = c.fetchone()
|
result = c.fetchone()
|
||||||
|
if not result and isinstance(key, str):
|
||||||
|
# Try exact match by name if phone/username failed
|
||||||
|
c.execute('select id, hash from entities where name=?', (key,))
|
||||||
|
result = c.fetchone()
|
||||||
|
|
||||||
c.close()
|
c.close()
|
||||||
if result:
|
if result:
|
||||||
i, h = result # unpack resulting tuple
|
i, h = result # unpack resulting tuple
|
||||||
|
@ -1915,9 +1915,9 @@ class TelegramClient(TelegramBareClient):
|
|||||||
if user.phone == phone:
|
if user.phone == phone:
|
||||||
return user
|
return user
|
||||||
else:
|
else:
|
||||||
string, is_join_chat = utils.parse_username(string)
|
username, is_join_chat = utils.parse_username(string)
|
||||||
if is_join_chat:
|
if is_join_chat:
|
||||||
invite = self(CheckChatInviteRequest(string))
|
invite = self(CheckChatInviteRequest(username))
|
||||||
if isinstance(invite, ChatInvite):
|
if isinstance(invite, ChatInvite):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'Cannot get entity from a channel '
|
'Cannot get entity from a channel '
|
||||||
@ -1925,13 +1925,18 @@ class TelegramClient(TelegramBareClient):
|
|||||||
)
|
)
|
||||||
elif isinstance(invite, ChatInviteAlready):
|
elif isinstance(invite, ChatInviteAlready):
|
||||||
return invite.chat
|
return invite.chat
|
||||||
else:
|
elif username:
|
||||||
if string in ('me', 'self'):
|
if username in ('me', 'self'):
|
||||||
return self.get_me()
|
return self.get_me()
|
||||||
result = self(ResolveUsernameRequest(string))
|
result = self(ResolveUsernameRequest(username))
|
||||||
for entity in itertools.chain(result.users, result.chats):
|
for entity in itertools.chain(result.users, result.chats):
|
||||||
if entity.username.lower() == string:
|
if entity.username.lower() == username:
|
||||||
return entity
|
return entity
|
||||||
|
try:
|
||||||
|
# Nobody with this username, maybe it's an exact name/title
|
||||||
|
return self.get_entity(self.get_input_entity(string))
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
pass
|
||||||
|
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
'Cannot turn "{}" into any entity (user or chat)'.format(string)
|
'Cannot turn "{}" into any entity (user or chat)'.format(string)
|
||||||
|
Loading…
Reference in New Issue
Block a user