Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_dump_msg_entities(self):
"""Show that entities are correctly parsed and stored"""
message = types.Message(
id=1,
to_id=types.PeerUser(321),
date=datetime.now(),
message='No entities'
)
dumper = Dumper(self.dumper_config)
fmt = BaseFormatter(dumper.conn)
# Test with no entities
dumper.dump_message(message, 123, None, None)
dumper.commit()
assert not next(fmt.get_messages_from_context(123, order='DESC')).formatting
# Test with many entities
text, entities = markdown.parse(
'Testing message with __italic__, **bold**, inline '
'[links](https://example.com) and [mentions](@hi), '
'as well as `code` and ``pre`` blocks.'
def get_entity_rows_by_id(self, key: int, exact: bool = True) -> Optional[Tuple[int, int]]:
t = self.Entity.__table__
if exact:
rows = self.engine.execute(select([t.c.id, t.c.hash]).where(
and_(t.c.session_id == self.session_id, t.c.id == key)))
else:
ids = (
utils.get_peer_id(PeerUser(key)),
utils.get_peer_id(PeerChat(key)),
utils.get_peer_id(PeerChannel(key))
)
rows = self.engine.execute(select([t.c.id, t.c.hash])
.where(
and_(t.c.session_id == self.session_id, t.c.id.in_(ids))))
try:
return next(rows)
except StopIteration:
return None
def __init__(self, messages):
message = messages[0]
if not message.out and isinstance(message.to_id, types.PeerUser):
# Incoming message (e.g. from a bot) has to_id=us, and
# from_id=bot (the actual "chat" from a user's perspective).
chat_peer = types.PeerUser(message.from_id)
else:
chat_peer = message.to_id
super().__init__(chat_peer=chat_peer,
msg_id=message.id, broadcast=bool(message.post))
SenderGetter.__init__(self, message.sender_id)
self.messages = messages
async def _into_id_set(client, chats):
"""Helper util to turn the input chat or chats into a set of IDs."""
if chats is None:
return None
if not utils.is_list_like(chats):
chats = (chats,)
result = set()
for chat in chats:
if isinstance(chat, int):
if chat < 0:
result.add(chat) # Explicitly marked IDs are negative
else:
result.update({ # Support all valid types of peers
utils.get_peer_id(types.PeerUser(chat)),
utils.get_peer_id(types.PeerChat(chat)),
utils.get_peer_id(types.PeerChannel(chat)),
})
elif isinstance(chat, TLObject) and chat.SUBCLASS_OF_ID == 0x2d45687:
# 0x2d45687 == crc32(b'Peer')
result.add(utils.get_peer_id(chat))
else:
chat = await client.get_input_entity(chat)
if isinstance(chat, types.InputPeerSelf):
chat = await client.get_me(input_peer=True)
result.add(utils.get_peer_id(chat))
return result
def list_dialogs(client):
dialogs = client.get_dialogs()
# import pdb; pdb.set_trace()
for item in dialogs:
dialog = item.dialog
if isinstance(dialog.peer, PeerUser):
process_dialog_with_user(dialog)
elif isinstance(dialog.peer, (PeerChannel, PeerChat)):
log.debug('Dialogs in chats/channels are not supported yet')
else:
log.warn('Unknown dialog type %s', dialog)
pattern = re.compile(
"(?i)^" + prefix + exp,
flags=flags
).finditer
else:
pattern = re.compile(exp, flags=flags).finditer
text = event.message.message or ''
matches = list(pattern(text)) or None
if not matches:
return
event.matches = matches
if self.require_admin:
text = "`You need to be an admin to use this command!`"
if not isinstance(event._chat_peer, types.PeerUser):
is_creator = False
is_admin = False
creator = hasattr(event.chat, 'creator')
admin_rights = hasattr(event.chat, 'admin_rights')
if not creator and not admin_rights:
event.chat = event._client.loop.create_task(
event.get_chat()
)
if self.incoming:
try:
p = event._client.loop.create_task(
event._client(
functions.channels.GetParticipantRequest(
channel=event.chat_id,
user_id=event.sender_id
async def _into_id_set(client, chats):
"""Helper util to turn the input chat or chats into a set of IDs."""
if chats is None:
return None
if not utils.is_list_like(chats):
chats = (chats,)
result = set()
for chat in chats:
if isinstance(chat, int):
if chat < 0:
result.add(chat) # Explicitly marked IDs are negative
else:
result.update({ # Support all valid types of peers
utils.get_peer_id(types.PeerUser(chat)),
utils.get_peer_id(types.PeerChat(chat)),
utils.get_peer_id(types.PeerChannel(chat)),
})
elif isinstance(chat, TLObject) and chat.SUBCLASS_OF_ID == 0x2d45687:
# 0x2d45687 == crc32(b'Peer')
result.add(utils.get_peer_id(chat))
else:
chat = await client.get_input_entity(chat)
if isinstance(chat, types.InputPeerSelf):
chat = await client.get_me(input_peer=True)
result.add(utils.get_peer_id(chat))
return result
elif peer.SUBCLASS_OF_ID == 0x2d45687:
return peer
elif isinstance(peer, (
types.contacts.ResolvedPeer, types.InputNotifyPeer,
types.TopPeer, types.Dialog, types.DialogPeer)):
return peer.peer
elif isinstance(peer, types.ChannelFull):
return types.PeerChannel(peer.id)
if peer.SUBCLASS_OF_ID in (0x7d7c6f86, 0xd9c7fc18):
# ChatParticipant, ChannelParticipant
return types.PeerUser(peer.user_id)
peer = get_input_peer(peer, allow_self=False, check_hash=False)
if isinstance(peer, (types.InputPeerUser, types.InputPeerUserFromMessage)):
return types.PeerUser(peer.user_id)
elif isinstance(peer, types.InputPeerChat):
return types.PeerChat(peer.chat_id)
elif isinstance(peer, (types.InputPeerChannel, types.InputPeerChannelFromMessage)):
return types.PeerChannel(peer.channel_id)
except (AttributeError, TypeError):
pass
_raise_cast_fail(peer, 'Peer')
def get_input_entity(self, peer):
try:
i = utils.get_peer_id(peer, add_mark=True)
h = self._input_entities[i] # we store the IDs marked
i, k = utils.resolve_id(i) # removes the mark and returns kind
if k == PeerUser:
return InputPeerUser(i, h)
elif k == PeerChat:
return InputPeerChat(i)
elif k == PeerChannel:
return InputPeerChannel(i, h)
except ValueError as e:
raise KeyError(peer) from e
raise KeyError(peer)
def __init__(self, message):
self.__dict__['_init'] = False
if not message.out and isinstance(message.to_id, types.PeerUser):
# Incoming message (e.g. from a bot) has to_id=us, and
# from_id=bot (the actual "chat" from a user's perspective).
chat_peer = types.PeerUser(message.from_id)
else:
chat_peer = message.to_id
super().__init__(chat_peer=chat_peer,
msg_id=message.id, broadcast=bool(message.post))
self.pattern_match = None
self.message = message