mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-11-04 18:07:35 +03:00 
			
		
		
		
	Add enum for typing action
This commit is contained in:
		
							parent
							
								
									dbe66bf805
								
							
						
					
					
						commit
						03de901b7f
					
				| 
						 | 
					@ -13,6 +13,18 @@ it can take advantage of new goodies!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. contents:: List of All Versions
 | 
					.. contents:: List of All Versions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Complete overhaul of the library (v2.0)
 | 
				
			||||||
 | 
					=======================================
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					+------------------------+
 | 
				
			||||||
 | 
					| Scheme layer used: 133 |
 | 
				
			||||||
 | 
					+------------------------+
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(inc and link all of migration guide)
 | 
				
			||||||
 | 
					properly-typed enums for filters and actions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
New schema and bug fixes (v1.23)
 | 
					New schema and bug fixes (v1.23)
 | 
				
			||||||
================================
 | 
					================================
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,31 +17,6 @@ _MAX_PROFILE_PHOTO_CHUNK_SIZE = 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class _ChatAction:
 | 
					class _ChatAction:
 | 
				
			||||||
    _str_mapping = {
 | 
					 | 
				
			||||||
        'typing': _tl.SendMessageTypingAction(),
 | 
					 | 
				
			||||||
        'contact': _tl.SendMessageChooseContactAction(),
 | 
					 | 
				
			||||||
        'game': _tl.SendMessageGamePlayAction(),
 | 
					 | 
				
			||||||
        'location': _tl.SendMessageGeoLocationAction(),
 | 
					 | 
				
			||||||
        'sticker': _tl.SendMessageChooseStickerAction(),
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        'record-audio': _tl.SendMessageRecordAudioAction(),
 | 
					 | 
				
			||||||
        'record-voice': _tl.SendMessageRecordAudioAction(),  # alias
 | 
					 | 
				
			||||||
        'record-round': _tl.SendMessageRecordRoundAction(),
 | 
					 | 
				
			||||||
        'record-video': _tl.SendMessageRecordVideoAction(),
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        'audio': _tl.SendMessageUploadAudioAction(1),
 | 
					 | 
				
			||||||
        'voice': _tl.SendMessageUploadAudioAction(1),  # alias
 | 
					 | 
				
			||||||
        'song': _tl.SendMessageUploadAudioAction(1),  # alias
 | 
					 | 
				
			||||||
        'round': _tl.SendMessageUploadRoundAction(1),
 | 
					 | 
				
			||||||
        'video': _tl.SendMessageUploadVideoAction(1),
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        'photo': _tl.SendMessageUploadPhotoAction(1),
 | 
					 | 
				
			||||||
        'document': _tl.SendMessageUploadDocumentAction(1),
 | 
					 | 
				
			||||||
        'file': _tl.SendMessageUploadDocumentAction(1),  # alias
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        'cancel': _tl.SendMessageCancelAction()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def __init__(self, client, chat, action, *, delay, auto_cancel):
 | 
					    def __init__(self, client, chat, action, *, delay, auto_cancel):
 | 
				
			||||||
        self._client = client
 | 
					        self._client = client
 | 
				
			||||||
        self._chat = chat
 | 
					        self._chat = chat
 | 
				
			||||||
| 
						 | 
					@ -88,6 +63,28 @@ class _ChatAction:
 | 
				
			||||||
                await self._client(_tl.fn.messages.SetTyping(
 | 
					                await self._client(_tl.fn.messages.SetTyping(
 | 
				
			||||||
                    self._chat, _tl.SendMessageCancelAction()))
 | 
					                    self._chat, _tl.SendMessageCancelAction()))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @staticmethod
 | 
				
			||||||
 | 
					    def _parse(action):
 | 
				
			||||||
 | 
					        if isinstance(action, tlobject.TLObject) and action.SUBCLASS_OF_ID != 0x20b2cc21:
 | 
				
			||||||
 | 
					            return action
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return {
 | 
				
			||||||
 | 
					            enums.TYPING: _tl.SendMessageTypingAction(),
 | 
				
			||||||
 | 
					            enums.CONTACT: _tl.SendMessageChooseContactAction(),
 | 
				
			||||||
 | 
					            enums.GAME: _tl.SendMessageGamePlayAction(),
 | 
				
			||||||
 | 
					            enums.LOCATION: _tl.SendMessageGeoLocationAction(),
 | 
				
			||||||
 | 
					            enums.STICKER: _tl.SendMessageChooseStickerAction(),
 | 
				
			||||||
 | 
					            enums.RECORD_AUDIO: _tl.SendMessageRecordAudioAction(),
 | 
				
			||||||
 | 
					            enums.RECORD_ROUND: _tl.SendMessageRecordRoundAction(),
 | 
				
			||||||
 | 
					            enums.RECORD_VIDEO: _tl.SendMessageRecordVideoAction(),
 | 
				
			||||||
 | 
					            enums.AUDIO: _tl.SendMessageUploadAudioAction(1),
 | 
				
			||||||
 | 
					            enums.ROUND: _tl.SendMessageUploadRoundAction(1),
 | 
				
			||||||
 | 
					            enums.VIDEO: _tl.SendMessageUploadVideoAction(1),
 | 
				
			||||||
 | 
					            enums.PHOTO: _tl.SendMessageUploadPhotoAction(1),
 | 
				
			||||||
 | 
					            enums.DOCUMENT: _tl.SendMessageUploadDocumentAction(1),
 | 
				
			||||||
 | 
					            enums.CANCEL: _tl.SendMessageCancelAction(),
 | 
				
			||||||
 | 
					        }[enums.parse_typing_action(action)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def progress(self, current, total):
 | 
					    def progress(self, current, total):
 | 
				
			||||||
        if hasattr(self._action, 'progress'):
 | 
					        if hasattr(self._action, 'progress'):
 | 
				
			||||||
            self._action.progress = 100 * round(current / total)
 | 
					            self._action.progress = 100 * round(current / total)
 | 
				
			||||||
| 
						 | 
					@ -457,18 +454,7 @@ def action(
 | 
				
			||||||
        *,
 | 
					        *,
 | 
				
			||||||
        delay: float = 4,
 | 
					        delay: float = 4,
 | 
				
			||||||
        auto_cancel: bool = True) -> 'typing.Union[_ChatAction, typing.Coroutine]':
 | 
					        auto_cancel: bool = True) -> 'typing.Union[_ChatAction, typing.Coroutine]':
 | 
				
			||||||
    if isinstance(action, str):
 | 
					    action = _ChatAction._parse(action)
 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
            action = _ChatAction._str_mapping[action.lower()]
 | 
					 | 
				
			||||||
        except KeyError:
 | 
					 | 
				
			||||||
            raise ValueError(
 | 
					 | 
				
			||||||
                'No such action "{}"'.format(action)) from None
 | 
					 | 
				
			||||||
    elif not isinstance(action, tlobject.TLObject) or action.SUBCLASS_OF_ID != 0x20b2cc21:
 | 
					 | 
				
			||||||
        # 0x20b2cc21 = crc32(b'SendMessageAction')
 | 
					 | 
				
			||||||
        if isinstance(action, type):
 | 
					 | 
				
			||||||
            raise ValueError('You must pass an instance, not the class')
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            raise ValueError('Cannot use {} as action'.format(action))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if isinstance(action, _tl.SendMessageCancelAction):
 | 
					    if isinstance(action, _tl.SendMessageCancelAction):
 | 
				
			||||||
        # ``SetTyping.resolve`` will get input peer of ``entity``.
 | 
					        # ``SetTyping.resolve`` will get input peer of ``entity``.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,6 +17,27 @@ class Participant(Enum):
 | 
				
			||||||
    CONTACT = 'contact'
 | 
					    CONTACT = 'contact'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Action(Enum):
 | 
				
			||||||
 | 
					    TYPING = 'typing'
 | 
				
			||||||
 | 
					    CONTACT = 'contact'
 | 
				
			||||||
 | 
					    GAME = 'game'
 | 
				
			||||||
 | 
					    LOCATION = 'location'
 | 
				
			||||||
 | 
					    STICKER = 'sticker'
 | 
				
			||||||
 | 
					    RECORD_AUDIO = 'record-audio'
 | 
				
			||||||
 | 
					    RECORD_VOICE = RECORD_AUDIO
 | 
				
			||||||
 | 
					    RECORD_ROUND = 'record-round'
 | 
				
			||||||
 | 
					    RECORD_VIDEO = 'record-video'
 | 
				
			||||||
 | 
					    AUDIO = 'audio'
 | 
				
			||||||
 | 
					    VOICE = AUDIO
 | 
				
			||||||
 | 
					    SONG = AUDIO
 | 
				
			||||||
 | 
					    ROUND = 'round'
 | 
				
			||||||
 | 
					    VIDEO = 'video'
 | 
				
			||||||
 | 
					    PHOTO = 'photo'
 | 
				
			||||||
 | 
					    DOCUMENT = 'document'
 | 
				
			||||||
 | 
					    FILE = DOCUMENT
 | 
				
			||||||
 | 
					    CANCEL = 'cancel'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _mk_parser(cls):
 | 
					def _mk_parser(cls):
 | 
				
			||||||
    def parser(value):
 | 
					    def parser(value):
 | 
				
			||||||
        if isinstance(value, cls):
 | 
					        if isinstance(value, cls):
 | 
				
			||||||
| 
						 | 
					@ -35,3 +56,4 @@ def _mk_parser(cls):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
parse_conn_mode = _mk_parser(ConnectionMode)
 | 
					parse_conn_mode = _mk_parser(ConnectionMode)
 | 
				
			||||||
parse_participant = _mk_parser(Participant)
 | 
					parse_participant = _mk_parser(Participant)
 | 
				
			||||||
 | 
					parse_typing_action = _mk_parser(Action)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
from ._misc.enums import (
 | 
					from ._misc.enums import (
 | 
				
			||||||
    ConnectionMode,
 | 
					    ConnectionMode,
 | 
				
			||||||
    Participant,
 | 
					    Participant,
 | 
				
			||||||
 | 
					    Action,
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user