mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-10 10:49:39 +00:00
Begin updating the way updates are built
This commit is contained in:
@@ -107,7 +107,7 @@ class Album(EventBuilder, _custom.chatgetter.ChatGetter, _custom.sendergetter.Se
|
||||
_custom.sendergetter.SenderGetter.__init__(self, message.sender_id)
|
||||
self.messages = messages
|
||||
|
||||
def _build(cls, update, others=None, self_id=None, *todo, **todo2):
|
||||
def _build(cls, client, update, entities):
|
||||
if not others:
|
||||
return # We only care about albums which come inside the same Updates
|
||||
|
||||
@@ -146,6 +146,12 @@ class Album(EventBuilder, _custom.chatgetter.ChatGetter, _custom.sendergetter.Se
|
||||
and u.message.grouped_id == group)
|
||||
])
|
||||
|
||||
self = cls.__new__(cls)
|
||||
self._client = client
|
||||
self._sender = entities.get(_tl.PeerUser(update.user_id))
|
||||
self._chat = entities.get(_tl.PeerUser(update.user_id))
|
||||
return self
|
||||
|
||||
def _set_client(self, client):
|
||||
super()._set_client(client)
|
||||
self._sender, self._input_sender = utils._get_entity_pair(self.sender_id, self._entities)
|
||||
|
@@ -34,15 +34,12 @@ class StopPropagation(Exception):
|
||||
class EventBuilder(abc.ABC):
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def _build(cls, update, others, self_id, entities, client):
|
||||
def _build(cls, client, update, entities):
|
||||
"""
|
||||
Builds an event for the given update if possible, or returns None.
|
||||
|
||||
`others` are the rest of updates that came in the same container
|
||||
as the current `update`.
|
||||
|
||||
`self_id` should be the current user's ID, since it is required
|
||||
for some events which lack this information but still need it.
|
||||
`entities` must have `get(Peer) -> User|Chat` and `self_id`,
|
||||
which must be the current user's ID.
|
||||
"""
|
||||
|
||||
|
||||
|
@@ -69,29 +69,32 @@ class CallbackQuery(EventBuilder, _custom.chatgetter.ChatGetter, _custom.senderg
|
||||
Button.inline('Nope', b'no')
|
||||
])
|
||||
"""
|
||||
def __init__(self, query, peer, msg_id):
|
||||
_custom.chatgetter.ChatGetter.__init__(self, peer)
|
||||
_custom.sendergetter.SenderGetter.__init__(self, query.user_id)
|
||||
@classmethod
|
||||
def _build(cls, client, update, entities):
|
||||
query = update
|
||||
if isinstance(update, _tl.UpdateBotCallbackQuery):
|
||||
peer = update.peer
|
||||
msg_id = update.msg_id
|
||||
elif isinstance(update, _tl.UpdateInlineBotCallbackQuery):
|
||||
# See https://github.com/LonamiWebs/Telethon/pull/1005
|
||||
# The long message ID is actually just msg_id + peer_id
|
||||
msg_id, pid = struct.unpack('<ii', struct.pack('<q', update.msg_id.id))
|
||||
peer = _tl.PeerChannel(-pid) if pid < 0 else _tl.PeerUser(pid)
|
||||
else:
|
||||
return None
|
||||
|
||||
self = cls.__new__(cls)
|
||||
self._client = client
|
||||
self._sender = entities.get(_tl.PeerUser(query.user_id))
|
||||
self._chat = entities.get(peer)
|
||||
|
||||
self.query = query
|
||||
self.data_match = None
|
||||
self.pattern_match = None
|
||||
self._message = None
|
||||
self._answered = False
|
||||
|
||||
@classmethod
|
||||
def _build(cls, update, others=None, self_id=None, *todo, **todo2):
|
||||
if isinstance(update, _tl.UpdateBotCallbackQuery):
|
||||
return cls.Event(update, update.peer, update.msg_id)
|
||||
elif isinstance(update, _tl.UpdateInlineBotCallbackQuery):
|
||||
# See https://github.com/LonamiWebs/Telethon/pull/1005
|
||||
# The long message ID is actually just msg_id + peer_id
|
||||
mid, pid = struct.unpack('<ii', struct.pack('<q', update.msg_id.id))
|
||||
peer = _tl.PeerChannel(-pid) if pid < 0 else _tl.PeerUser(pid)
|
||||
return cls.Event(update, peer, mid)
|
||||
|
||||
def _set_client(self, client):
|
||||
super()._set_client(client)
|
||||
self._sender, self._input_sender = utils._get_entity_pair(self.sender_id, self._entities)
|
||||
return self
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
|
@@ -72,18 +72,110 @@ class ChatAction(EventBuilder):
|
||||
await event.reply('Welcome to the group!')
|
||||
"""
|
||||
|
||||
def __init__(self, where, new_photo=None,
|
||||
added_by=None, kicked_by=None, created=None, from_approval=None,
|
||||
users=None, new_title=None, pin_ids=None, pin=None, new_score=None):
|
||||
@classmethod
|
||||
def _build(cls, client, update, entities):
|
||||
where = None
|
||||
new_photo = None
|
||||
added_by = None
|
||||
kicked_by = None
|
||||
created = None
|
||||
from_approval = None
|
||||
users = None
|
||||
new_title = None
|
||||
pin_ids = None
|
||||
pin = None
|
||||
new_score = None
|
||||
|
||||
# Rely on specific pin updates for unpins, but otherwise ignore them
|
||||
# for new pins (we'd rather handle the new service message with pin,
|
||||
# so that we can act on that message').
|
||||
if isinstance(update, _tl.UpdatePinnedChannelMessages) and not update.pinned:
|
||||
where = _tl.PeerChannel(update.channel_id)
|
||||
pin_ids = update.messages
|
||||
pin = update.pinned
|
||||
|
||||
elif isinstance(update, _tl.UpdatePinnedMessages) and not update.pinned:
|
||||
where = update.peer
|
||||
pin_ids = update.messages
|
||||
pin = update.pinned
|
||||
|
||||
elif isinstance(update, _tl.UpdateChatParticipantAdd):
|
||||
where = _tl.PeerChat(update.chat_id)
|
||||
added_by = update.inviter_id or True
|
||||
users = update.user_id
|
||||
|
||||
elif isinstance(update, _tl.UpdateChatParticipantDelete):
|
||||
where = _tl.PeerChat(update.chat_id)
|
||||
kicked_by = True
|
||||
users = update.user_id
|
||||
|
||||
# UpdateChannel is sent if we leave a channel, and the update._entities
|
||||
# set by _process_update would let us make some guesses. However it's
|
||||
# better not to rely on this. Rely only in MessageActionChatDeleteUser.
|
||||
|
||||
elif (isinstance(update, (
|
||||
_tl.UpdateNewMessage, _tl.UpdateNewChannelMessage))
|
||||
and isinstance(update.message, _tl.MessageService)):
|
||||
msg = update.message
|
||||
action = update.message.action
|
||||
if isinstance(action, _tl.MessageActionChatJoinedByLink):
|
||||
where = msg
|
||||
added_by = True
|
||||
users = msg.from_id
|
||||
elif isinstance(action, _tl.MessageActionChatAddUser):
|
||||
# If a user adds itself, it means they joined via the public chat username
|
||||
added_by = ([msg.sender_id] == action.users) or msg.from_id
|
||||
where = msg
|
||||
added_by = added_by
|
||||
users = action.users
|
||||
elif isinstance(action, _tl.MessageActionChatJoinedByRequest):
|
||||
# user joined from join request (after getting admin approval)
|
||||
where = msg
|
||||
from_approval = True
|
||||
users = msg.from_id
|
||||
elif isinstance(action, _tl.MessageActionChatDeleteUser):
|
||||
where = msg
|
||||
kicked_by = utils.get_peer_id(msg.from_id) if msg.from_id else True
|
||||
users = action.user_id
|
||||
elif isinstance(action, _tl.MessageActionChatCreate):
|
||||
where = msg
|
||||
users = action.users
|
||||
created = True
|
||||
new_title = action.title
|
||||
elif isinstance(action, _tl.MessageActionChannelCreate):
|
||||
where = msg
|
||||
created = True
|
||||
users = msg.from_id
|
||||
new_title = action.title
|
||||
elif isinstance(action, _tl.MessageActionChatEditTitle):
|
||||
where = msg
|
||||
users = msg.from_id
|
||||
new_title = action.title
|
||||
elif isinstance(action, _tl.MessageActionChatEditPhoto):
|
||||
where = msg
|
||||
users = msg.from_id
|
||||
new_photo = action.photo
|
||||
elif isinstance(action, _tl.MessageActionChatDeletePhoto):
|
||||
where = msg
|
||||
users = msg.from_id
|
||||
new_photo = True
|
||||
elif isinstance(action, _tl.MessageActionPinMessage) and msg.reply_to:
|
||||
where = msg
|
||||
pin_ids=[msg.reply_to_msg_id]
|
||||
elif isinstance(action, _tl.MessageActionGameScore):
|
||||
where = msg
|
||||
new_score = action.score
|
||||
|
||||
self = cls.__new__(cls)
|
||||
self._client = client
|
||||
|
||||
if isinstance(where, _tl.MessageService):
|
||||
self.action_message = where
|
||||
where = where.peer_id
|
||||
else:
|
||||
self.action_message = None
|
||||
|
||||
# TODO needs some testing (can there be more than one id, and do they follow pin order?)
|
||||
# same in get_pinned_message
|
||||
super().__init__(chat_peer=where, msg_id=pin_ids[0] if pin_ids else None)
|
||||
self._chat = entities.get(where)
|
||||
|
||||
self.new_pin = pin_ids is not None
|
||||
self._pin_ids = pin_ids
|
||||
@@ -128,87 +220,7 @@ class ChatAction(EventBuilder):
|
||||
self.new_score = new_score
|
||||
self.unpin = not pin
|
||||
|
||||
@classmethod
|
||||
def _build(cls, update, others=None, self_id=None, *todo, **todo2):
|
||||
# Rely on specific pin updates for unpins, but otherwise ignore them
|
||||
# for new pins (we'd rather handle the new service message with pin,
|
||||
# so that we can act on that message').
|
||||
if isinstance(update, _tl.UpdatePinnedChannelMessages) and not update.pinned:
|
||||
return cls.Event(_tl.PeerChannel(update.channel_id),
|
||||
pin_ids=update.messages,
|
||||
pin=update.pinned)
|
||||
|
||||
elif isinstance(update, _tl.UpdatePinnedMessages) and not update.pinned:
|
||||
return cls.Event(update.peer,
|
||||
pin_ids=update.messages,
|
||||
pin=update.pinned)
|
||||
|
||||
elif isinstance(update, _tl.UpdateChatParticipantAdd):
|
||||
return cls.Event(_tl.PeerChat(update.chat_id),
|
||||
added_by=update.inviter_id or True,
|
||||
users=update.user_id)
|
||||
|
||||
elif isinstance(update, _tl.UpdateChatParticipantDelete):
|
||||
return cls.Event(_tl.PeerChat(update.chat_id),
|
||||
kicked_by=True,
|
||||
users=update.user_id)
|
||||
|
||||
# UpdateChannel is sent if we leave a channel, and the update._entities
|
||||
# set by _process_update would let us make some guesses. However it's
|
||||
# better not to rely on this. Rely only in MessageActionChatDeleteUser.
|
||||
|
||||
elif (isinstance(update, (
|
||||
_tl.UpdateNewMessage, _tl.UpdateNewChannelMessage))
|
||||
and isinstance(update.message, _tl.MessageService)):
|
||||
msg = update.message
|
||||
action = update.message.action
|
||||
if isinstance(action, _tl.MessageActionChatJoinedByLink):
|
||||
return cls.Event(msg,
|
||||
added_by=True,
|
||||
users=msg.from_id)
|
||||
elif isinstance(action, _tl.MessageActionChatAddUser):
|
||||
# If a user adds itself, it means they joined via the public chat username
|
||||
added_by = ([msg.sender_id] == action.users) or msg.from_id
|
||||
return cls.Event(msg,
|
||||
added_by=added_by,
|
||||
users=action.users)
|
||||
elif isinstance(action, _tl.MessageActionChatJoinedByRequest):
|
||||
# user joined from join request (after getting admin approval)
|
||||
return cls.Event(msg,
|
||||
from_approval=True,
|
||||
users=msg.from_id)
|
||||
elif isinstance(action, _tl.MessageActionChatDeleteUser):
|
||||
return cls.Event(msg,
|
||||
kicked_by=utils.get_peer_id(msg.from_id) if msg.from_id else True,
|
||||
users=action.user_id)
|
||||
elif isinstance(action, _tl.MessageActionChatCreate):
|
||||
return cls.Event(msg,
|
||||
users=action.users,
|
||||
created=True,
|
||||
new_title=action.title)
|
||||
elif isinstance(action, _tl.MessageActionChannelCreate):
|
||||
return cls.Event(msg,
|
||||
created=True,
|
||||
users=msg.from_id,
|
||||
new_title=action.title)
|
||||
elif isinstance(action, _tl.MessageActionChatEditTitle):
|
||||
return cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_title=action.title)
|
||||
elif isinstance(action, _tl.MessageActionChatEditPhoto):
|
||||
return cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_photo=action.photo)
|
||||
elif isinstance(action, _tl.MessageActionChatDeletePhoto):
|
||||
return cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_photo=True)
|
||||
elif isinstance(action, _tl.MessageActionPinMessage) and msg.reply_to:
|
||||
return cls.Event(msg,
|
||||
pin_ids=[msg.reply_to_msg_id])
|
||||
elif isinstance(action, _tl.MessageActionGameScore):
|
||||
return cls.Event(msg,
|
||||
new_score=action.score)
|
||||
return self
|
||||
|
||||
async def respond(self, *args, **kwargs):
|
||||
"""
|
||||
|
@@ -40,21 +40,19 @@ class InlineQuery(EventBuilder, _custom.chatgetter.ChatGetter, _custom.senderget
|
||||
builder.article('lowercase', text=event.text.lower()),
|
||||
])
|
||||
"""
|
||||
def __init__(self, query):
|
||||
_custom.chatgetter.ChatGetter.__init__(self, _tl.PeerUser(query.user_id))
|
||||
_custom.sendergetter.SenderGetter.__init__(self, query.user_id)
|
||||
self.query = query
|
||||
@classmethod
|
||||
def _build(cls, client, update, entities):
|
||||
if not isinstance(update, _tl.UpdateBotInlineQuery):
|
||||
return None
|
||||
|
||||
self = cls.__new__(cls)
|
||||
self._client = client
|
||||
self._sender = entities.get(_tl.PeerUser(update.user_id))
|
||||
self._chat = entities.get(_tl.PeerUser(update.user_id))
|
||||
self.query = update
|
||||
self.pattern_match = None
|
||||
self._answered = False
|
||||
|
||||
@classmethod
|
||||
def _build(cls, update, others=None, self_id=None, *todo, **todo2):
|
||||
if isinstance(update, _tl.UpdateBotInlineQuery):
|
||||
return cls.Event(update)
|
||||
|
||||
def _set_client(self, client):
|
||||
super()._set_client(client)
|
||||
self._sender, self._input_sender = utils._get_entity_pair(self.sender_id, self._entities)
|
||||
return self
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
|
@@ -35,20 +35,18 @@ class MessageDeleted(EventBuilder, _custom.chatgetter.ChatGetter):
|
||||
for msg_id in event.deleted_ids:
|
||||
print('Message', msg_id, 'was deleted in', event.chat_id)
|
||||
"""
|
||||
def __init__(self, deleted_ids, peer):
|
||||
_custom.chatgetter.ChatGetter.__init__(self, chat_peer=peer)
|
||||
self.deleted_id = None if not deleted_ids else deleted_ids[0]
|
||||
self.deleted_ids = deleted_ids
|
||||
|
||||
@classmethod
|
||||
def _build(cls, update, others=None, self_id=None, *todo, **todo2):
|
||||
def _build(cls, client, update, entities):
|
||||
if isinstance(update, _tl.UpdateDeleteMessages):
|
||||
return cls.Event(
|
||||
deleted_ids=update.messages,
|
||||
peer=None
|
||||
)
|
||||
peer = None
|
||||
elif isinstance(update, _tl.UpdateDeleteChannelMessages):
|
||||
return cls.Event(
|
||||
deleted_ids=update.messages,
|
||||
peer=_tl.PeerChannel(update.channel_id)
|
||||
)
|
||||
peer = _tl.PeerChannel(update.channel_id)
|
||||
else:
|
||||
return None
|
||||
|
||||
self = cls.__new__(cls)
|
||||
self._client = client
|
||||
self._chat = entities.get(peer)
|
||||
self.deleted_id = None if not update.messages else update.messages[0]
|
||||
self.deleted_ids = update.messages
|
||||
return self
|
||||
|
@@ -41,7 +41,7 @@ class MessageEdited(EventBuilder):
|
||||
print('Message', event.id, 'changed at', event.date)
|
||||
"""
|
||||
@classmethod
|
||||
def _build(cls, update, others, self_id, entities, client):
|
||||
def _build(cls, client, update, entities):
|
||||
if isinstance(update, (_tl.UpdateEditMessage,
|
||||
_tl.UpdateEditChannelMessage)):
|
||||
return cls._new(client, update.message, entities, None)
|
||||
|
@@ -45,24 +45,39 @@ class MessageRead(EventBuilder):
|
||||
super().__init__(peer, self.max_id)
|
||||
|
||||
@classmethod
|
||||
def _build(cls, update, others=None, self_id=None, *todo, **todo2):
|
||||
def _build(cls, client, update, entities):
|
||||
out = False
|
||||
contents = False
|
||||
message_ids = None
|
||||
if isinstance(update, _tl.UpdateReadHistoryInbox):
|
||||
return cls.Event(update.peer, update.max_id, False)
|
||||
peer = update.peer
|
||||
max_id = update.max_id
|
||||
out = False
|
||||
elif isinstance(update, _tl.UpdateReadHistoryOutbox):
|
||||
return cls.Event(update.peer, update.max_id, True)
|
||||
peer = update.peer
|
||||
max_id = update.max_id
|
||||
out = True
|
||||
elif isinstance(update, _tl.UpdateReadChannelInbox):
|
||||
return cls.Event(_tl.PeerChannel(update.channel_id),
|
||||
update.max_id, False)
|
||||
peer = _tl.PeerChannel(update.channel_id)
|
||||
max_id = update.max_id
|
||||
out = False
|
||||
elif isinstance(update, _tl.UpdateReadChannelOutbox):
|
||||
return cls.Event(_tl.PeerChannel(update.channel_id),
|
||||
update.max_id, True)
|
||||
peer = _tl.PeerChannel(update.channel_id)
|
||||
max_id = update.max_id
|
||||
out = True
|
||||
elif isinstance(update, _tl.UpdateReadMessagesContents):
|
||||
return cls.Event(message_ids=update.messages,
|
||||
contents=True)
|
||||
peer = None
|
||||
message_ids = update.messages
|
||||
contents = True
|
||||
elif isinstance(update, _tl.UpdateChannelReadMessagesContents):
|
||||
return cls.Event(_tl.PeerChannel(update.channel_id),
|
||||
message_ids=update.messages,
|
||||
contents=True)
|
||||
peer = _tl.PeerChannel(update.channel_id)
|
||||
message_ids = update.messages
|
||||
contents = True
|
||||
|
||||
self = cls.__new__(cls)
|
||||
self._client = client
|
||||
self._chat = entities.get(peer)
|
||||
return self
|
||||
|
||||
@property
|
||||
def inbox(self):
|
||||
|
@@ -57,16 +57,8 @@ class NewMessage(EventBuilder, _custom.Message):
|
||||
await asyncio.sleep(5)
|
||||
await client.delete_messages(event.chat_id, [event.id, m.id])
|
||||
"""
|
||||
def __init__(self, message):
|
||||
self.__dict__['_init'] = False
|
||||
super().__init__(chat_peer=message.peer_id,
|
||||
msg_id=message.id, broadcast=bool(message.post))
|
||||
|
||||
self.pattern_match = None
|
||||
self.message = message
|
||||
|
||||
@classmethod
|
||||
def _build(cls, update, others, self_id, entities, client):
|
||||
def _build(cls, client, update, entities):
|
||||
if isinstance(update,
|
||||
(_tl.UpdateNewMessage, _tl.UpdateNewChannelMessage)):
|
||||
if not isinstance(update.message, _tl.Message):
|
||||
|
@@ -19,5 +19,5 @@ class Raw(EventBuilder):
|
||||
print(update.stringify())
|
||||
"""
|
||||
@classmethod
|
||||
def _build(cls, update, others=None, self_id=None, *todo, **todo2):
|
||||
def _build(cls, client, update, entities):
|
||||
return update
|
||||
|
@@ -62,33 +62,35 @@ class UserUpdate(EventBuilder, _custom.chatgetter.ChatGetter, _custom.sendergett
|
||||
if event.uploading:
|
||||
await client.send_message(event.user_id, 'What are you sending?')
|
||||
"""
|
||||
def __init__(self, peer, *, status=None, chat_peer=None, typing=None):
|
||||
_custom.chatgetter.ChatGetter.__init__(self, chat_peer or peer)
|
||||
_custom.sendergetter.SenderGetter.__init__(self, utils.get_peer_id(peer))
|
||||
@classmethod
|
||||
def _build(cls, client, update, entities):
|
||||
chat_peer = None
|
||||
status = None
|
||||
if isinstance(update, _tl.UpdateUserStatus):
|
||||
peer = _tl.PeerUser(update.user_id)
|
||||
status = update.status
|
||||
typing = None
|
||||
elif isinstance(update, _tl.UpdateChannelUserTyping):
|
||||
peer = update.from_id
|
||||
chat_peer = _tl.PeerChannel(update.channel_id)
|
||||
typing = update.action
|
||||
elif isinstance(update, _tl.UpdateChatUserTyping):
|
||||
peer = update.from_id
|
||||
chat_peer = _tl.PeerChat(update.chat_id)
|
||||
typing = update.action
|
||||
elif isinstance(update, _tl.UpdateUserTyping):
|
||||
peer = update.user_id
|
||||
typing = update.action
|
||||
else:
|
||||
return None
|
||||
|
||||
self = cls.__new__(cls)
|
||||
self._client = client
|
||||
self._sender = entities.get(peer)
|
||||
self._chat = entities.get(chat_peer or peer)
|
||||
self.status = status
|
||||
self.action = typing
|
||||
|
||||
@classmethod
|
||||
def _build(cls, update, others=None, self_id=None, *todo, **todo2):
|
||||
if isinstance(update, _tl.UpdateUserStatus):
|
||||
return UserUpdateEvent(_tl.PeerUser(update.user_id),
|
||||
status=update.status)
|
||||
elif isinstance(update, _tl.UpdateChannelUserTyping):
|
||||
return UserUpdateEvent(update.from_id,
|
||||
chat_peer=_tl.PeerChannel(update.channel_id),
|
||||
typing=update.action)
|
||||
elif isinstance(update, _tl.UpdateChatUserTyping):
|
||||
return UserUpdateEvent(update.from_id,
|
||||
chat_peer=_tl.PeerChat(update.chat_id),
|
||||
typing=update.action)
|
||||
elif isinstance(update, _tl.UpdateUserTyping):
|
||||
return UserUpdateEvent(update.user_id,
|
||||
typing=update.action)
|
||||
|
||||
def _set_client(self, client):
|
||||
super()._set_client(client)
|
||||
self._sender, self._input_sender = utils._get_entity_pair(self.sender_id, self._entities)
|
||||
return self
|
||||
|
||||
@property
|
||||
def user(self):
|
||||
|
Reference in New Issue
Block a user