2018-04-05 21:14:22 +03:00
|
|
|
import datetime
|
2019-07-04 11:34:48 +03:00
|
|
|
import functools
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
from .common import EventBuilder, EventCommon, name_inner_event
|
2021-09-12 14:27:13 +03:00
|
|
|
from .._misc import utils
|
|
|
|
from .. import _tl
|
2021-09-12 17:58:06 +03:00
|
|
|
from ..types import _custom
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
|
2019-07-04 11:17:40 +03:00
|
|
|
# TODO Either the properties are poorly named or they should be
|
|
|
|
# different events, but that would be a breaking change.
|
2019-07-04 11:34:48 +03:00
|
|
|
#
|
|
|
|
# TODO There are more "user updates", but bundling them all up
|
|
|
|
# in a single place will make it annoying to use (since
|
|
|
|
# the user needs to check for the existence of `None`).
|
|
|
|
#
|
|
|
|
# TODO Handle UpdateUserBlocked, UpdateUserName, UpdateUserPhone, UpdateUserPhoto
|
|
|
|
|
|
|
|
def _requires_action(function):
|
|
|
|
@functools.wraps(function)
|
|
|
|
def wrapped(self):
|
|
|
|
return None if self.action is None else function(self)
|
|
|
|
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
def _requires_status(function):
|
|
|
|
@functools.wraps(function)
|
|
|
|
def wrapped(self):
|
|
|
|
return None if self.status is None else function(self)
|
|
|
|
|
|
|
|
return wrapped
|
|
|
|
|
2019-07-04 11:17:40 +03:00
|
|
|
|
2018-04-05 21:14:22 +03:00
|
|
|
@name_inner_event
|
|
|
|
class UserUpdate(EventBuilder):
|
|
|
|
"""
|
2019-06-11 12:09:22 +03:00
|
|
|
Occurs whenever a user goes online, starts typing, etc.
|
2020-02-20 12:18:26 +03:00
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from telethon import events
|
|
|
|
|
|
|
|
@client.on(events.UserUpdate)
|
|
|
|
async def handler(event):
|
|
|
|
# If someone is uploading, say something
|
|
|
|
if event.uploading:
|
|
|
|
await client.send_message(event.user_id, 'What are you sending?')
|
2018-04-05 21:14:22 +03:00
|
|
|
"""
|
2018-07-19 02:47:32 +03:00
|
|
|
@classmethod
|
2021-12-11 15:31:38 +03:00
|
|
|
def build(cls, update, others=None, self_id=None, *todo, **todo2):
|
2021-09-12 13:16:02 +03:00
|
|
|
if isinstance(update, _tl.UpdateUserStatus):
|
|
|
|
return cls.Event(_tl.PeerUser(update.user_id),
|
2019-06-30 14:23:18 +03:00
|
|
|
status=update.status)
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(update, _tl.UpdateChannelUserTyping):
|
2021-03-14 13:03:03 +03:00
|
|
|
return cls.Event(update.from_id,
|
2021-09-12 13:16:02 +03:00
|
|
|
chat_peer=_tl.PeerChannel(update.channel_id),
|
2021-03-14 13:03:03 +03:00
|
|
|
typing=update.action)
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(update, _tl.UpdateChatUserTyping):
|
2021-03-14 12:58:22 +03:00
|
|
|
return cls.Event(update.from_id,
|
2021-09-12 13:16:02 +03:00
|
|
|
chat_peer=_tl.PeerChat(update.chat_id),
|
2019-06-30 14:23:18 +03:00
|
|
|
typing=update.action)
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(update, _tl.UpdateUserTyping):
|
2019-06-30 14:23:18 +03:00
|
|
|
return cls.Event(update.user_id,
|
|
|
|
typing=update.action)
|
2018-04-05 21:14:22 +03:00
|
|
|
|
2021-09-12 17:58:06 +03:00
|
|
|
class Event(EventCommon, _custom.sendergetter.SenderGetter):
|
2018-04-05 21:14:22 +03:00
|
|
|
"""
|
2019-06-11 12:09:22 +03:00
|
|
|
Represents the event of a user update
|
|
|
|
such as gone online, started typing, etc.
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
Members:
|
2019-07-04 11:17:40 +03:00
|
|
|
status (:tl:`UserStatus`, optional):
|
|
|
|
The user status if the update is about going online or offline.
|
2018-04-05 21:14:22 +03:00
|
|
|
|
2019-07-04 11:17:40 +03:00
|
|
|
You should check this attribute first before checking any
|
2019-07-04 11:34:48 +03:00
|
|
|
of the seen within properties, since they will all be `None`
|
2019-07-04 11:17:40 +03:00
|
|
|
if the status is not set.
|
2018-04-05 21:14:22 +03:00
|
|
|
|
|
|
|
action (:tl:`SendMessageAction`, optional):
|
|
|
|
The "typing" action if any the user is performing if any.
|
|
|
|
|
2019-07-04 11:17:40 +03:00
|
|
|
You should check this attribute first before checking any
|
2019-07-04 11:34:48 +03:00
|
|
|
of the typing properties, since they will all be `None`
|
2019-07-04 11:17:40 +03:00
|
|
|
if the action is not set.
|
2018-04-05 21:14:22 +03:00
|
|
|
"""
|
2021-03-14 13:03:03 +03:00
|
|
|
def __init__(self, peer, *, status=None, chat_peer=None, typing=None):
|
|
|
|
super().__init__(chat_peer or peer)
|
2021-09-12 17:58:06 +03:00
|
|
|
_custom.sendergetter.SenderGetter.__init__(self, utils.get_peer_id(peer))
|
2019-03-26 11:18:18 +03:00
|
|
|
|
2019-07-04 11:17:40 +03:00
|
|
|
self.status = status
|
2018-04-05 21:14:22 +03:00
|
|
|
self.action = typing
|
|
|
|
|
2019-03-26 11:14:55 +03:00
|
|
|
def _set_client(self, client):
|
|
|
|
super()._set_client(client)
|
2019-05-01 18:52:32 +03:00
|
|
|
self._sender, self._input_sender = utils._get_entity_pair(
|
|
|
|
self.sender_id, self._entities, client._entity_cache)
|
2019-03-28 12:47:15 +03:00
|
|
|
|
2018-04-05 21:14:22 +03:00
|
|
|
@property
|
2018-06-25 12:03:20 +03:00
|
|
|
def user(self):
|
2019-05-09 13:24:37 +03:00
|
|
|
"""Alias for `sender <telethon.tl.custom.sendergetter.SenderGetter.sender>`."""
|
2019-03-26 11:18:18 +03:00
|
|
|
return self.sender
|
2018-06-25 12:03:20 +03:00
|
|
|
|
|
|
|
async def get_user(self):
|
2019-05-09 13:24:37 +03:00
|
|
|
"""Alias for `get_sender <telethon.tl.custom.sendergetter.SenderGetter.get_sender>`."""
|
2019-03-26 11:18:18 +03:00
|
|
|
return await self.get_sender()
|
2018-04-08 15:22:11 +03:00
|
|
|
|
|
|
|
@property
|
2018-06-25 12:03:20 +03:00
|
|
|
def input_user(self):
|
2019-05-09 13:24:37 +03:00
|
|
|
"""Alias for `input_sender <telethon.tl.custom.sendergetter.SenderGetter.input_sender>`."""
|
2019-03-26 11:18:18 +03:00
|
|
|
return self.input_sender
|
2018-06-25 12:03:20 +03:00
|
|
|
|
|
|
|
async def get_input_user(self):
|
2019-05-09 13:24:37 +03:00
|
|
|
"""Alias for `get_input_sender <telethon.tl.custom.sendergetter.SenderGetter.get_input_sender>`."""
|
2019-03-26 11:18:18 +03:00
|
|
|
return await self.get_input_sender()
|
2018-04-08 15:22:11 +03:00
|
|
|
|
|
|
|
@property
|
2018-06-25 12:03:20 +03:00
|
|
|
def user_id(self):
|
2019-05-09 13:24:37 +03:00
|
|
|
"""Alias for `sender_id <telethon.tl.custom.sendergetter.SenderGetter.sender_id>`."""
|
2019-03-26 11:18:18 +03:00
|
|
|
return self.sender_id
|
2019-07-04 11:17:40 +03:00
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def typing(self):
|
|
|
|
"""
|
|
|
|
`True` if the action is typing a message.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
return isinstance(self.action, _tl.SendMessageTypingAction)
|
2019-07-04 11:17:40 +03:00
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def uploading(self):
|
|
|
|
"""
|
|
|
|
`True` if the action is uploading something.
|
|
|
|
"""
|
|
|
|
return isinstance(self.action, (
|
2021-09-12 13:16:02 +03:00
|
|
|
_tl.SendMessageChooseContactAction,
|
|
|
|
_tl.SendMessageChooseStickerAction,
|
|
|
|
_tl.SendMessageUploadAudioAction,
|
|
|
|
_tl.SendMessageUploadDocumentAction,
|
|
|
|
_tl.SendMessageUploadPhotoAction,
|
|
|
|
_tl.SendMessageUploadRoundAction,
|
|
|
|
_tl.SendMessageUploadVideoAction
|
2019-07-04 11:17:40 +03:00
|
|
|
))
|
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def recording(self):
|
|
|
|
"""
|
|
|
|
`True` if the action is recording something.
|
|
|
|
"""
|
|
|
|
return isinstance(self.action, (
|
2021-09-12 13:16:02 +03:00
|
|
|
_tl.SendMessageRecordAudioAction,
|
|
|
|
_tl.SendMessageRecordRoundAction,
|
|
|
|
_tl.SendMessageRecordVideoAction
|
2019-07-04 11:17:40 +03:00
|
|
|
))
|
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def playing(self):
|
|
|
|
"""
|
|
|
|
`True` if the action is playing a game.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
return isinstance(self.action, _tl.SendMessageGamePlayAction)
|
2019-07-04 11:17:40 +03:00
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def cancel(self):
|
|
|
|
"""
|
|
|
|
`True` if the action was cancelling other actions.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
return isinstance(self.action, _tl.SendMessageCancelAction)
|
2019-07-04 11:17:40 +03:00
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def geo(self):
|
|
|
|
"""
|
|
|
|
`True` if what's being uploaded is a geo.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
return isinstance(self.action, _tl.SendMessageGeoLocationAction)
|
2019-07-04 11:17:40 +03:00
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def audio(self):
|
|
|
|
"""
|
|
|
|
`True` if what's being recorded/uploaded is an audio.
|
|
|
|
"""
|
|
|
|
return isinstance(self.action, (
|
2021-09-12 13:16:02 +03:00
|
|
|
_tl.SendMessageRecordAudioAction,
|
|
|
|
_tl.SendMessageUploadAudioAction
|
2019-07-04 11:17:40 +03:00
|
|
|
))
|
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def round(self):
|
|
|
|
"""
|
|
|
|
`True` if what's being recorded/uploaded is a round video.
|
|
|
|
"""
|
|
|
|
return isinstance(self.action, (
|
2021-09-12 13:16:02 +03:00
|
|
|
_tl.SendMessageRecordRoundAction,
|
|
|
|
_tl.SendMessageUploadRoundAction
|
2019-07-04 11:17:40 +03:00
|
|
|
))
|
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def video(self):
|
|
|
|
"""
|
|
|
|
`True` if what's being recorded/uploaded is an video.
|
|
|
|
"""
|
|
|
|
return isinstance(self.action, (
|
2021-09-12 13:16:02 +03:00
|
|
|
_tl.SendMessageRecordVideoAction,
|
|
|
|
_tl.SendMessageUploadVideoAction
|
2019-07-04 11:17:40 +03:00
|
|
|
))
|
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def contact(self):
|
|
|
|
"""
|
|
|
|
`True` if what's being uploaded (selected) is a contact.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
return isinstance(self.action, _tl.SendMessageChooseContactAction)
|
2019-07-04 11:17:40 +03:00
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def document(self):
|
|
|
|
"""
|
|
|
|
`True` if what's being uploaded is document.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
return isinstance(self.action, _tl.SendMessageUploadDocumentAction)
|
2019-07-04 11:17:40 +03:00
|
|
|
|
2021-09-01 16:08:58 +03:00
|
|
|
@property
|
|
|
|
@_requires_action
|
|
|
|
def sticker(self):
|
|
|
|
"""
|
|
|
|
`True` if what's being uploaded is a sticker.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
return isinstance(self.action, _tl.SendMessageChooseStickerAction)
|
2021-09-01 16:08:58 +03:00
|
|
|
|
2019-07-04 11:17:40 +03:00
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def photo(self):
|
|
|
|
"""
|
|
|
|
`True` if what's being uploaded is a photo.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
return isinstance(self.action, _tl.SendMessageUploadPhotoAction)
|
2019-07-04 11:17:40 +03:00
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_action
|
2019-07-04 11:17:40 +03:00
|
|
|
def last_seen(self):
|
|
|
|
"""
|
|
|
|
Exact `datetime.datetime` when the user was last seen if known.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
if isinstance(self.status, _tl.UserStatusOffline):
|
2019-07-04 11:17:40 +03:00
|
|
|
return self.status.was_online
|
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_status
|
2019-07-04 11:17:40 +03:00
|
|
|
def until(self):
|
|
|
|
"""
|
|
|
|
The `datetime.datetime` until when the user should appear online.
|
|
|
|
"""
|
2021-09-12 13:16:02 +03:00
|
|
|
if isinstance(self.status, _tl.UserStatusOnline):
|
2019-07-04 11:17:40 +03:00
|
|
|
return self.status.expires
|
|
|
|
|
|
|
|
def _last_seen_delta(self):
|
2021-09-12 13:16:02 +03:00
|
|
|
if isinstance(self.status, _tl.UserStatusOffline):
|
2019-07-04 11:17:40 +03:00
|
|
|
return datetime.datetime.now(tz=datetime.timezone.utc) - self.status.was_online
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(self.status, _tl.UserStatusOnline):
|
2019-07-04 11:17:40 +03:00
|
|
|
return datetime.timedelta(days=0)
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(self.status, _tl.UserStatusRecently):
|
2019-07-04 11:17:40 +03:00
|
|
|
return datetime.timedelta(days=1)
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(self.status, _tl.UserStatusLastWeek):
|
2019-07-04 11:17:40 +03:00
|
|
|
return datetime.timedelta(days=7)
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(self.status, _tl.UserStatusLastMonth):
|
2019-07-04 11:17:40 +03:00
|
|
|
return datetime.timedelta(days=30)
|
2019-07-04 11:34:48 +03:00
|
|
|
else:
|
|
|
|
return datetime.timedelta(days=365)
|
2019-07-04 11:17:40 +03:00
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_status
|
2019-07-04 11:17:40 +03:00
|
|
|
def online(self):
|
|
|
|
"""
|
|
|
|
`True` if the user is currently online,
|
|
|
|
"""
|
|
|
|
return self._last_seen_delta() <= datetime.timedelta(days=0)
|
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_status
|
2019-07-04 11:17:40 +03:00
|
|
|
def recently(self):
|
|
|
|
"""
|
|
|
|
`True` if the user was seen within a day.
|
|
|
|
"""
|
|
|
|
return self._last_seen_delta() <= datetime.timedelta(days=1)
|
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_status
|
2019-07-04 11:17:40 +03:00
|
|
|
def within_weeks(self):
|
|
|
|
"""
|
|
|
|
`True` if the user was seen within 7 days.
|
|
|
|
"""
|
|
|
|
return self._last_seen_delta() <= datetime.timedelta(days=7)
|
|
|
|
|
|
|
|
@property
|
2019-07-04 11:34:48 +03:00
|
|
|
@_requires_status
|
2019-07-04 11:17:40 +03:00
|
|
|
def within_months(self):
|
|
|
|
"""
|
|
|
|
`True` if the user was seen within 30 days.
|
|
|
|
"""
|
|
|
|
return self._last_seen_delta() <= datetime.timedelta(days=30)
|